Mercurial > hg > isophonics-drupal-site
comparison core/modules/block_content/src/Entity/BlockContent.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 1fec387a4317 |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\block_content\Entity; | 3 namespace Drupal\block_content\Entity; |
4 | 4 |
5 use Drupal\block_content\Access\RefinableDependentAccessTrait; | |
5 use Drupal\Core\Entity\EditorialContentEntityBase; | 6 use Drupal\Core\Entity\EditorialContentEntityBase; |
6 use Drupal\Core\Entity\EntityStorageInterface; | 7 use Drupal\Core\Entity\EntityStorageInterface; |
7 use Drupal\Core\Entity\EntityTypeInterface; | 8 use Drupal\Core\Entity\EntityTypeInterface; |
8 use Drupal\Core\Field\BaseFieldDefinition; | 9 use Drupal\Core\Field\BaseFieldDefinition; |
9 use Drupal\block_content\BlockContentInterface; | 10 use Drupal\block_content\BlockContentInterface; |
13 * Defines the custom block entity class. | 14 * Defines the custom block entity class. |
14 * | 15 * |
15 * @ContentEntityType( | 16 * @ContentEntityType( |
16 * id = "block_content", | 17 * id = "block_content", |
17 * label = @Translation("Custom block"), | 18 * label = @Translation("Custom block"), |
19 * label_collection = @Translation("Custom blocks"), | |
20 * label_singular = @Translation("custom block"), | |
21 * label_plural = @Translation("custom blocks"), | |
22 * label_count = @PluralTranslation( | |
23 * singular = "@count custom block", | |
24 * plural = "@count custom blocks", | |
25 * ), | |
18 * bundle_label = @Translation("Custom block type"), | 26 * bundle_label = @Translation("Custom block type"), |
19 * handlers = { | 27 * handlers = { |
20 * "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage", | 28 * "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage", |
21 * "access" = "Drupal\block_content\BlockContentAccessControlHandler", | 29 * "access" = "Drupal\block_content\BlockContentAccessControlHandler", |
22 * "list_builder" = "Drupal\block_content\BlockContentListBuilder", | 30 * "list_builder" = "Drupal\block_content\BlockContentListBuilder", |
68 * caching. | 76 * caching. |
69 * See https://www.drupal.org/node/2284917#comment-9132521 for more information. | 77 * See https://www.drupal.org/node/2284917#comment-9132521 for more information. |
70 */ | 78 */ |
71 class BlockContent extends EditorialContentEntityBase implements BlockContentInterface { | 79 class BlockContent extends EditorialContentEntityBase implements BlockContentInterface { |
72 | 80 |
81 use RefinableDependentAccessTrait; | |
82 | |
73 /** | 83 /** |
74 * The theme the block is being created in. | 84 * The theme the block is being created in. |
75 * | 85 * |
76 * When creating a new custom block from the block library, the user is | 86 * When creating a new custom block from the block library, the user is |
77 * redirected to the configure form for that block in the given theme. The | 87 * redirected to the configure form for that block in the given theme. The |
109 /** | 119 /** |
110 * {@inheritdoc} | 120 * {@inheritdoc} |
111 */ | 121 */ |
112 public function postSave(EntityStorageInterface $storage, $update = TRUE) { | 122 public function postSave(EntityStorageInterface $storage, $update = TRUE) { |
113 parent::postSave($storage, $update); | 123 parent::postSave($storage, $update); |
114 static::invalidateBlockPluginCache(); | 124 if ($this->isReusable() || (isset($this->original) && $this->original->isReusable())) { |
125 static::invalidateBlockPluginCache(); | |
126 } | |
115 } | 127 } |
116 | 128 |
117 /** | 129 /** |
118 * {@inheritdoc} | 130 * {@inheritdoc} |
119 */ | 131 */ |
120 public static function postDelete(EntityStorageInterface $storage, array $entities) { | 132 public static function postDelete(EntityStorageInterface $storage, array $entities) { |
121 parent::postDelete($storage, $entities); | 133 parent::postDelete($storage, $entities); |
122 static::invalidateBlockPluginCache(); | 134 /** @var \Drupal\block_content\BlockContentInterface $block */ |
135 foreach ($entities as $block) { | |
136 if ($block->isReusable()) { | |
137 // If any deleted blocks are reusable clear the block cache. | |
138 static::invalidateBlockPluginCache(); | |
139 return; | |
140 } | |
141 } | |
123 } | 142 } |
124 | 143 |
125 /** | 144 /** |
126 * {@inheritdoc} | 145 * {@inheritdoc} |
127 */ | 146 */ |
191 ->setLabel(t('Changed')) | 210 ->setLabel(t('Changed')) |
192 ->setDescription(t('The time that the custom block was last edited.')) | 211 ->setDescription(t('The time that the custom block was last edited.')) |
193 ->setTranslatable(TRUE) | 212 ->setTranslatable(TRUE) |
194 ->setRevisionable(TRUE); | 213 ->setRevisionable(TRUE); |
195 | 214 |
215 $fields['reusable'] = BaseFieldDefinition::create('boolean') | |
216 ->setLabel(t('Reusable')) | |
217 ->setDescription(t('A boolean indicating whether this block is reusable.')) | |
218 ->setTranslatable(FALSE) | |
219 ->setRevisionable(FALSE) | |
220 ->setDefaultValue(TRUE); | |
221 | |
196 return $fields; | 222 return $fields; |
197 } | 223 } |
198 | 224 |
199 /** | 225 /** |
200 * {@inheritdoc} | 226 * {@inheritdoc} |
271 * {@inheritdoc} | 297 * {@inheritdoc} |
272 */ | 298 */ |
273 public function setRevisionLogMessage($revision_log_message) { | 299 public function setRevisionLogMessage($revision_log_message) { |
274 $this->set('revision_log', $revision_log_message); | 300 $this->set('revision_log', $revision_log_message); |
275 return $this; | 301 return $this; |
302 } | |
303 | |
304 /** | |
305 * {@inheritdoc} | |
306 */ | |
307 public function isReusable() { | |
308 return (bool) $this->get('reusable')->value; | |
309 } | |
310 | |
311 /** | |
312 * {@inheritdoc} | |
313 */ | |
314 public function setReusable() { | |
315 return $this->set('reusable', TRUE); | |
316 } | |
317 | |
318 /** | |
319 * {@inheritdoc} | |
320 */ | |
321 public function setNonReusable() { | |
322 return $this->set('reusable', FALSE); | |
276 } | 323 } |
277 | 324 |
278 /** | 325 /** |
279 * Invalidates the block plugin cache after changes and deletions. | 326 * Invalidates the block plugin cache after changes and deletions. |
280 */ | 327 */ |