You are here

function facetapi_realm_build in Facet API 6

Builds a facet realm, in other words converts the facet values to some normalized value.

Parameters

$searcher: A string containing the machine readable name of the searcher module.

$realm_name: A string containing the machine readable name of the realm.

Return value

The realm's render array.

2 calls to facetapi_realm_build()
facetapi_block_view in ./facetapi.widget.inc
Returns data for the "view" operation of hook_block().
facetapi_form_search_form_alter in ./facetapi.module
Implementation of hook_form_FORM_ID_alter().

File

./facetapi.module, line 890
An abstracted facet API that can be used by various search backens.

Code

function facetapi_realm_build($searcher, $realm_name) {
  $build = array();

  // Loads adapter and realm, adds JavaScript, builds render array.
  if (($adapter = facetapi_adapter_load($searcher)) && ($realm = facetapi_realm_load($realm_name))) {
    drupal_add_js(drupal_get_path('module', 'facetapi') . '/js/facetapi.js');

    // Initializes render array.
    $build['#adapter'] = $adapter;
    $build['#realm'] = $realm;

    // Builds each facet in the realm, merges into realm's render array.
    foreach (facetapi_enabled_facets_get($searcher, $realm['name']) as $facet) {
      $field_alias = $facet['field alias'];
      $facet_build = $adapter
        ->getFacet($facet)
        ->build($realm);

      // Tries to be smart when merging the render arrays. Crazy things happen
      // when merging facets with the same field alias such as taxonomy terms in
      // the fieldset realm. We want to merge only the values.
      foreach (element_children($facet_build) as $child) {
        if (!isset($build[$child])) {
          $build = array_merge_recursive($build, $facet_build);
        }
        else {
          if (isset($build[$child][$field_alias]) && isset($facet_build[$child][$field_alias])) {
            $build[$child][$field_alias] = array_merge_recursive($build[$child][$field_alias], $facet_build[$child][$field_alias]);
          }
          elseif (isset($build[$child]['#options']) && isset($facet_build[$child]['#options'])) {
            $build[$child]['#options'] = array_merge_recursive($build[$child]['#options'], $facet_build[$child]['#options']);
          }
          else {
            $build = array_merge_recursive($build, $facet_build);
          }
        }
      }
    }

    // Allows modules to alter the entire realm's render array.
    drupal_alter('facetapi_facets', $build, $adapter, $realm);
  }
  return $build;
}