0

While editing a node I've printed the dsm($form) in hook_form_alter and in the dsm array I see the following values:

....
form_id (Array, 3 elements)

    #type (String, 6 characters ) hidden
    #value (String, 14 characters ) unit_node_form
    #id (String, 19 characters ) edit-unit-node-form

.....

This node's machine name is "unit" I want to know what is the difference between #value and #id values.

I want to catch form alter of only this node while editing it.

2

Typically you would use a check on $form_id to make sure your hook_form_alter would only apply the specific form. A node editing form is normally only present on edit pages, so it would only apply to edit pages as the form is not displayed anywhere else.

Don't forget that hook_form_alter is called on all forms, so you need to check for the some values to not apply it to all forms. Usually this is the form_id.

To check for a specific form you would use:

/**
* Implementation of HOOK_form_alter()
*/
function MYMODULE_form_alter(&$form, $form_state, $form_id) {

  // 1.UNIT CONTENT TYPE FORM
  if ($form_id == 'unit_node_form') {

  }

}

This can be helpful if you modify multiple forms in one module. If you prefer not to do that you should maybe use hook_form_FORM_ID_alter() to modify only a specific form.

Checking the form api $form_id is defined as:

String representing the name of the form itself. Typically this is the name of the function that generated the form.

The question this throws up now is:

Is $form_id defined from

$form['form_id']['#value'] 

or from

$form['#form_id']

Both of which give the same results. So unfortunately I can not directly give you an answer, other than that the form will only display on edit pages, so using $form_id seems to be standard proceedure.

  • Thanks but my question is whether form id of "unit_node_form" specifies the unit node form while in edit mode? That is I want to change the form field only in edit mode. To be sure do I also need to check for edit-unit-node-form ? – AgA Dec 6 '11 at 12:55
  • I am confused! When you say 'edit mode', do you mean when an already existing node is edited as opposed to when a new node is created? Please clarify. – tecjam Dec 6 '11 at 13:13
  • In both the cases if node is new or being re-edited. I don't want the user to enter any title. – AgA Dec 6 '11 at 13:22
  • In that case I have already provided you the answer. The node edit form will only be shown on the edit or create page. Nowhere else unless you have a really, really strange setup. so using $form_id will work perfectly. – tecjam Dec 6 '11 at 13:27
  • 1
    Yes, that is correct. The form is only shown on the edit and create page. If you would like to check to see the form_id to make sure it is right for every form shown anywhere, create a basic module - add mymodule_form_alter function - like above - but before checking the form id add this line dsm($form_id); and it will show your form_id on any form in the message area. that way you can double check and make sure you're applying your function to the correct form_id. – tecjam Dec 6 '11 at 13:39

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.