You are here

public function CurrentSearchGroup::execute in Facet API 7

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

Implements CurrentSearchItem::execute().

Overrides CurrentSearchItem::execute

File

contrib/current_search/plugins/current_search/item_group.inc, line 19
The group current search item plugin class.

Class

CurrentSearchGroup
Current search item plugin that displays the active facet items in a group.

Code

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

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

  // Adds other current search module's CSS.
  $path = drupal_get_path('module', 'current_search');
  drupal_add_css($path . '/current_search.css');

  // Adds active facets to the current search block.
  $searcher = $adapter
    ->getSearcher();
  foreach ($adapter
    ->getAllActiveItems() as $item) {
    $facet_name = $item['facets'][0];
    $facet_value = $item['value'];
    $groups[$facet_name][$facet_value] = $item;
  }

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

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

  // Iterates over groups, builds list.
  $build = array();
  foreach ($groups as $facet_name => $group) {
    $items = array();

    // Builds list items.
    foreach ($group as $item) {
      $markup = $adapter
        ->getMappedValue($item['facets'][0], $item['value']);
      $text = empty($markup['#html']) ? check_plain($markup['#markup']) : $markup['#markup'];
      $variables = array(
        'text' => $text,
        'path' => $this
          ->getFacetPath($item, $adapter),
        'options' => array(
          'attributes' => $attributes,
          'html' => TRUE,
          'query' => $this
            ->getQueryString($item, $adapter),
        ),
      );
      $row['data'] = theme('current_search_link_active', $variables);
      $row['class'][] = 'active';
      $items[] = $row;
    }

    // If there are items, add the render array.
    if ($items) {
      $build[$facet_name]['#theme_wrappers'] = array(
        'current_search_group_wrapper',
      );
      $build[$facet_name]['#id'] = drupal_html_id("current-search-group-{$searcher}-{$this->configName}-{$facet_name}");
      $build[$facet_name]['#facet_name'] = $facet_name;

      // Performs token replacemenets and themes the group title.
      $data = array(
        'facetapi_facet' => facetapi_facet_load($facet_name, $searcher),
      );
      $title = filter_xss(token_replace($field_pattern, $data));
      $build[$facet_name]['title']['#markup'] = theme('current_search_group_title', array(
        'title' => $title,
      ));

      // Builds the list.
      $build[$facet_name]['list'] = array(
        '#theme' => 'item_list__facetapi__current_search_group',
        '#items' => $items,
        '#attributes' => array(
          'class' => array(
            'inline',
          ),
        ),
      );
    }
  }
  return $build;
}