You are here

protected function FacetsBlock::buildFacets in Facets Block 8

Builds facets.

Parameters

array $facets_to_include: A list of facets to display.

Return value

array An array of facets.

Throws

\Drupal\Component\Plugin\Exception\PluginException

1 call to FacetsBlock::buildFacets()
FacetsBlock::build in src/Plugin/Block/FacetsBlock.php
Builds and returns the renderable array for this block plugin.

File

src/Plugin/Block/FacetsBlock.php, line 225

Class

FacetsBlock
Provides a 'Facets Block' block.

Namespace

Drupal\facets_block\Plugin\Block

Code

protected function buildFacets(array $facets_to_include) {
  $facets = [];
  $available_facets = $this
    ->getAvailableFacets();
  foreach ($available_facets as $plugin_id => $facet_title) {
    if (isset($facets_to_include[$plugin_id]) && $facets_to_include[$plugin_id] === $plugin_id) {
      $block_plugin = $this->pluginManagerBlock
        ->createInstance($plugin_id, []);
      if ($block_plugin && $block_plugin
        ->access($this->currentUser)) {
        $build = $block_plugin
          ->build();
        $exclude_empty_facets = !isset($this->configuration['exclude_empty_facets']) ? TRUE : $this->configuration['exclude_empty_facets'];

        // Skip empty facets.
        $is_empty = FALSE;
        if (!$build) {
          $is_empty = TRUE;
        }
        elseif (isset($build[0]['#attributes']['class']) && in_array('facet-empty', $build[0]['#attributes']['class'])) {
          $is_empty = TRUE;
        }
        elseif (isset($build['#items']) && count($build['#items']) == 0) {
          $is_empty = TRUE;
        }
        if ($exclude_empty_facets && $is_empty) {
          continue;
        }
        if (empty($build['#attributes'])) {
          $build['#attributes'] = [];
        }
        $facets[] = [
          'title' => $facet_title,
          'content' => $build,
          'attributes' => new Attribute($build['#attributes']),
        ];
      }
    }
  }
  return $facets;
}