I have a project where our design has input fields with specialized default placeholder text--'Select your grade', 'Select subject(s)', etc. It would be great if these fields could be set up to have placeholder text that differs from the configured settings.

Comments

Dave Reid’s picture

Version: 7.x-2.0-beta4 » 7.x-2.x-dev

I was going to recommend that we support existing solutions like https://drupal.org/project/field_placeholder but then I came across https://github.com/harvesthq/chosen/pull/1342 which means we'd need to add support specifically for data-placeholder ourselves in field widgets.

lslinnet’s picture

Just to repeat what they said in the linked issue and give an example for use here:

Add an attribute called data-placeholder to the select box with the placeholder text.
Example:

  $form['time'] = array(
    '#type' => 'select',
    '#title' => t('What time in the day?'),
    '#options' => array(
      '06:00-09:00' => t('Morning (6:00-9:00)'),
      '09:00-12:00' => t('Mid-morning (9:00-12:00)'),
      '12:00-17:00' => t('Afternoon (12:00-17:00)'),
      '17:00-21:00' => t('Evening (17:00-21:00)')
    ),
    '#multiple' => TRUE,
    '#attributes' => array(
      'data-placeholder' => t('What time in the day?'),
    ),
    '#default_value' => $default_values['time'],
  );

Perhaps adding a link to the related chosen document or give an example of how to do it within the readme?
Link to chosen documentation: http://harvesthq.github.io/chosen/#default-text-support

capfive’s picture

went looking for this feature this morning, it's a must have becuase usually the chosen interface is used in an anonymous/authenticated user setting to give better UX.

I found this https://www.drupal.org/project/field_placeholder and i wonder if someone would be able to have it apply to the chosen setup?

At the moment it just targets CCK text fields, Long text & Long text with summary, or could someone utilize the code to put it into the chosen setup?

This is a phenomenal module and really polishes off sites when it is used properly, would be great to have this feature, as at the moment I am overriding each field to put a placeholder in, and when you have more than 3 fields per content type, it's A LOT of overrides to get it to look perfect.

+1 for this feature :)

FluxSauce’s picture

Version: 7.x-2.x-dev » 8.x-2.x-dev
Component: User interface » Documentation

Looks like I wasn't the only one with this need. I'd say this is documentation, as this works:

// Use Chosen.
foreach ([
  'FIELDNAME',
] as $chosen) {
  // Enable Chosen.
  $form[$chosen]['#chosen'] = TRUE;
  // Set placeholder.
  $form[$chosen]['#attributes'] = [
    'data-placeholder' => $form['#info']['filter-' . $key]['label'],
  ];
  // Hide title.
  $form[$chosen]['#title_display'] = 'invisible';
}
jonathanshaw’s picture

Component: Documentation » User interface

Ideally this would be exposed in the widget settings UI, as some other widgets do.

cdmo’s picture

Just another example of #2, this time applied in a hook_form_alter (in my case to an exposed view filter):

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] == 'views-exposed-form-blah-page') {
    $form['field_to_set_placeholder_for']['#attributes']['data-placeholder'] = t('Choose dinosaur(s)');
  }
}
serundeputy’s picture

Not sure if the examples in #6 and #4 are for D7 or D8, but in D8 they are having no effect on the placeholder text for me.

In my case the field is an Unlimited number of values select list.

cdmo’s picture

#6 is for D7

chucksimply’s picture

Finally worked out a D8 solution. Hope this helps some.

function MYMODULE_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if($form_id === 'MYFORMID') {
    $form['MYFIELD']['widget']['#attributes']['data-placeholder'] = t('This is placeholder text'); //Multi value
    $form['MYFIELD']['widget']['#options']['_none'] = t('Custom Select Text'); //Single value
  }
}
texas-bronius’s picture

Thanks ChuckSimply. I'll just mention this is true of custom select elements (ie. no hook_form_alter required) like:

  ...
  '#attributes' => [
    'data-placeholder' => $this->t('Library (or leave blank for "All")'),
  ],
  ...

Terrific! One step cooler would be for Chosen module to adjust field widget settings and expose a per-field placeholder text field just like autocomplete entity reference has.