Nov 28, 2019 in Drupal 8, Snippets

The chosen widget is my default option for select widget. This snippet shows you how to add the placeholder in a form alter.

/**
 * @param $form
 * @param FormStateInterface $form_state
 * @param $form_id
 * Change placeholder value for chosen
 */
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if($form['#id'] === 'MYFORMID') {
    $form['MYFIELD']['#attributes']['data-placeholder'] = t('Select an option');
  }
}