Drupal hook_form_alter() On Node Form

I just spend the last few minutes looking for the solution to this so I thought that someone else might be able to benefit from it.

Calling the hook_form_alter() hook in your module using just the module name allows you to target all forms in your system, if that's the sort of thing you want to do. For a module called "My Module" this would be called mymodule_form_alter().

The power of hook_form_alter() hook is that it alos allows you to specifically target a form with a specific name within a module. A good example is if you want to alter the search form to add a class, which is a common task. To do this, create a module so that you can add the hook_form_alter() hook to it and then construct the function name in the following way:

module name + '_form_' + form ID + '_alter'

Note that any hyphens in the form ID are changed to underscores. This would create following function when looking at the search form specifically, as in the example given above.

function mymodule_form_search_theme_form_alter(&$form, &$form_state) {
    $vars['#attributes'] = array('class' => 'search-form-thing'); 
}

When using hook_form_alter() you normally look at the HTML of the page to find the ID of the form in question to construct the function name from this ID. However, when trying to look at the node edit form this format is slightly different, which caused me to spend a few minutes trying to figure out what was going on. In this case the ID of the form is not "node-form" but "contenttype-node-form". So in order to alter the blog node edit form you would create the following function:

function mymodule_form_blog_node_form_alter(&$form, &$form_state) {
    $vars['#attributes'] = array('class' => 'node-edit-form'); 
}

This will allow you to alter the node edit form.

Comments

I'm stumped because, theoretically, the $nid isn't created nor available at the time when the page loads.

 

I'd like to use it to db_insert into a field belonging to a table different than {node} - - - -  so that I can perform joins in future queries

 

Many thnx for your time and consideration

 

-Ben

Permalink

You are correct. The nid isn't available in the form until the node has been saved, which would mean that the form is in the edit state. In this case the node object is kept in the $form['node']->nid parameter.

What you need is to create a hook that will intercept the information after it is saved. This would probably be hook_insert($node), which gets passed the node information after it has been saved and so contains the nid you want.

Of course, you'll probably want to create another hook (something like hook_load()) that will load the information into the node object that you saved in the previous hook.

I hope that helps :)

Name
Philip Norton
Permalink

Hi Philip,

 

Many thnx for your guidance! It definitely opened up doors for me to further research. I took your advice and understand how hook_node_insert will perform what I am looking for. I noticed that hook_insert works so long that I use it in the same module that defines the the node type. However, I am planning on using module [B] to add fields to a form that has been created in module [A] - - module[A].install being where the node type is created & module[B] is where I plan to put the hook_node_insert. 

 

I've been reading about creating node types programatically. In my readings, they recommend creating instances (and the fields they align with) inside the install file.

 

    // Create all the fields we are adding to our content type.

    foreach (_node_example_installed_fields() as $field) {

    field_create_field($field);

    }

 

    // Create all the instances for our fields.

    foreach (_node_example_installed_instances() as $instance) {

    $instance['entity_type'] = 'node';

    $instance['bundle'] = $avatard['type'];

    field_create_instance($instance);

    }

 

My question is this:

 

If I have one module[A] where I create the node type and another module[B] that has a hook_form_alter that adds field(s) to the form_id of the node creation form in module[A] ,,,,

 

,,,to define the table structure that I need to hook_node_insert into, would I create an additional instance in module[B].install or should I have preplanned (when creating module[A].install) and added those extra instances in module[A].install - - - even though the instances would be orphaned until the hook_form_alter in module[B] comes into play?

 

Or should I bypass this complexity and just use hook_schema in the .install file of module[B]?

 

Many thnx for taking the time to help.

 

-Ben

Permalink

Thank you

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.