Newer
Older
Nick Veenhof
committed
<?php
namespace Drupal\facets_summary\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\UncacheableDependencyTrait;
Nick Veenhof
committed
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets_summary\Entity\FacetsSummary;
use Drupal\facets_summary\FacetsSummaryBlockInterface;
use Drupal\facets_summary\FacetsSummaryManager\DefaultFacetsSummaryManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Exposes a summary based on all the facets as a block.
Nick Veenhof
committed
*
* @Block(
* id = "facets_summary_block",
* deriver = "Drupal\facets_summary\Plugin\Block\FacetsSummaryBlockDeriver"
* )
*/
class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterface, ContainerFactoryPluginInterface {
use UncacheableDependencyTrait;
Nick Veenhof
committed
/**
* The facet manager service.
*
* @var \Drupal\facets_summary\FacetsSummaryManager\DefaultFacetsSummaryManager
*/
protected $facetManager;
/**
* The associated facets_source_summary entity.
*
* @var \Drupal\facets_summary\FacetsSummaryInterface
*/
protected $facetsSummary;
/**
* Constructs a source summary block.
*
* @param array $configuration
* The configuration of the Facets Summary Block.
* @param string $plugin_id
* The block plugin block identifier.
* @param array $plugin_definition
* The block plugin block definition.
* @param \Drupal\facets_summary\FacetsSummaryManager\DefaultFacetsSummaryManager $facets_summary_manager
* The facet manager service.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, DefaultFacetsSummaryManager $facets_summary_manager) {
Nick Veenhof
committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->facetsSummaryManager = $facets_summary_manager;
}
/**
* {@inheritdoc}
*/
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')
);
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
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()],
];
}
return $build;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$source_id = $this->getDerivativeId();
if ($summary = FacetsSummary::load($source_id)) {
return [$summary->getConfigDependencyKey() => [$summary->getConfigDependencyName()]];
}
return [];
}
}