You are here

public function FacetapiFacet::build in Facet API 6.3

Same name and namespace in other branches
  1. 6 facetapi.adapter.inc \FacetapiFacet::build()
  2. 7.2 plugins/facetapi/adapter.inc \FacetapiFacet::build()
  3. 7 plugins/facetapi/adapter.inc \FacetapiFacet::build()

Returns the facet's render array.

Parameters

array $realm: An array containing the realm definition.

FacetapiFacetProcessor $processor: The processor object.

Return value

The facet's build array.

File

plugins/facetapi/adapter.inc, line 1027
Adapter plugin and adapter related calsses.

Class

FacetapiFacet
Stores facet data, provides methods that build the facet's render array.

Code

public function build(array $realm, FacetapiFacetProcessor $processor) {
  $settings = $this
    ->getSettings($realm);

  // Gets the base render array from the facet processor.
  $this->build = $processor
    ->getBuild();

  // Executes filter plugins.
  // @todo Defensive coding here for filters?
  $enabled_filters = array_filter($settings->settings['filters'], 'facetapi_filter_disabled_filters');
  uasort($enabled_filters, 'facetapi_sort_weight');
  foreach ($enabled_filters as $filter_id => $filter_settings) {
    if ($class = ctools_plugin_load_class('facetapi', 'filters', $filter_id, 'handler')) {
      $filter_plugin = new $class($filter_id, $this->adapter, $settings);
      $this->build = $filter_plugin
        ->execute($this->build);
    }
    else {
      watchdog('facetapi', 'Filter %name not valid.', array(
        '%name' => $filter_id,
      ), WATCHDOG_ERROR);
    }
  }

  // Instantiates the widget plugin and initializes.
  // @todo Defensive coding here for widgets?
  $widget_name = $settings->settings['widget'];
  if (!($class = ctools_plugin_load_class('facetapi', 'widgets', $widget_name, 'handler'))) {
    watchdog('facetapi', 'Widget %name not valid.', array(
      '%name' => $widget_name,
    ), WATCHDOG_ERROR);
    return array();
  }
  $widget_plugin = new $class($widget_name, $realm, $this, $settings);
  $widget_plugin
    ->init();
  if ($this->build) {

    // Executes widget plugin.
    $widget_plugin
      ->execute();
    $build = $widget_plugin
      ->getBuild();
  }
  else {

    // Instantiates empty behavior plugin.
    $id = $settings->settings['empty_behavior'];
    $class = ctools_plugin_load_class('facetapi', 'empty_behaviors', $id, 'handler');
    $empty_plugin = new $class($settings);

    // Executes empty behavior plugin.
    $build = $widget_plugin
      ->getBuild();
    $build[$this['field alias']] = $empty_plugin
      ->execute();
  }

  // If the element is empty, unset it.
  if (!$build[$this['field alias']]) {
    unset($build[$this['field alias']]);
  }

  // Adds JavaScript settings in a way that merges with others already set.
  $merge_settings['facetapi']['facets'][] = $widget_plugin
    ->getJavaScriptSettings();
  drupal_add_js($merge_settings, 'setting');

  // Returns array keyed by the FacetapiWidget::$key property.
  return array(
    $widget_plugin
      ->getKey() => $build,
  );
}