a0bbd25c889bad2953d2540c6531fedb2c864253
[yaffs-website] / web / core / modules / block / src / Entity / Block.php
1 <?php
2
3 namespace Drupal\block\Entity;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Condition\ConditionPluginCollection;
7 use Drupal\Core\Config\Entity\ConfigEntityBase;
8 use Drupal\block\BlockPluginCollection;
9 use Drupal\block\BlockInterface;
10 use Drupal\Core\Config\Entity\ConfigEntityInterface;
11 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
12 use Drupal\Core\Entity\EntityStorageInterface;
13
14 /**
15  * Defines a Block configuration entity class.
16  *
17  * @ConfigEntityType(
18  *   id = "block",
19  *   label = @Translation("Block"),
20  *   handlers = {
21  *     "access" = "Drupal\block\BlockAccessControlHandler",
22  *     "view_builder" = "Drupal\block\BlockViewBuilder",
23  *     "list_builder" = "Drupal\block\BlockListBuilder",
24  *     "form" = {
25  *       "default" = "Drupal\block\BlockForm",
26  *       "delete" = "Drupal\block\Form\BlockDeleteForm"
27  *     }
28  *   },
29  *   admin_permission = "administer blocks",
30  *   entity_keys = {
31  *     "id" = "id",
32  *     "status" = "status"
33  *   },
34  *   links = {
35  *     "delete-form" = "/admin/structure/block/manage/{block}/delete",
36  *     "edit-form" = "/admin/structure/block/manage/{block}",
37  *     "enable" = "/admin/structure/block/manage/{block}/enable",
38  *     "disable" = "/admin/structure/block/manage/{block}/disable",
39  *   },
40  *   config_export = {
41  *     "id",
42  *     "theme",
43  *     "region",
44  *     "weight",
45  *     "provider",
46  *     "plugin",
47  *     "settings",
48  *     "visibility",
49  *   },
50  *   lookup_keys = {
51  *     "theme"
52  *   }
53  * )
54  */
55 class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginCollectionInterface {
56
57   /**
58    * The ID of the block.
59    *
60    * @var string
61    */
62   protected $id;
63
64   /**
65    * The plugin instance settings.
66    *
67    * @var array
68    */
69   protected $settings = [];
70
71   /**
72    * The region this block is placed in.
73    *
74    * @var string
75    */
76   protected $region;
77
78   /**
79    * The block weight.
80    *
81    * @var int
82    */
83   protected $weight;
84
85   /**
86    * The plugin instance ID.
87    *
88    * @var string
89    */
90   protected $plugin;
91
92   /**
93    * The visibility settings for this block.
94    *
95    * @var array
96    */
97   protected $visibility = [];
98
99   /**
100    * The plugin collection that holds the block plugin for this entity.
101    *
102    * @var \Drupal\block\BlockPluginCollection
103    */
104   protected $pluginCollection;
105
106   /**
107    * The available contexts for this block and its visibility conditions.
108    *
109    * @var array
110    */
111   protected $contexts = [];
112
113   /**
114    * The visibility collection.
115    *
116    * @var \Drupal\Core\Condition\ConditionPluginCollection
117    */
118   protected $visibilityCollection;
119
120   /**
121    * The condition plugin manager.
122    *
123    * @var \Drupal\Core\Executable\ExecutableManagerInterface
124    */
125   protected $conditionPluginManager;
126
127   /**
128    * The theme that includes the block plugin for this entity.
129    *
130    * @var string
131    */
132   protected $theme;
133
134   /**
135    * {@inheritdoc}
136    */
137   public function getPlugin() {
138     return $this->getPluginCollection()->get($this->plugin);
139   }
140
141   /**
142    * Encapsulates the creation of the block's LazyPluginCollection.
143    *
144    * @return \Drupal\Component\Plugin\LazyPluginCollection
145    *   The block's plugin collection.
146    */
147   protected function getPluginCollection() {
148     if (!$this->pluginCollection) {
149       $this->pluginCollection = new BlockPluginCollection(\Drupal::service('plugin.manager.block'), $this->plugin, $this->get('settings'), $this->id());
150     }
151     return $this->pluginCollection;
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public function getPluginCollections() {
158     return [
159       'settings' => $this->getPluginCollection(),
160       'visibility' => $this->getVisibilityConditions(),
161     ];
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public function getPluginId() {
168     return $this->plugin;
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   public function getRegion() {
175     return $this->region;
176   }
177
178   /**
179    * {@inheritdoc}
180    */
181   public function getTheme() {
182     return $this->theme;
183   }
184
185   /**
186    * {@inheritdoc}
187    */
188   public function getWeight() {
189     return $this->weight;
190   }
191
192   /**
193    * {@inheritdoc}
194    */
195   public function label() {
196     $settings = $this->get('settings');
197     if ($settings['label']) {
198       return $settings['label'];
199     }
200     else {
201       $definition = $this->getPlugin()->getPluginDefinition();
202       return $definition['admin_label'];
203     }
204   }
205
206   /**
207    * Sorts active blocks by weight; sorts inactive blocks by name.
208    */
209   public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
210     // Separate enabled from disabled.
211     $status = (int) $b->status() - (int) $a->status();
212     if ($status !== 0) {
213       return $status;
214     }
215
216     // Sort by weight.
217     $weight = $a->getWeight() - $b->getWeight();
218     if ($weight) {
219       return $weight;
220     }
221
222     // Sort by label.
223     return strcmp($a->label(), $b->label());
224   }
225
226   /**
227    * {@inheritdoc}
228    */
229   public function calculateDependencies() {
230     parent::calculateDependencies();
231     $this->addDependency('theme', $this->theme);
232     return $this;
233   }
234
235   /**
236    * {@inheritdoc}
237    */
238   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
239     parent::postSave($storage, $update);
240
241     // Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
242     // handles the regular cases. The Block entity has one special case: a
243     // newly created block may *also* appear on any page in the current theme,
244     // so we must invalidate the associated block's cache tag (which includes
245     // the theme cache tag).
246     if (!$update) {
247       Cache::invalidateTags($this->getCacheTagsToInvalidate());
248     }
249   }
250
251   /**
252    * {@inheritdoc}
253    */
254   public function getVisibility() {
255     return $this->getVisibilityConditions()->getConfiguration();
256   }
257
258   /**
259    * {@inheritdoc}
260    */
261   public function setVisibilityConfig($instance_id, array $configuration) {
262     $conditions = $this->getVisibilityConditions();
263     if (!$conditions->has($instance_id)) {
264       $configuration['id'] = $instance_id;
265       $conditions->addInstanceId($instance_id, $configuration);
266     }
267     else {
268       $conditions->setInstanceConfiguration($instance_id, $configuration);
269     }
270     return $this;
271   }
272
273   /**
274    * {@inheritdoc}
275    */
276   public function getVisibilityConditions() {
277     if (!isset($this->visibilityCollection)) {
278       $this->visibilityCollection = new ConditionPluginCollection($this->conditionPluginManager(), $this->get('visibility'));
279     }
280     return $this->visibilityCollection;
281   }
282
283   /**
284    * {@inheritdoc}
285    */
286   public function getVisibilityCondition($instance_id) {
287     return $this->getVisibilityConditions()->get($instance_id);
288   }
289
290   /**
291    * Gets the condition plugin manager.
292    *
293    * @return \Drupal\Core\Executable\ExecutableManagerInterface
294    *   The condition plugin manager.
295    */
296   protected function conditionPluginManager() {
297     if (!isset($this->conditionPluginManager)) {
298       $this->conditionPluginManager = \Drupal::service('plugin.manager.condition');
299     }
300     return $this->conditionPluginManager;
301   }
302
303   /**
304    * {@inheritdoc}
305    */
306   public function setRegion($region) {
307     $this->region = $region;
308     return $this;
309   }
310
311   /**
312    * {@inheritdoc}
313    */
314   public function setWeight($weight) {
315     $this->weight = $weight;
316     return $this;
317   }
318
319   /**
320    * {@inheritdoc}
321    */
322   public function createDuplicateBlock($new_id = NULL, $new_theme = NULL) {
323     $duplicate = parent::createDuplicate();
324     if (!empty($new_id)) {
325       $duplicate->id = $new_id;
326     }
327     if (!empty($new_theme)) {
328       $duplicate->theme = $new_theme;
329     }
330     return $duplicate;
331   }
332
333   /**
334    * {@inheritdoc}
335    */
336   public function preSave(EntityStorageInterface $storage) {
337     parent::preSave($storage);
338
339     // Ensure the region is valid to mirror the behavior of block_rebuild().
340     // This is done primarily for backwards compatibility support of
341     // \Drupal\block\BlockInterface::BLOCK_REGION_NONE.
342     $regions = system_region_list($this->theme);
343     if (!isset($regions[$this->region]) && $this->status()) {
344       $this
345         ->setRegion(system_default_region($this->theme))
346         ->disable();
347     }
348   }
349
350 }