You are here

function views_advanced_labels_form_views_exposed_form_alter in Views Advanced Labels 7

Implements hook_form_FORM_ID_alter() for views_exposed_form().

File

./views_advanced_labels.module, line 223
Lets users configure the placeholders or "- Any -" options for Views filters.

Code

function views_advanced_labels_form_views_exposed_form_alter(&$form, &$form_state) {
  $labels = views_advanced_labels_get_option($form_state['view'], 'select_labels', 'filters');
  if (empty($form['#info'])) {
    return;
  }
  foreach ($form['#info'] as $key => $info) {
    list($type, $id) = explode('-', $key, 2);
    if ($type != 'filter' || empty($labels[$id]) || empty($info['value']) || !isset($form[$info['value']])) {
      continue;
    }
    $element =& $form[$info['value']];

    // Make sure this is an actual form element. Otherwise, try the nested
    // "value" element – or skip this key.
    if (!isset($element['#type'])) {
      if (isset($element['value']['#type'])) {
        $element =& $element['value'];
      }
      else {
        continue;
      }
    }

    // Add the "placeholder" attribute and also the special "data-placeholder"
    // attribute used by Chosen. Also, if there are options and they include an
    // "- Any -" option, change that option's label to ours.
    $element['#attributes']['placeholder'] = $labels[$id];
    $element['#attributes']['data-placeholder'] = $labels[$id];
    if (!empty($element['#options']) && ($i = array_search(t('- Any -'), $element['#options'])) !== FALSE) {
      $element['#options'][$i] = $labels[$id];
    }
  }
}