You are here

public function FacetapiFacet::build in Facet API 7.2

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

Build the facet's render array for the realm.

Executes the filter plugins to modify the base render array, then passes the filtered array to the widget plugin. The widget plugin is executed to finalize the build if the filtered array contains items. Otherwise the empty behavior plugin is executed to finalize the build.

Parameters

array $realm: The realm definition as returned by facetapi_realm_load().

FacetapiFacetProcessor $processor: The processor object.

Return value

array The facet's render array keyed by the FacetapiWidget::$key property.

File

plugins/facetapi/adapter.inc, line 1271
Adapter plugin and adapter related classes.

Class

FacetapiFacet
Wrapper around the facet definition with methods that build render arrays.

Code

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

  // Get the base render array used as a starting point for the widget.
  $this->build = $processor
    ->getBuild();

  // Execute the filter plugins.
  // @todo Defensive coding here for filters?
  $enabled_filters = array_filter($settings->settings['filters'], 'facetapi_filter_disabled_filters');
  uasort($enabled_filters, 'drupal_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);
    }
  }

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

    // Execute the widget plugin and get the finalized render array.
    $widget_plugin
      ->execute();
    $build = $widget_plugin
      ->getBuild();
  }
  else {

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

    // Execute the 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']]);
  }

  // Add JavaScript settings by merging with the others already set.
  $merge_settings['facetapi']['facets'][] = $widget_plugin
    ->getJavaScriptSettings();
  drupal_add_js($merge_settings, 'setting');

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