FacetsSummaryBlock.php in Facets 8
File
modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php
View source
<?php
namespace Drupal\facets_summary\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\UncacheableDependencyTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\facets_summary\Entity\FacetsSummary;
use Drupal\facets_summary\FacetsSummaryBlockInterface;
use Drupal\facets_summary\FacetsSummaryManager\DefaultFacetsSummaryManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterface, ContainerFactoryPluginInterface {
use UncacheableDependencyTrait;
protected $facetsSummaryManager;
protected $facetsSummary;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, DefaultFacetsSummaryManager $facets_summary_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->facetsSummaryManager = $facets_summary_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('facets_summary.manager'));
}
public function getEntity() {
if (!isset($this->facetsSummary)) {
$source_id = $this
->getDerivativeId();
if (!($this->facetsSummary = FacetsSummary::load($source_id))) {
$this->facetsSummary = FacetsSummary::create([
'id' => $source_id,
]);
$this->facetsSummary
->save();
}
}
return $this->facetsSummary;
}
public function build() {
$facets_summary = $this
->getEntity();
$build = $this->facetsSummaryManager
->build($facets_summary);
if (!empty($build)) {
$build['#contextual_links']['facets_summary'] = [
'route_parameters' => [
'facets_summary' => $facets_summary
->id(),
],
];
}
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;
}
public function calculateDependencies() {
$source_id = $this
->getDerivativeId();
if ($summary = FacetsSummary::load($source_id)) {
return [
$summary
->getConfigDependencyKey() => [
$summary
->getConfigDependencyName(),
],
];
}
return [];
}
}