Drupal 7 – Customize the exposed filter selection list

Yesterday we have talked about how to use selection list for selecting Entity reference in Views exposed filter.
Drupal 7 – Selection list for Entity reference in Views exposed filter

Now i want to have more control on the selection list. For example, i have a content type called competition and i want to limit the number of options in the Entity reference selection list. In this case, we have to create a custom module and alter the views_exposed_form.

1. Create a custom module and implement the hook_form_FORM_ID_alter().

function <module-name>_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  print '<pre>';
  print_r($form);
  print '</pre>';
}

 

2. Refresh the view page and you should get the content of the $form variable. Here is the part i would like to edit.

[field_competition_target_id_1] => Array
(
  [#type] => select
  [#options] => Array
  (
    [All] => - Any -
    [4] => Competition D
    [3] => Competition C 
    [2] => Competition B 
    [1] => Competition A 
  )

  [#default_value] => All
  [#size] => 
)

 

3. So now we need to modify this array in order to customize the selection list options. We will make use of the EntityFieldQuery and only select those competitions which is in result_released status. here is the finalized hook_form_views_exposed_form_alter().

<?php
function <module-name>_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  // Get the nodes which you want to show in the selection list
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'competition')
    ->propertyCondition('status', 1)
    ->fieldCondition('field_competition_status', 'value', 'result_released', '=');
  $result = $query->execute();
  
  $nodes = NULL;
  if (isset($result['node'])) {
    $nids = array_keys($result['node']);
    $competitions = node_load_multiple($nids);
  }

  if (isset($competitions)) {
    // Replace the existing list
    unset($form['field_competition_target_id_1']['#options']);
    $form['field_competition_target_id_1']['#options']['All'] = '- Any -'; 
    foreach($competitions as $competition) {
      $form['field_competition_target_id_1']['#options'][$competition->nid] = $competition->title;
    }
  } else {
    // Remove all options except '- Any -'
    unset($form['field_competition_target_id_1']['#options']);
    $form['field_competition_target_id_1']['#options']['All'] = '- Any -'; 
  }
}

 

Done =)

Reference:

Advertisement

4 thoughts on “Drupal 7 – Customize the exposed filter selection list”

  1. Very great tutorial !! 🙂

    But is it possible to customize this exposed filter on differents taxonomy page ?

    I mean, if there is a “catalog” with a main vocabulary (all terms -> the same level) and some differents vocabularies exposed as filters on all the pages of this catalog.

    Something like :

    catalog’s term 1
    – vocabulary 1 (exposed as filter)
    — term 3 of vocabulary 1
    — term 5 of vocabulary 1
    — term 7 of vocabulary 1
    – vocabulary 4 (exposed as filter)
    — term 2 of vocabulary 4
    — term 3 of vocabulary 4
    — term 7 of vocabulary 4

    catalog’s term 2
    – vocabulary 1 (exposed as filter)
    — term 1 of vocabulary 1
    — term 2 of vocabulary 1
    — term 4 of vocabulary 1
    – vocabulary 3 (exposed as filter)
    — term 2 of vocabulary 3
    — term 3 of vocabulary 3
    — term 4 of vocabulary 3

    etc

    Like

    1. not very sure about what u mean but if u want to customize the exposed filter, all u need to do it’s alter the selection list in the hook function. i.e.

      <?php
      function <module-name>_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
        // Get the nodes which you want to show in the selection list
        $query = new EntityFieldQuery();
        $query->entityCondition('entity_type', 'node')
          ->entityCondition('bundle', 'competition')
          ->propertyCondition('status', 1)
          ->fieldCondition('field_competition_status', 'value', 'result_released', '=');
        $result = $query->execute();
         
        $nodes = NULL;
        if (isset($result['node'])) {
          $nids = array_keys($result['node']);
          $competitions = node_load_multiple($nids);
        }
       
        if (isset($competitions)) {
          // Replace the existing list
          unset($form['field_competition_target_id_1']['#options']);
          $form['field_competition_target_id_1']['#options']['All'] = '- Any -'; 
          foreach($competitions as $competition) {
            $form['field_competition_target_id_1']['#options'][$competition->nid] = $competition->title;
          }
        } else {
          // Remove all options except '- Any -'
          unset($form['field_competition_target_id_1']['#options']);
          $form['field_competition_target_id_1']['#options']['All'] = '- Any -'; 
        }
      }
      

       

      So instead of selecting nodes from EntityFieldQuery, select the terms u want to have. And of course in your views setting, you have to add the terms id as filter.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.