User login

Adding to the content types settings forms with form_alter

in

drupal form_alter node type settings form

How do you find out what the form ID of the node settings forms are?

The forms in question are those listed under:

Administer » Content » Content types
admin/content/types

It's as easy as clicking on one of them:

admin/content/types/casetracker-basic-project

And view source. A little ways down in the HTML:

<form action="/joeaustin/?q=admin/content/types/casetracker-basic-project"  method="post" id="node-type-form">

The form ID is probably actually node_type_form, as underscores are replaced with dashes (n-dash, the minus sign) for creating the CSS ID for a form.

This of course works to get the form ID of any form you're interested in in Drupal.

(A surer way to get the form ID is to write some code in a form_alter hook that displays the form ID for every form you visit. Agaric will look up where we used that and post it.)

OK it's so simple, why wait. To get the exact form ID for any and all Drupal forms, put this immediately inside your module's implementation of hook_form_alter, before any switch or case statement:

drupal_set_message("Form ID: " . $form_id);

So if it's the only thing in your module's form_alter function, and your module is named cmt (it better not be, we took that for Community-managed Taxonomy!) the full code for your implementation of hook_form_alter would be:

<?php
function cmt_form_alter($form_id, &$form) {
drupal_set_message("Form ID: " . $form_id);
}
?>

This will give you the form ID (or IDs for multiple forms) on every page you visit. (It will give it to everyone else who visits your site too, so as with all development work, it is not recommended for live sites. Note that Agaric doesn't indent code used for temporary testing; we're not sure what other Drupal developers would think of this practice.)

So in our example above the result is indeed:

Form ID: node_type_form

Now you know what to put in the case: comparisons of your switch statement to grab the data for any form to add to or change that form.

This Agaric tutorial should be made 1/6th the length and put in the Drupal developers handbook, if this isn't in there yet (obviously, we couldn't find it, but it might be there).

Comments

This doesn't seems to work for Drupal 7

As form_id seems to be an array.

form_id is not an array in 7

This is a very old post so some things may have changed, but form_id is still a string.

Your problem is probably the order of arguments, see http://api.drupal.org/hook_form_alter

And once you know the form_id consider using http://api.drupal.org/hook_form_FORM_ID_alter

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.