You are here

function mefibs_hide_exposed_form_items in MEFIBS - More exposed forms in blocks 7

Hide form elements that should not show up for the given block id.

Parameters

array $form:

object $view:

string $block_id:

Return value

void

1 call to mefibs_hide_exposed_form_items()
mefibs_form_views_exposed_form_alter in ./mefibs.module
Implements hook_form_FORM_ID_alter().

File

./mefibs.module, line 956
Primarily Drupal hooks and global API functions to manipulate views and to provide an additional block with an exposed filter form.

Code

function mefibs_hide_exposed_form_items(&$form, $view, $block_id) {
  $elements = mefibs_get_expected_items_for_exposed_form_block($view, $block_id);
  $mefibs_options = $view->display_handler
    ->get_option('mefibs');
  $display_id = $view->current_display;
  $display = $view->display[$display_id]->handler;
  $form_keys = array();
  foreach ($form['#info'] as $key => $definition) {
    list($type, $id) = explode('-', $key);
    if ($display
      ->get_handler($type, $id) && in_array($id, $elements[$type])) {

      // This is a non grouped filter
      if (!isset($form['#info']['filter-' . $id]['value'])) {
        $key = $id;
      }
      else {
        $key = $form['#info']['filter-' . $id]['value'];
      }
      $form_keys[] = $key;
      if (isset($definition['operator']) && !empty($definition['operator'])) {
        $form_keys[] = $definition['operator'];
      }
    }
    elseif (isset($elements[$type][$id])) {

      // This is a grouped filter
      $form_keys[] = $form['#info']['filter-' . $id]['value'];
      if (isset($definition['operator']) && !empty($definition['operator'])) {
        $form_keys[] = $definition['operator'];
      }
    }
  }
  if (count($elements['sort']) && in_array($block_id, $mefibs_options[$display_id]['sort_block'])) {
    $form_keys[] = 'sort_by';
    $form_keys[] = 'sort_order';
  }
  if (count($elements['other'])) {
    $form_keys = array_merge($form_keys, $elements['other']);
  }

  // Add available elements to JS settings.
  drupal_add_js(array(
    'mefibs' => array(
      'forms' => array(
        $block_id => array(
          'elements' => $form_keys,
        ),
      ),
    ),
  ), 'setting');

  // Add default form keys that we never want to hide.
  $form_keys = array_merge($form_keys, array(
    'submit',
    'reset',
    'form_build_id',
    'form_id',
    'form_token',
  ));
  $context = array(
    'view' => clone $view,
    'display_id' => $display_id,
    'block_id' => $block_id,
    'type' => 'hide_items',
  );
  drupal_alter('mefibs_elements', $form_keys, $context);

  // Do some magic: hide all other elements.
  $prefix = '<div style="display: none;">';
  $suffix = '</div>';
  mefibs_set_form_property_recursive($form, 'prefix', $prefix, $form_keys);
  mefibs_set_form_property_recursive($form, 'suffix', $suffix, $form_keys);

  // Also hide the labels of hidden filters.
  $mefibs_options = $display
    ->get_option('mefibs');

  // Finally hide the labels of all non-shown filters.
  foreach ($form['#info'] as $key => $info) {
    if (isset($info['value']) && !in_array($info['value'], $form_keys)) {
      $form['#info'][$key]['label'] = '';
      $form['#info'][$key]['description'] = '';
    }
  }
}