3

I've got an exposed filter on my view that allows users to specify a date, and I want to specify a placeholder for this text field.

I've tried doing this using the following code in a hook:

$form['field_start_end_dates_value']['value']['#attributes'] = array('placeholder' => array(t('Search')));

(I know this is in the correct hook as I'm successfully altering other form elements within the same form)

I've var_dumped the form variable, and I'm getting the following output for the date filter:

[field_start_end_dates_value] => Array
        (
            [#tree] => 1
            [value] => Array
                (
                    [#title] => 
                    [#type] => date_text
                    [#size] => 20
                    [#default_value] => 
                    [#date_format] => j M Y
                    [#date_label_position] => within
                    [#date_year_range] => -3:+3
                    [#process] => Array
                        (
                            [0] => date_text_element_process
                        )

                    [#prefix] => 
                    [#suffix] => 
                    [#attributes] => Array
                        (
                            [placeholder] => Array
                                (
                                    [0] => Specify Date
                                )

                        )

                )

        )

This looks right to me, but I'm not seeing the placeholder on the front end.

I've tried flushing the cache multiple times, and loading the page without JavaScript enabled.

What am I doing wrong?

1
3
+50

You are doing it wrong way

Form alter doesn't work here. This is how it can be achieved:

It depends on which date element you are using There are few hooks available

hook_date_text_process_alter()
hook_date_popup_process_alter()
hook_date_combo_process_alter()
hook_date_select_process_alter()

I am showing for hook_date_text_process_alter()

/**
 * Implements hook_date_text_process_alter().
 */
function MYMODULE_date_text_process_alter(&$element, &$form_state, $context) {
  if ($form_state['complete form']['#form_id'] == 'views_exposed_form') {
    //dpm($form_state);
    if ($element['#id'] == 'edit-field-appintment-date-value-value') {
      $element['date']['#attributes']['placeholder'] = t('Demo placeholder');
      //dpm($element);
    }
  }
}

For Other text elements Below is the correct example of adding a placeholder to views_exposed_form to any other text field but date field can't be done like this.

/**
 * Implements hook_form_views_exposed_form_alter().
 */
function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  $form['title']['#attributes'] = array('placeholder' => array(t('Your Placeholder')));
}

Hope this helps

1
  • This worked for me. Was trying FOREVER. I had to both override #default_value to nothing first, then ALSO add a placeholder to #attributes. Just adding the placeholder didn't work because the default_value would show.
    – ProGrammar
    Mar 24 '17 at 18:19
1

Apply the attributes to the element and not its value. The following should work:

$form['field_start_end_dates_value']['#attributes'] = array('placeholder' => array(t('Search')));
3
  • ajit, he was asking for date field man :). See my answer how it an be done Mar 23 '15 at 11:39
  • @HimanshuPathak I think, It can be done via hook_form_alter to any form element. Date text form element is simply a textfield. Mar 26 '15 at 10:39
  • @SumitMadan as per my knowledge, no. Try doing this for any date field. not text field. Mar 26 '15 at 23:58
1

Or In Page load you can hack that text field using JQuery Like below

$("#yourId").attr('placeholder', 'Search');
1

You can check this. Its work fine for me...

function yourmodule_form_alter(&$form, &$form_state, $form_id) {
    if($form_id == 'views_exposed_form' && ($form['#id'] == 'views-exposed-form-test-date-page')) {
           // check for your field. here 'changed' value for 'update field'.
           $form['changed']['#attributes'] = array('placeholder' => array(t('Your Placeholder')));
    }
}
0

Here is what worked for me on Drupal 7 in 2017 (for a popup):

function MYMODULE_date_popup_process_alter(&$element, &$form_state, $context) {
  if ($form_state['complete form']['#form_id'] == 'views_exposed_form') {
    $form_state['complete form']['field_event_date_value2']['value']['#default_value'] = "";
    $form_state['complete form']['field_event_date_value2']['value']['#attributes'] = array('placeholder' => array(t('End Date')));
  }
}

I had to override the #default_value (which, if is not a date, just leaves it blank), and THEN the placeholder will show. I guess it is populating the form with #default_value, thus leaving it without a placeholder.

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.