0

When creating a new content type, the $form_id is correctly picked up in my module. Let's say in my example, my form is called the 'foo_form'. The below hook_form_FORM_ID_alter function works fine when I go to add/content/foo-form

function MyModule_form_foo_form_alter(&$form,&$form_state,$form_id) {
  echo 'It works!';
}

The problem is that my content type has some required fields. If the user does not fill them in all out and submits, then the submission fails as expected.

The problem is that now on the error page telling the user that he or she needs to fill out the required fields, the $form_id is no longer 'foo_form', but instead named 'search_block_form' because on all my pages I have a search block on the left side bar. Now my hook_form_FORM_ID_alter() does not get invoked because my module is getting 'search_block_form' as its $form_id instead of 'foo_form'.

I tried using hook_form_alter(), which works globally, but because there are 2 $form_id's on this required fields error message page, my MyModule_form_foo_form_alter() gets called twice (once for 'search_block_form' and 'foo_form')!

How do I resolve this issue, other than exclude displaying the search form block when adding new content? How do I make 'foo_form' the primary $form_id???? I find this odd since if I go to add a new content, Drupal is "smart" enough to ignore the 'search_block_form' $form_id.

EDIT: My code to test visibility about what $form_id's are present before submitting and after submitted without filling in all the required fields.

function MyModule_form_alter(&$form,&$form_state,$form_id) {
  case 'search_block_form':
    //this one always shows up
    echo $form_id . "<br>";
  break;
  case 'foo_form':
    //does *not* show up when submitting form 
    //with missing required field values
    echo $form_id . "<br>";
  break;
}
  • by the way what is this foo_form is it a form you have created? – Mohammed Shameem Jan 25 '13 at 19:28
  • 'foo_form' is a standard Drupal content type I created using 'admin/structure/types/add'. – user785179 Jan 25 '13 at 19:31
  • and you are trying to add a node of that content type. and that is were you want to change your form elements. Hope that is what you want to do. – Mohammed Shameem Jan 25 '13 at 19:32
  • My goal is being able to use hook_form_alter() correctly so I can inject some form changes. I need to be able to target the $form_id as to not run it all the time unnecessarily – user785179 Jan 25 '13 at 19:35
  • OK let me get this right. you want to alter a node edit form of a specific content type and not all the content type. is that what you are trying to do? – Mohammed Shameem Jan 25 '13 at 19:39
0

To do form validation use the form API validation capabilities:

http://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#validation

http://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#validate

  • I don't need to do any kind form validation. I am not sure how this is applicable. Can you clarify? – user785179 Jan 25 '13 at 19:09
0

Use hook_form_alter()

and in that use a if condition

if($form_id == 'foo_form')
{
  //do your stuff
}

To target a specific node edit page you would use the following function

MY_MODULE_NAME_form_CONTENT_TYPE_NAME_node_form_alter(&$form, &$form_state, $form_id)

You can try out the following test to see if you the hook is working during the validation phase.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form['title']['#description'] = 'a';

}

function mymodule_form_node_form_alter(&$form, &$form_state, $form_id) {
  $form['title']['#description'] .= 'b';
  dsm($form_id);
}

function mymodule_form_page_node_form_alter(&$form, &$form_state, $form_id) {
  $form['title']['#description'] .= 'c';
}

This will add a b and c below the title.

The first two function would work on all the node add edit pages the last one would work on the Basic Page alone you can change the page to the name of your content type to test it with your content type.

  • As I mentioned, this does not work. Drupal becomes "dumb" and the $form_id of 'foo_form' no longer exists. I can verify this by doing an 'echo $form_id'. Only the 'search_block_form' gets picked up when not all required fields are entered on the form. However, when adding a new content, both 'foo_form' and 'search_block_form' are there. Any other ideas? – user785179 Jan 25 '13 at 19:08
  • could you show the code you are using. – Mohammed Shameem Jan 25 '13 at 19:18
  • I added the code in my edit way above. I think you can test this yourself by creating a new content type and then adding a required field. Make sure to have a search block on the sides. Then, submit the form without filling in the required field and watch as your $form_id is missing! :( – user785179 Jan 25 '13 at 19:25
  • I never knew such a hook existed, unfortunately, I have the same problem. :( If I submit the form without filling in all the required values and there is another form (i.e. search block), then this hook does not get called. ARghhh! Any other ideas? – user785179 Jan 25 '13 at 20:01
  • that is strange I have tried it with search form and it still gets called. have you tried it without the case statement. – Mohammed Shameem Jan 26 '13 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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