You are here

function entityreference_filter_update_callback in Views Reference Filter 7

Ajax callback returning ajax commands to update dependent filters.

1 string reference to 'entityreference_filter_update_callback'
entityreference_filter_menu in ./entityreference_filter.module
Implements hook_menu().

File

./entityreference_filter.views.inc, line 72
Views Reference Filter views integration.

Code

function entityreference_filter_update_callback($view_name, $display_name, $controlling_filter) {
  $result = array(
    '#type' => 'ajax',
    '#commands' => array(),
  );
  if (empty($_REQUEST['entityreference_filter_form_id'])) {
    return $result;
  }

  // Check that the view is valid and the display still exists.

  /** @var view $view */
  $view = views_get_view($view_name);
  if (!$view || !isset($view->display[$display_name]) || !$view
    ->access($display_name)) {
    watchdog('entityreference', 'The view %view_name is no longer eligible for the filter.', array(
      '%view_name' => $view_name,
    ), WATCHDOG_WARNING);
    return $result;
  }
  $view
    ->set_display($display_name);
  if (!$view
    ->access(array(
    $display_name,
  ))) {
    return $result;
  }
  if (!empty($_REQUEST['entityreference_filter_args'])) {

    // Use the initial arguments of this view.
    $view
      ->set_arguments($_REQUEST['entityreference_filter_args']);
  }
  $view
    ->build();
  $filter_names = array();
  foreach ($view->filter as $filter_handler) {
    if (empty($filter_handler->options['exposed'])) {
      continue;
    }
    $filter_names[$filter_handler->options['expose']['identifier']] = TRUE;
  }
  $filters_to_update = array();
  foreach ($view->filter as $filter_label => $filter_handler) {
    if (!$filter_handler instanceof entityreference_filter_view_result) {
      continue;
    }
    if (empty($filter_handler->options['exposed'])) {
      continue;
    }
    if (!$filter_handler
      ->dynamic_filter_depends_on($controlling_filter)) {
      continue;
    }
    $filters_to_update[$filter_label] = $filter_handler;
  }
  foreach ($filters_to_update as $filter_label => $filter_handler) {
    $option_str = '';
    $options = $filter_handler
      ->get_value_options();
    foreach ($options as $key => $choice) {
      $key = (string) $key;
      $selected = '';
      $option_str .= '<option value="' . strip_tags($key) . '"' . $selected . '>' . strip_tags($choice) . '</option>';
    }
    if (empty($filter_handler->always_required) && empty($filter_handler->options['expose']['required']) && empty($filter_handler->options['expose']['multiple'])) {
      $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -');

      // Get 'Any' option label defined in Better Exposed Filters.
      $exposed_form = $filter_handler->view->display_handler
        ->get_plugin('exposed_form');
      if (method_exists($exposed_form, '_bef_get_settings')) {
        $bef_settings = $exposed_form
          ->_bef_get_settings();
        if (!empty($bef_settings[$filter_label]['more_options']['any_label'])) {
          $any_label = $bef_settings[$filter_label]['more_options']['any_label'];
        }
      }
      $option_str = '<option value="All">' . check_plain($any_label) . '</option>' . $option_str;
    }
    $filter_name = $filter_handler->options['expose']['identifier'];
    $form_id = $_REQUEST['entityreference_filter_form_id'];
    $selector = '#' . $form_id . ' [name="' . $filter_name . '"],#' . $form_id . ' [name="' . $filter_name . '[]"]';
    $command = ajax_command_html($selector, $option_str);
    $command['command'] = 'entityreference_filter_insertnowrap';
    $result['#commands'][] = $command;

    // If chosen is applied, it can't be updated by attachBehavior().
    $result['#commands'][] = ajax_command_invoke($selector, 'trigger', array(
      'liszt:updated',
    ));
    $result['#commands'][] = ajax_command_invoke($selector, 'trigger', array(
      'chosen:updated',
    ));

    // Options are changed, so run 'change' handlers.
    $result['#commands'][] = ajax_command_invoke($selector, 'trigger', array(
      'change',
    ));
  }
  return $result;
}