Newer
Older
namespace Drupal\facets\Plugin\Block;

Hoi Sing Edison Wong
committed
use Drupal\Core\Access\AccessResult;

Markus Kalkbrenner
committed
use Drupal\Core\Cache\Cache;

Markus Kalkbrenner
committed
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityStorageInterface;

Joris Vercammen
committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;

Hoi Sing Edison Wong
committed
use Drupal\Core\Session\AccountInterface;

Markus Kalkbrenner
committed
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetManager\DefaultFacetManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Exposes a facet rendered as a block.

Joris Vercammen
committed
* id = "facet_block",
* )
*/
class FacetBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The facet manager.
* @var \Drupal\facets\FacetManager\DefaultFacetManager
protected $facetManager;
* The entity storage used for facets.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $facetStorage;

Markus Kalkbrenner
committed
/**

Markus Kalkbrenner
committed
* @var \Drupal\facets\FacetInterface
*/
protected $facet;
/**
* Construct a FacetBlock instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param string $plugin_definition
* The plugin implementation definition.
* @param \Drupal\facets\FacetManager\DefaultFacetManager $facet_manager
* The facet manager.
* @param \Drupal\Core\Entity\EntityStorageInterface $facet_storage
* The entity storage used for facets.
public function __construct(array $configuration, $plugin_id, $plugin_definition, DefaultFacetManager $facet_manager, EntityStorageInterface $facet_storage) {
$this->facetStorage = $facet_storage;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('facets.manager'),
$container->get('entity_type.manager')->getStorage('facets_facet')
);
}
/**
* {@inheritdoc}
*/
public function build() {

Tim Plunkett
committed
// Do not build the facet if the block is being previewed.
if ($this->getContextValue('in_preview')) {
return [];
}

Hoi Sing Edison Wong
committed
$facet = $this->getFacet();

Jur de Vries
committed
// Let the facet_manager build the facets.
$facet_build = $this->facetManager->build($facet);
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
if ($facet_build) {
CacheableMetadata::createFromObject($this)->applyTo($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();
$facet_build += $facet_source->buildFacet();
$build = [
'#type' => 'container',
'#contextual_links' => [
'facets_facet' => [
'route_parameters' => ['facets_facet' => $facet->id()],
],
],
'#attributes' => [
'class' => ['block-facet__wrapper'],
],
0 => $facet_build,
];
// Add css classes.
if (!empty($facet_build[0]['#attributes']['class'])) {
$css_classes = $facet_build[0]['#attributes']['class'];
// Active/inactive css classes.
if (in_array('facet-active', $css_classes)) {
$build['#attributes']['class'][] = 'facet-active';

Joris Vercammen
committed
}
else {
$build['#attributes']['class'][] = 'facet-inactive';

Joris Vercammen
committed
}
// Whether it is necessary to add hide css class.
if (in_array('facet-hidden', $css_classes)) {
$build['#attributes']['class'][] = 'hidden';
}
}
// Add classes needed for ajax.
if (!empty($facet_build['#use_ajax'])) {
$build['#attributes']['class'][] = 'block-facets-ajax';
$block_id = str_replace(':', '--', $this->pluginId);
$block_id = Html::cleanCssIdentifier($block_id);
$build['#attributes']['class'][] = 'js-facet-block-id-' . $block_id;
$build['#attributes']['id'] = Html::getUniqueId($block_id);
}
// To render correctly in different situations.
$build = [
'facet_block' => $build,
];

Hoi Sing Edison Wong
committed
/**

Markus Kalkbrenner
committed
* Get facet entity.

Hoi Sing Edison Wong
committed
*
* @return \Drupal\facets\FacetInterface
* The facet entity.
*/

Markus Kalkbrenner
committed
protected function getFacet(): FacetInterface {
if (!$this->facet) {
$this->facet = $this->facetStorage->load($this->getDerivativeId());
}
return $this->facet;

Hoi Sing Edison Wong
committed
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {

Markus Kalkbrenner
committed
return Cache::mergeTags(parent::getCacheTags(), $this->getFacet()->getCacheTags());

Hoi Sing Edison Wong
committed
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {

Markus Kalkbrenner
committed
return Cache::mergeContexts(parent::getCacheContexts(), $this->getFacet()->getCacheContexts());

Hoi Sing Edison Wong
committed
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {

Markus Kalkbrenner
committed
return Cache::mergeMaxAges(parent::getCacheMaxAge(), $this->getFacet()->getCacheMaxAge());

Joris Vercammen
committed
/**
* {@inheritdoc}
*/
public function calculateDependencies() {

Hoi Sing Edison Wong
committed
return ['config' => [$this->getFacet()->getConfigDependencyName()]];

Joris Vercammen
committed
}

Joris Vercammen
committed
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {

Mike Madison
committed
// Checks for a valid form id. Panelizer does not generate one.
if (isset($form['id']['#value'])) {
// Save block id to configuration, we do this for loading the original
// block with ajax.

Mike Madison
committed
$block_id = $form['id']['#value'];
$this->configuration['block_id'] = $block_id;
}

Joris Vercammen
committed
}

Tim Plunkett
committed
/**
* {@inheritdoc}
*/
public function getPreviewFallbackString() {
return $this->t('Placeholder for the "@facet" facet', ['@facet' => $this->getDerivativeId()]);
}

Hoi Sing Edison Wong
committed
/**
* {@inheritDoc}
*
* Allow to render facet block if one of the following conditions are met:
* - facet is allowed to be displayed regardless of the source visibility
* - facet source is rendered in the same request as facet.
*/
public function blockAccess(AccountInterface $account) {
$facet = $this->getFacet();
return AccessResult::allowedIf(
!$facet->getOnlyVisibleWhenFacetSourceIsVisible()
|| ($facet->getFacetSource() && $facet->getFacetSource()->isRenderedInCurrentRequest())
)->addCacheableDependency($facet);
}