You are here

function core_views_facets_block_view_facet_block_alter in Core Views Facets 8

Implements hook_block_view_alter().

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException Thrown if the entity type doesn't exist.

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException Thrown if the storage handler couldn't be loaded.

File

./core_views_facets.module, line 42
Contains core_views_facets.module.

Code

function core_views_facets_block_view_facet_block_alter(array &$build, BlockPluginInterface $block) {
  $facet_id = str_replace('facet_block' . PluginBase::DERIVATIVE_SEPARATOR, '', $block
    ->getPluginId());

  /** @var \Drupal\facets\FacetInterface $facet */
  $facet = \Drupal::entityTypeManager()
    ->getStorage('facets_facet')
    ->load($facet_id);
  if (!is_subclass_of($facet
    ->getFacetSource(), CoreViewsFacetSourceBase::class)) {
    return;
  }

  /** @var \Drupal\core_views_facets\Plugin\facets\facet_source\CoreViewsFacetSourceBase $facet_source */
  $facet_source = $facet
    ->getFacetSource();

  /** @var \Drupal\views\ViewExecutable $view */
  if ($view = $facet_source
    ->getView()) {
    if ($view->display_handler
      ->ajaxEnabled()) {
      $build['#attached']['library'][] = 'core_views_facets/core_views_facets.views.ajax';
      $build['#attached']['drupalSettings']['core_views_facets'] = [
        $facet_id => $facet_source
          ->getAjaxSettingsByFacet($facet),
      ];
    }
  }
}