public function FacetBlock::build in Facets 8
Builds and returns the renderable array for this block plugin.
If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).
Return value
array A renderable array representing the content of the block.
Overrides BlockPluginInterface::build
See also
\Drupal\block\BlockViewBuilder
File
- src/
Plugin/ Block/ FacetBlock.php, line 72
Class
- FacetBlock
- Exposes a facet rendered as a block.
Namespace
Drupal\facets\Plugin\BlockCode
public function build() {
/** @var \Drupal\facets\FacetInterface $facet */
$facet = $this->facetStorage
->load($this
->getDerivativeId());
// No need to build the facet if it does not need to be visible.
if ($facet
->getOnlyVisibleWhenFacetSourceIsVisible() && (!$facet
->getFacetSource() || !$facet
->getFacetSource()
->isRenderedInCurrentRequest())) {
return [];
}
// Let the facet_manager build the facets.
$build = $this->facetManager
->build($facet);
if (!empty($build)) {
// Add extra elements from facet source, for example, ajax scripts.
// @see Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay
/* @var \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source */
$facet_source = $facet
->getFacetSource();
$build += $facet_source
->buildFacet();
// Add contextual links only when we have results.
$build['#contextual_links']['facets_facet'] = [
'route_parameters' => [
'facets_facet' => $facet
->id(),
],
];
if (!empty($build[0]['#attributes']['class']) && in_array('facet-active', $build[0]['#attributes']['class'], TRUE)) {
$build['#attributes']['class'][] = 'facet-active';
}
else {
$build['#attributes']['class'][] = 'facet-inactive';
}
// Add classes needed for ajax.
if (!empty($build['#use_ajax'])) {
$build['#attributes']['class'][] = 'block-facets-ajax';
// The configuration block id isn't always set in the configuration.
if (isset($this->configuration['block_id'])) {
$build['#attributes']['class'][] = 'js-facet-block-id-' . $this->configuration['block_id'];
}
else {
$build['#attributes']['class'][] = 'js-facet-block-id-' . $this->pluginId;
}
}
}
return $build;
}