3 namespace Drupal\ctools\Plugin;
6 * Provides methods for \Drupal\ctools\Plugin\BlockVariantInterface.
8 trait BlockVariantTrait {
13 * @var \Drupal\Core\Block\BlockManager
15 protected $blockManager;
18 * The plugin collection that holds the block plugins.
20 * @var \Drupal\ctools\Plugin\BlockPluginCollection
22 protected $blockPluginCollection;
25 * @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionNames()
27 abstract public function getRegionNames();
30 * @see \Drupal\ctools\Plugin\BlockVariantInterface::getBlock()
32 public function getBlock($block_id) {
33 return $this->getBlockCollection()->get($block_id);
37 * @see \Drupal\ctools\Plugin\BlockVariantInterface::addBlock()
39 public function addBlock(array $configuration) {
40 $configuration['uuid'] = $this->uuidGenerator()->generate();
41 $this->getBlockCollection()->addInstanceId($configuration['uuid'], $configuration);
42 return $configuration['uuid'];
46 * @see \Drupal\ctools\Plugin\BlockVariantInterface::removeBlock()
48 public function removeBlock($block_id) {
49 $this->getBlockCollection()->removeInstanceId($block_id);
54 * @see \Drupal\ctools\Plugin\BlockVariantInterface::updateBlock()
56 public function updateBlock($block_id, array $configuration) {
57 $existing_configuration = $this->getBlock($block_id)->getConfiguration();
58 $this->getBlockCollection()->setInstanceConfiguration($block_id, $configuration + $existing_configuration);
63 * @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionAssignment()
65 public function getRegionAssignment($block_id) {
66 $configuration = $this->getBlock($block_id)->getConfiguration();
67 return isset($configuration['region']) ? $configuration['region'] : NULL;
71 * @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionAssignments()
73 public function getRegionAssignments() {
74 // Build an array of the region names in the right order.
75 $empty = array_fill_keys(array_keys($this->getRegionNames()), []);
76 $full = $this->getBlockCollection()->getAllByRegion();
77 // Merge it with the actual values to maintain the ordering.
78 return array_intersect_key(array_merge($empty, $full), $empty);
82 * @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionName()
84 public function getRegionName($region) {
85 $regions = $this->getRegionNames();
86 return isset($regions[$region]) ? $regions[$region] : '';
90 * Gets the block plugin manager.
92 * @return \Drupal\Core\Block\BlockManager
93 * The block plugin manager.
95 protected function getBlockManager() {
96 if (!$this->blockManager) {
97 $this->blockManager = \Drupal::service('plugin.manager.block');
99 return $this->blockManager;
103 * Returns the block plugins used for this display variant.
105 * @return \Drupal\Core\Block\BlockPluginInterface[]|\Drupal\ctools\Plugin\BlockPluginCollection
106 * An array or collection of configured block plugins.
108 protected function getBlockCollection() {
109 if (!$this->blockPluginCollection) {
110 $this->blockPluginCollection = new BlockPluginCollection($this->getBlockManager(), $this->getBlockConfig());
112 return $this->blockPluginCollection;
116 * Returns the UUID generator.
118 * @return \Drupal\Component\Uuid\UuidInterface
120 abstract protected function uuidGenerator();
123 * Returns the configuration for stored blocks.
126 * An array of block configuration, keyed by the unique block ID.
128 abstract protected function getBlockConfig();