Snippets

Metadrop VIEWS - Alter views exposed filters dynamically

Created by Agustin Gomez Iglesias last modified
<?php

/**
 * Implements hook_form_alter().
 *
 * In this example we will remove "-" character
 * from taxonomy labels on select list exposed filters.
 *
 * @param $form
 * @param $form_state
 * @param $form_id
 */
function MY_MODULE_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  if ($form_state['view']->name == 'MY_MODULE_view_name'
      && $form_state['view']->current_display == 'MY_MODULE_view_current_display') { // Tags taxonomy
    if (!empty($form['field_xx_id'])) {
      foreach ($form['field_xx_id']['#options'] as $key => &$exposed_option) {
        try {
          if (is_object($exposed_option) && isset($exposed_option->option) && is_array($exposed_option->option)) {
            foreach ($exposed_option->option as &$label) {
              $label = str_replace('-', '', $label);
            }
          }
          elseif (is_string($exposed_option)) {
            $exposed_option = str_replace('-', '', $exposed_option);
          }
        }
        catch (Exception $e) {
          watchdog('MY_MODULE', 'Couldn\'t change labels for taxonomy because: %msg', array('%msg' => $e->getMessage()), WATCHDOG_ERROR);
        }
      }
    }
  }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.