You are here

public function CurrentSearchItemActive::execute in Facet API 7

Same name and namespace in other branches
  1. 6.3 contrib/current_search/plugins/current_search/item_active.inc \CurrentSearchItemActive::execute()
  2. 7.2 contrib/current_search/plugins/current_search/item_active.inc \CurrentSearchItemActive::execute()

Implements CurrentSearchItem::execute().

Overrides CurrentSearchItem::execute

File

contrib/current_search/plugins/current_search/item_active.inc, line 22
The active current search item plugin class.

Class

CurrentSearchItemActive
Current search item plugin that displays the active facet items.

Code

public function execute(FacetapiAdapter $adapter) {
  $items = array();

  // Makes sure facet builds are initialized.
  $adapter
    ->processFacets();

  // Initializes links attributes, adds rel="nofollow" if configured.
  $attributes = $this->settings['nofollow'] ? array(
    'rel' => 'nofollow',
  ) : array();
  $attributes += array(
    'class' => array(),
  );

  // Gets the translated pattern with token replacements in tact.
  $pattern = $this
    ->translate('pattern', $this->settings['pattern']);

  // Adds the keywords if any were passed.
  $keys = $adapter
    ->getSearchKeys();
  $keys_link = isset($this->settings['keys_link']) ? $this->settings['keys_link'] : FALSE;
  if ($this->settings['keys'] && !$keys_link && $keys) {
    $items[] = theme('current_search_keys', array(
      'keys' => $keys,
      'adapter' => $adapter,
    ));
  }
  elseif ($this->settings['keys'] && $keys_link && $keys) {
    $active_items = $adapter
      ->getAllActiveItems();
    if (count($active_items) > 0) {
      $item = reset($active_items);
      $item['adapter'] = $adapter;
      if (!isset($item['name'])) {
        $item['name'] = $item['field alias'];
      }
      $item['adapter'] = $adapter;
      $path = $adapter
        ->getUrlProcessor()
        ->getFacetPath($item, $this
        ->getItemValues($item, $adapter), 0);
      $params = $adapter
        ->getUrlProcessor()
        ->getQueryString($item, $this
        ->getItemValues($item, $adapter), 0);
    }
    else {
      $path = $adapter
        ->getSearchPath();
      $params = $adapter
        ->getUrlProcessor()
        ->getParams();
    }

    // Remove the keywords param if one is configured.
    if ($target_param = $this->settings['keys_param']) {
      unset($params[$target_param]);
    }
    elseif (strpos($path, $keys) !== FALSE) {
      $parts = array_filter(explode('/', $path));
      if ($keys == end($parts)) {
        $part = key($parts);
        unset($parts[$part]);
        $path = implode('/', $parts);
      }
    }
    $variables = array(
      'text' => theme('current_search_keys', array(
        'keys' => $keys,
        'adapter' => $adapter,
      )),
      'path' => $path,
      'options' => array(
        'attributes' => $attributes,
        'html' => TRUE,
        'query' => $params,
      ),
    );
    $items[] = theme('current_search_link_active', $variables);
  }

  // Adds active facets to the current search block.
  foreach ($adapter
    ->getAllActiveItems() as $item) {

    // Adds adapter to the active item for token replacement.
    $item['adapter'] = $adapter;

    // Builds variables to pass to theme function.
    $data = array(
      'facetapi_active_item' => $item,
    );
    $variables = array(
      'text' => token_replace($pattern, $data),
      'path' => $this
        ->getFacetPath($item, $adapter),
      'options' => array(
        'attributes' => $attributes,
        'html' => TRUE,
        'query' => $this
          ->getQueryString($item, $adapter),
      ),
    );

    // Renders the active link.
    $row['data'] = theme('current_search_link_active', $variables);
    $row['class'][] = 'active';
    $items[] = $row;
  }

  // If there are items, return the render array.
  if ($items) {
    $classes = $this->settings['css'] ? current_search_get_classes($this->settings['classes']) : array();
    return array(
      '#theme' => 'item_list__facetapi__current_search_item_active',
      '#items' => $items,
      '#attributes' => array(
        'class' => $classes,
      ),
    );
  }
}