0

I am using hook_form_alter in template.php to add datepicker to a field on an exposed view filter.

function MYTHEME_form_alter(&$form, &$form_state, $form_id) {

  if($form_id == "views_exposed_form"){
    $form['data_2_op'] = array(
            '#type' => 'date_popup',
            '#date_format' => 'Y-m-d'
        );
    }
}

But all it is doing is adding another field into the form, rather than editing the field I have declared. Is there a way I can just add #type > 'date_popup' to an existing field?

EDIT:

Results after using...

drupal_set_message($form_id);  // print form ID to messages
  drupal_set_message(print_r($form, TRUE));  // print array to messages

views_exposed_form
Array ( 
  [#info] => Array ( 
    [filter-combine] => Array ( 
      [operator] => combine_op 
      [value] => combine 
      [label] => Search Event ID 
      [description] => 
    ) 
[filter-data_2] => Array ( 
  [operator] => data_2_op 
  [value] => data_2 
  [label] => Choose Date [description] => 
  )
[filter-data] => Array ( 
  [value] => data 
  [label] => Data field 
  [description] => ) 
  [filter-data_1] => Array ( 
    [value] => data_1 
    [label] => Data field 
    [description] => 
    ) 
  ) 
[combine] => Array ( 
  [#type] => textfield 
  [#size] => 30 
  [#default_value] => ) 
[data_2] => Array (
  [#type] => textfield 
  [#size] => 30 
  [#default_value] => ) 

1 Answer 1

1

hook_form_alter work only with module implementation. So you have to use it in any module not in template.php

Use the above statement in module like

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {

  if($form_id == "views_exposed_form"){
    $form['data_2'] = array(
            '#type' => 'date_popup',
            '#date_format' => 'Y-m-d'
        );
    }
}
5
  • Its working, its just adding another field rather than editing it. If i add this as a module, will it automatically edit the field instead?
    – Collins
    May 11, 2014 at 19:42
  • Yeah i used drupal_set_message(print_r($form, TRUE)); to print an array and got the field name from there. It inserts the new datepicker field in between the data_2_op field and its label, instead of just altering it. I'll try it in a module to see if it makes a difference. Thanks.
    – Collins
    May 11, 2014 at 19:50
  • Nope, That didn't work, putting it into a custom module did the exact same thing that it did in the template.php file.
    – Collins
    May 11, 2014 at 20:07
  • Just updated it, its the second field in the exposed filter that Im looking to change.
    – Collins
    May 11, 2014 at 20:15
  • I don't know why, but it has suddenly started working! Im using the exact same code I had to begin with, and I had already flushed the cache a few times. Oh well, at least it works! Thanks for you help
    – Collins
    May 11, 2014 at 20:28

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.