Adding placeholder to exposed sort in a view

Created on 21 November 2024, about 6 hours ago

I have a view with 2 exposed sorts:- Relevance, and Title. After selecting 'select2' option for sort, from the better exposed filter settings, I am not able to add a placeholder to the dropdown. When I view the widget, 'Relevance' is selected by default and gets rendered as the placeholder.

I also tried adding a placeholder using form alter but that didn't work

function my module_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
     // All of these are not working.
     $form['sort_by']['#select2']['placeholder'] = t('Placeholder');
     $form['sort_by'] ['#empty_option'] = t('Placeholder');
     $form['sort_by']['#placeholder'] = t('Placeholder');
}
🐛 Bug report
Status

Active

Version

1.0

Component

Field widgets

Created by

🇮🇳India Akhil Babu Chengannur

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Akhil Babu
  • 🇮🇳India Akhil Babu Chengannur

    I think it's not possible to add placeholder to the sort widget as view applies the first sort option by default.

    Also, it is not possible to add a placeholder to select2 facets from the UI. I added it in the following way

     */
    function my_modul_preprocess_select(array &$variables) {
        $facet_id = $variables['attributes']['data-drupal-facet-id'];
        if (isset($variables['attributes']['data-select2-config'])) {
          $select2_config = Json::decode($variables['attributes']['data-select2-config']);
          $select2_config['placeholder']['text'] = t('New placeholder')
          $variables['attributes']['data-select2-config'] = Json::encode($select2_config);
        }
     
    }
    
    
Production build 0.71.5 2024