You are here

public function FacetapiFacet::build in Facet API 6

Same name and namespace in other branches
  1. 6.3 plugins/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

$realm: An array containing the realm definition.

Return value

The facet's build array.

File

./facetapi.adapter.inc, line 387
Defines classes used by the FacetAPI module.

Class

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

Code

public function build(array $realm) {

  // Builds render array for facet items if necessary.
  if (!isset($this->_build)) {
    $this->_build = $this
      ->buildItems();
    $this
      ->processHierarchy($this->_build)
      ->processQueryStrings($this->_build);
  }

  // Gets searcher since we use it a lot, gets field alias for readability.
  $searcher = $this->_adapter
    ->getSearcher();
  $field_alias = $this->_facet['field alias'];

  // Initializes render array.
  $build = array(
    '#title' => $this->_facet['title'],
    '#description' => $this->_facet['description'],
    '#weight' => $this->_facet['weight'],
    '#adapter' => $this->_adapter,
    '#realm_name' => $realm['name'],
    '#facet' => $this->_facet,
    $field_alias => $this->_build,
  );

  // Adds identifiers to facet.
  $build['#attributes'] = array(
    'class' => "facetapi-facet-{$this->_facet['name']}",
    'id' => "facetapi-facet-{$searcher}-{$realm['name']}-{$this->_facet['name']}",
  );

  // Applies sorting algorithms.
  $this->_sorts = facetapi_facet_sorts_get($this->_adapter, $realm, $this->_facet);
  $this
    ->_sort($build[$field_alias]);
  unset($this->_sorts);

  // Gets available widgets.
  $widgets = facetapi_widgets_get(array(
    'realm' => $realm,
    'facet' => $this->_facet,
  ));

  // Gets widget from settings, finds default if necessary.
  $widget_name = facetapi_facet_widget_get($widgets, $searcher, $realm, $this->_facet);

  // Initializes JavaScript settings.
  $facet_settings = array(
    'searcher' => $searcher,
    'type' => $this->_adapter
      ->getType(),
    'realmName' => $realm['name'],
    'facetName' => $this->_facet['name'],
    'widget' => $widget_name,
    'queryType' => $this->_facet['query type'],
  );

  // Passes render array, JavaScript settings to widget.
  $key = $this->_facet['field alias'];
  if (NULL !== $widget_name && isset($widgets[$widget_name])) {
    $build['#widget'] = $widgets[$widget_name];
    if (facetapi_file_include($build['#widget'])) {
      $build['#widget']['callback']($build, $key, $facet_settings);
    }
  }

  // Adds JavaScript settings.
  $settings['facetapi']['facets'][] = $facet_settings;
  drupal_add_js($settings, 'setting');
  return array(
    $key => $build,
  );
}