You are here

class FacetapiFacet in Facet API 7

Same name and namespace in other branches
  1. 6.3 plugins/facetapi/adapter.inc \FacetapiFacet
  2. 6 facetapi.adapter.inc \FacetapiFacet
  3. 7.2 plugins/facetapi/adapter.inc \FacetapiFacet

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

This class contain methods that assist in render array generation and stores additional context about how and what generated the render arrays for consumption by the widget plugins. Objects can also be used as if they are the facet definitions returned by facetapi_facet_load().

Hierarchy

Expanded class hierarchy of FacetapiFacet

File

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

View source
class FacetapiFacet implements ArrayAccess {

  /**
   * The FacetapiAdapter object this class was instantiated from.
   *
   * @var FacetapiAdapter
   */
  protected $adapter;

  /**
   * The facet definition as returned by facetapi_facet_load().
   *
   * This is the array acted on by the ArrayAccess interface methods so the
   * object can be used as if it were the facet definition array.
   *
   * @var array
   */
  protected $facet;

  /**
   * The base render array used as a starting point for rendering.
   *
   * @var array
   */
  protected $build = array();

  /**
   * Constructs a FacetapiAdapter object.
   *
   * Sets the adapter and facet definitions.
   *
   * @param FacetapiAdapter $adapter
   *   he FacetapiAdapter object this class was instantiated from.
   * @param array $facet
   *   The facet definition as returned by facetapi_facet_load().
   */
  public function __construct(FacetapiAdapter $adapter, array $facet) {
    $this->adapter = $adapter;
    $this->facet = $facet;
  }

  /**
   * Implements ArrayAccess::offsetExists().
   */
  public function offsetExists($offset) {
    return isset($this->facet[$offset]);
  }

  /**
   * Implements ArrayAccess::offsetGet().
   */
  public function offsetGet($offset) {
    return isset($this->facet[$offset]) ? $this->facet[$offset] : NULL;
  }

  /**
   * Implements ArrayAccess::offsetSet().
   */
  public function offsetSet($offset, $value) {
    if (NULL === $offset) {
      $this->facet[] = $value;
    }
    else {
      $this->facet[$offset] = $value;
    }
  }

  /**
   * Implements ArrayAccess::offsetUnset().
   */
  public function offsetUnset($offset) {
    unset($this->facet[$offset]);
  }

  /**
   * Returns the FacetapiAdapter object this class was instantiated from.
   *
   * @return FacetapiAdapter
   *   The adapter object.
   */
  public function getAdapter() {
    return $this->adapter;
  }

  /**
   * Returns the facet definition as returned by facetapi_facet_load().
   *
   * @return array
   *   An array containing the facet definition.
   */
  public function getFacet() {
    return $this->facet;
  }

  /**
   * Returns the base render array used as a starting point for rendering.
   *
   * @return array
   *   The base render array.
   */
  public function getBuild() {
    return $this->build;
  }

  /**
   * Returns realm specific or global settings for a facet.
   *
   * @param string|array $realm
   *   The machine readable name of the realm or an array containing the realm
   *   definition. Pass NULL to return the facet's global settings.
   *
   * @return
   *   An object containing the settings.
   *
   * @see FacetapiAdapter::getFacetSettings()
   * @see FacetapiAdapter::getFacetSettingsGlobal()
   */
  public function getSettings($realm = NULL) {
    if ($realm && !is_array($realm)) {
      $realm = facetapi_realm_load($realm);
    }
    $method = $realm ? 'getFacetSettings' : 'getFacetSettingsGlobal';
    return $this->adapter
      ->{$method}($this->facet, $realm);
  }

  /**
   * 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.
   *
   * @param array $realm
   *   The realm definition as returned by facetapi_realm_load().
   * @param FacetapiFacetProcessor $processor
   *   The processor object.
   *
   * @return array
   *   The facet's render array keyed by the FacetapiWidget::$key property.
   */
  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'];
      if ($class = ctools_plugin_load_class('facetapi', 'empty_behaviors', $id, 'handler')) {
        $empty_plugin = new $class($settings);
      }
      else {
        $class = ctools_plugin_load_class('facetapi', 'empty_behaviors', 'none', '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();
    if (!empty($build[$this['field alias']])) {
      $build[$this['field alias']]['#attached']['js'][] = array(
        'data' => $merge_settings,
        'type' => 'setting',
      );
    }

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

}

Members

Namesort descending Modifiers Type Description Overrides
FacetapiFacet::$adapter protected property The FacetapiAdapter object this class was instantiated from.
FacetapiFacet::$build protected property The base render array used as a starting point for rendering.
FacetapiFacet::$facet protected property The facet definition as returned by facetapi_facet_load().
FacetapiFacet::build public function Build the facet's render array for the realm.
FacetapiFacet::getAdapter public function Returns the FacetapiAdapter object this class was instantiated from.
FacetapiFacet::getBuild public function Returns the base render array used as a starting point for rendering.
FacetapiFacet::getFacet public function Returns the facet definition as returned by facetapi_facet_load().
FacetapiFacet::getSettings public function Returns realm specific or global settings for a facet.
FacetapiFacet::offsetExists public function Implements ArrayAccess::offsetExists().
FacetapiFacet::offsetGet public function Implements ArrayAccess::offsetGet().
FacetapiFacet::offsetSet public function Implements ArrayAccess::offsetSet().
FacetapiFacet::offsetUnset public function Implements ArrayAccess::offsetUnset().
FacetapiFacet::__construct public function Constructs a FacetapiAdapter object.