0

I am at wits' end. I have a clean install of Drupal 7.28 and a custom module with the following two hooks:

/**
 * Implements hook_form_alter().
 * /
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  dpm($form_state, 'form_alter');
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 * /
function mymodule_form_node_form_alter(&$form, &$form_state, $form_id) {
  dpm($form_state, 'BASE_FORM_ID_alter');
}

And that is all there currently is in the custom module.

I have verified that the the base_form_id for the Article node form is node_form, by printing it out (using dpm($form_state). This is what is printed:

base_form_id (String, 9 characters ) node_form

And yes, I have cleared the cache (I've even restarted apache and rebooted the server).

I believe my code is identical to what is shown in numerous tutorials about how to use hook_form_BASE_FORM_ID_alter().

Still, when I open the form to create an article, the first hook (hook_form_alter()) fires, and the second (hook_form_BASE_FORM_ID_alter()) does not.

This hook is in 54 functions in the core, so it is very unlikely that it just does not work.

As I said, I am at wits' end.

Has anyone a suggestion for what to try next?

6
  • Strange, I am trying your code and I am receiving output from mymodule_form_node_form_alter().
    – devunder
    Commented Jul 6, 2014 at 21:04
  • Thanks! That at least confirms that it should work and that no magic is necessary. There is obviously some quirk somewhere in my site. Starting again with a squeaky-clean install. Commented Jul 6, 2014 at 21:10
  • Not sure if relevant but I while copying your code I did received a warning. Warning: Unterminated comment, but perhaps that sneaked in while placing the code in the question.
    – devunder
    Commented Jul 6, 2014 at 21:13
  • FWIW this has never worked for me either, if memory serves I've always had to use hook_form_alter() and strpos the $form_id to target all node forms in one go. Never did look into why, if I get some time I will...
    – Clive
    Commented Jul 6, 2014 at 21:19
  • @undersound, eureka! (and I feel stupid now). Please post this as an answer and you'll get your 25 rep points if you manage to get it typed up before this is closed as PEBKAC. Commented Jul 6, 2014 at 22:19

2 Answers 2

1

Not sure if relevant but I while copying your code I did received a warning. Warning: Unterminated comment, but perhaps that sneaked in while placing the code in the question.

0

These are always fun... First, did you clear your cache? You always have to clear your cache when adding or changing a hook, and, to be honest, your code looks correct.

Second, to find out the BASE_FORM_ID, to better target a node edit form, you can do this to output all the form ids on the page:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  dsm($form_id);
}

You may discover that the more-targeted $form_id is something like article_node_form. I have seen it both ways.

3
  • Yup, cleared the cache, restarted apache, rebooted the server. Sorry about leaving that detail out. Your method for getting the BASE_FORM_ID is wrong tho'. What your method extracts is the form_id (which is article_node_form for an article). That is not the base_form_id. Commented Jul 6, 2014 at 20:47
  • Correct, it is the form_id, but that works, too. I just double-checked some of my sites to verify that I remembered it correctly. We used it when we needed to modify multiple node forms in different ways, so rather than having a big function with a switch statement, we targeted the individual forms. The cache clear should have been enough (are you using any exotic caching solutions such as memcached or APC cache?)
    – coreyp_1
    Commented Jul 6, 2014 at 21:00
  • Well, my use case is the opposite, I have around 50 different node forms, and I want to add a field to each of them, so I rather than targeting all 50 individual forms, I want to target all of them with a single hook that (if it had worked) would fire on all of them. That is pretty a common use of hook_form_BASE_FORM_ID_alter() if tutorials are anything to go by. Commented Jul 6, 2014 at 21:05

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.