8

I tried:

mymodule_form_node_alter

mymodule_form_node_form_alter

and global hook:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form['#id'] == 'node-form') {
      #set message
  }
}

None from above works for me.

I need to create hook that invokes on every node form. if it possible.

4 Answers 4

11
function yourmodule_form_alter(&$form, &$form_state, $form_id) {
    if (strpos($form_id, '_node_form') !== FALSE) {
      // ...
    }
}
0
25
/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function MYMODULE_form_node_form_alter(&$form, &$form_state) {
  // Do stuff.
}

It could be that it didn't work because you have to clear the cache after adding the hook.

1
  • 1
    This is the right way to target node forms in D7..!! Commented Mar 2, 2015 at 11:37
2

Use hook_form_BASE_FORM_ID_alter() to provide a form-specific alteration for shared forms. For your example this would be:

function yourmodule_form_node_form_alter(&$form, &$form_state, $form_id) {
  // code that runs on every node form
}

https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_form_BASE_FORM_ID_alter/7

1

I find better solution:

if (isset($form['#node'])) {

}

Another solution is to use: hook_form_BASE_FORM_ID_alter()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.