fa0187073dea75f89973bab1e9049302e0f1bcd5
[yaffs-website] / Plugin / BlockVariantTrait.php
1 <?php
2
3 namespace Drupal\ctools\Plugin;
4
5 /**
6  * Provides methods for \Drupal\ctools\Plugin\BlockVariantInterface.
7  */
8 trait BlockVariantTrait {
9
10   /**
11    * The block manager.
12    *
13    * @var \Drupal\Core\Block\BlockManager
14    */
15   protected $blockManager;
16
17   /**
18    * The plugin collection that holds the block plugins.
19    *
20    * @var \Drupal\ctools\Plugin\BlockPluginCollection
21    */
22   protected $blockPluginCollection;
23
24   /**
25    * @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionNames()
26    */
27   abstract public function getRegionNames();
28
29   /**
30    * @see \Drupal\ctools\Plugin\BlockVariantInterface::getBlock()
31    */
32   public function getBlock($block_id) {
33     return $this->getBlockCollection()->get($block_id);
34   }
35
36   /**
37    * @see \Drupal\ctools\Plugin\BlockVariantInterface::addBlock()
38    */
39   public function addBlock(array $configuration) {
40     $configuration['uuid'] = $this->uuidGenerator()->generate();
41     $this->getBlockCollection()->addInstanceId($configuration['uuid'], $configuration);
42     return $configuration['uuid'];
43   }
44
45   /**
46    * @see \Drupal\ctools\Plugin\BlockVariantInterface::removeBlock()
47    */
48   public function removeBlock($block_id) {
49     $this->getBlockCollection()->removeInstanceId($block_id);
50     return $this;
51   }
52
53   /**
54    * @see \Drupal\ctools\Plugin\BlockVariantInterface::updateBlock()
55    */
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);
59     return $this;
60   }
61
62   /**
63    * @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionAssignment()
64    */
65   public function getRegionAssignment($block_id) {
66     $configuration = $this->getBlock($block_id)->getConfiguration();
67     return isset($configuration['region']) ? $configuration['region'] : NULL;
68   }
69
70   /**
71    * @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionAssignments()
72    */
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);
79   }
80
81   /**
82    * @see \Drupal\ctools\Plugin\BlockVariantInterface::getRegionName()
83    */
84   public function getRegionName($region) {
85     $regions = $this->getRegionNames();
86     return isset($regions[$region]) ? $regions[$region] : '';
87   }
88
89   /**
90    * Gets the block plugin manager.
91    *
92    * @return \Drupal\Core\Block\BlockManager
93    *   The block plugin manager.
94    */
95   protected function getBlockManager() {
96     if (!$this->blockManager) {
97       $this->blockManager = \Drupal::service('plugin.manager.block');
98     }
99     return $this->blockManager;
100   }
101
102   /**
103    * Returns the block plugins used for this display variant.
104    *
105    * @return \Drupal\Core\Block\BlockPluginInterface[]|\Drupal\ctools\Plugin\BlockPluginCollection
106    *   An array or collection of configured block plugins.
107    */
108   protected function getBlockCollection() {
109     if (!$this->blockPluginCollection) {
110       $this->blockPluginCollection = new BlockPluginCollection($this->getBlockManager(), $this->getBlockConfig());
111     }
112     return $this->blockPluginCollection;
113   }
114
115   /**
116    * Returns the UUID generator.
117    *
118    * @return \Drupal\Component\Uuid\UuidInterface
119    */
120   abstract protected function uuidGenerator();
121
122   /**
123    * Returns the configuration for stored blocks.
124    *
125    * @return array
126    *   An array of block configuration, keyed by the unique block ID.
127    */
128   abstract protected function getBlockConfig();
129
130 }