1

I've created a module which uses hook_form_alter(). When a form is loaded (node/add) I hide a field, and the hook works fine. The problem appears when I do a preview of the form. When the form appears again I see the hidden field (the hook doesn't work). Why?

I hide the field through the line:

$form['field_versio']['und']['0']['value']['#type']="hidden";

2 Answers 2

1

First, you should use #access = FALSE which will disallow access to a element. #type hidden is not safe, it means that it is printed as a hidden input field which can be easily changed by the user, for example with Firebug.

When you do that, you can also directly set #access on the field element, like $form['field_versio']['#access'] and it will disallow access to all subelements (if there are multiple).

Not sure why it doesn't work in Preview, try adding a drupal_set_message('test') call to see if your hook is called.

1
  • Thanks a lot for your advice, Berdir. I have already changed the way I hide the field 'versio'. And definetely, hook_form_alter doesn't run when I click on preview button. drupal_set_message('test') appears in the first load of the form but it doesn't appear in the preview page of the form. Why? I haven't found another hook for previews.
    – user709
    Apr 13, 2011 at 7:35
0

hook_node_validate() is the answer.

I have built a personal function X that includes a drupal_add_js function that I need to link to my form.

Depending on the form (content-type) I call my function X from hook_form_alter() (node/add first load) or from hook_node_validate() (node/add after previewing, validating,...).

function my_javascripts($form_id) {
  $path = drupal_get_path('module', 'MYMODULE');
  switch ($form_id) {
    case "CONTENTTYPE-node-form":
        drupal_add_js($path.'/CONTENTTYPE.js','file');
    break;
  }
} 

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  my_javascripts($form['#id']);
}

function MYMODULE_node_validate($node, $form, &$form_state) {
  my_javascripts($form['#id']); 
}

Your Answer

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

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