You are here

public function FacetsSummaryBlock::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

modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php, line 86

Class

FacetsSummaryBlock
Exposes a summary based on all the facets as a block.

Namespace

Drupal\facets_summary\Plugin\Block

Code

public function build() {

  /** @var \Drupal\facets_summary\FacetsSummaryInterface $summary */
  $facets_summary = $this
    ->getEntity();

  // Let the facet_manager build the facets.
  $build = $this->facetsSummaryManager
    ->build($facets_summary);

  // Add contextual links only when we have results.
  if (!empty($build)) {
    $build['#contextual_links']['facets_summary'] = [
      'route_parameters' => [
        'facets_summary' => $facets_summary
          ->id(),
      ],
    ];
  }

  /** @var \Drupal\views\ViewExecutable $view */
  if ($view = $facets_summary
    ->getFacetSource()
    ->getViewsDisplay()) {
    $build['#attached']['drupalSettings']['facets_views_ajax'] = [
      'facets_summary_ajax' => [
        'facets_summary_id' => $facets_summary
          ->id(),
        'view_id' => $view
          ->id(),
        'current_display_id' => $view->current_display,
        'ajax_path' => Url::fromRoute('views.ajax')
          ->toString(),
      ],
    ];
  }
  return $build;
}