List all form ids existing all over site

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
heykarthikwithu's picture

A function which returns all form ids of the site

Comments

use form Alter

rajiv.singh's picture

<?php
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
 
dpm($form_id);
}
?>

This function will show all form ids on the page.

list all form ids of the site

heykarthikwithu's picture

Yes, hook_form_alter will return form ids existing on the page,

Is their any possibility to get all form ids existing all over the site.

Thanks,
heykarthikwithu,
Dream it, Drupal it.

There no drupal way solution, But you can do this

krknth's picture

There is no way you can get all form_id 's. as rajiv.singh mentioned above you can see form id when that form is triggered.
But i can suggest you do this in following way

<?php
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
 
 
// Save form id in your custom table
  // Before saving check already exist or NOT

$count_of_form_id = db_query(" COUNT QUERY WITH FORM_ID WHICH TRIGGERED ");

if(
$count_of_form_id ) {
   
db_query("INSERT QUERY ")
}

}
?>

Now you can list form ids, But each form should be triggered once atleast

Does this work?

uberhacker's picture

I needed to find all form ids for a custom module I wrote recently:

<?php
/**
* Returns an array of all form ids.
*/
function my_module_get_all_form_ids($path = DRUPAL_ROOT) {
 
$form_ids = array();
 
// Locate all form ids from 'drupal_get_form' page callbacks.
 
$forms = db_query("SELECT page_arguments FROM {menu_router} WHERE page_callback = 'drupal_get_form'");
  foreach (
$forms as $form) {
   
$args = unserialize($form->page_arguments);
    if (isset(
$args[0])) {
     
$form_id = $args[0];
     
$form_ids[$form_id] = $form_id;
    }
  }
 
// Locate all form ids from 'drupal_get_form' function calls.
 
$slash = (isset($_SERVER['SERVER_SOFTWARE']) && stristr($_SERVER['SERVER_SOFTWARE'], 'win')) ? '\\' : '/';
 
$files = my_module_get_files($path);
 
$strings = array(
   
'drupal_get_form("',
   
'drupal_get_form(\'',
  );
  foreach (
$files as $file) {
   
$filename = file($path . $slash . $file);
    foreach (
$filename as $line => $text) {
      foreach (
$strings as $string) {
        if (
strpos($text, $string) !== FALSE) {
         
$form_id = substr($text, strpos($text, $string) + strlen($string));
          if (
$form_id && (strpos($form_id, ',') !== FALSE)) {
           
// Find the end of the form id.
           
$form_id = trim(substr($form_id, 0, strpos($form_id, ',') - 1));
           
// Remove trailing quotes and white space.
           
$form_id = trim(str_replace(array("'", '"'), '', $form_id));
           
// Ignore concatenated or embedded variable form ids.
           
if (ctype_alnum(str_replace('_', '', $form_id))) {
             
$form_ids[$form_id] = $form_id;
            }
          }
        }
      }
    }
  }
 
asort($form_ids);
  return
$form_ids;
}

/**
* Returns all files in Drupal root.
*/
function my_module_get_files($dir, $prefix = '') {
 
$exclude_dirs = array(
   
DRUPAL_ROOT . '/.git',
   
DRUPAL_ROOT . '/' . variable_get('file_public_path', 'sites/default/files'),
  );
 
$result = array();
  if (!
in_array($dir, $exclude_dirs)) {
   
$dir = rtrim($dir, '/');
    foreach (
glob("$dir/*", GLOB_MARK) as &$file) {
      if (
substr($file, -1) === '/') {
       
$result = array_merge($result, my_module_get_files($file, $prefix . basename($file) . '/'));
      }
      else {
       
$result[] = $prefix . basename($file);
      }
  }
  return
$result;
}
?>

"God gave us two ears and one mouth to remind us we should listen twice as much as we talk."

Sandbox project

uberhacker's picture

I have added a sandbox project custom module that displays a report of form ids and associate file or path references. See https://www.drupal.org/sandbox/uberhacker/2686091.

"God gave us two ears and one mouth to remind us we should listen twice as much as we talk."

Bangalore

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: