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