Mercurial > hg > isophonics-drupal-site
comparison core/modules/block_content/src/Entity/BlockContent.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\block_content\Entity; | |
4 | |
5 use Drupal\Core\Entity\ContentEntityBase; | |
6 use Drupal\Core\Entity\EntityChangedTrait; | |
7 use Drupal\Core\Entity\EntityStorageInterface; | |
8 use Drupal\Core\Entity\EntityTypeInterface; | |
9 use Drupal\Core\Field\BaseFieldDefinition; | |
10 use Drupal\block_content\BlockContentInterface; | |
11 use Drupal\user\UserInterface; | |
12 | |
13 /** | |
14 * Defines the custom block entity class. | |
15 * | |
16 * @ContentEntityType( | |
17 * id = "block_content", | |
18 * label = @Translation("Custom block"), | |
19 * bundle_label = @Translation("Custom block type"), | |
20 * handlers = { | |
21 * "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage", | |
22 * "access" = "Drupal\block_content\BlockContentAccessControlHandler", | |
23 * "list_builder" = "Drupal\block_content\BlockContentListBuilder", | |
24 * "view_builder" = "Drupal\block_content\BlockContentViewBuilder", | |
25 * "views_data" = "Drupal\block_content\BlockContentViewsData", | |
26 * "form" = { | |
27 * "add" = "Drupal\block_content\BlockContentForm", | |
28 * "edit" = "Drupal\block_content\BlockContentForm", | |
29 * "delete" = "Drupal\block_content\Form\BlockContentDeleteForm", | |
30 * "default" = "Drupal\block_content\BlockContentForm" | |
31 * }, | |
32 * "translation" = "Drupal\block_content\BlockContentTranslationHandler" | |
33 * }, | |
34 * admin_permission = "administer blocks", | |
35 * base_table = "block_content", | |
36 * revision_table = "block_content_revision", | |
37 * data_table = "block_content_field_data", | |
38 * revision_data_table = "block_content_field_revision", | |
39 * show_revision_ui = TRUE, | |
40 * links = { | |
41 * "canonical" = "/block/{block_content}", | |
42 * "delete-form" = "/block/{block_content}/delete", | |
43 * "edit-form" = "/block/{block_content}", | |
44 * "collection" = "/admin/structure/block/block-content", | |
45 * "create" = "/block", | |
46 * }, | |
47 * translatable = TRUE, | |
48 * entity_keys = { | |
49 * "id" = "id", | |
50 * "revision" = "revision_id", | |
51 * "bundle" = "type", | |
52 * "label" = "info", | |
53 * "langcode" = "langcode", | |
54 * "uuid" = "uuid" | |
55 * }, | |
56 * revision_metadata_keys = { | |
57 * "revision_user" = "revision_user", | |
58 * "revision_created" = "revision_created", | |
59 * "revision_log_message" = "revision_log" | |
60 * }, | |
61 * bundle_entity_type = "block_content_type", | |
62 * field_ui_base_route = "entity.block_content_type.edit_form", | |
63 * render_cache = FALSE, | |
64 * ) | |
65 * | |
66 * Note that render caching of block_content entities is disabled because they | |
67 * are always rendered as blocks, and blocks already have their own render | |
68 * caching. | |
69 * See https://www.drupal.org/node/2284917#comment-9132521 for more information. | |
70 */ | |
71 class BlockContent extends ContentEntityBase implements BlockContentInterface { | |
72 | |
73 use EntityChangedTrait; | |
74 | |
75 /** | |
76 * The theme the block is being created in. | |
77 * | |
78 * When creating a new custom block from the block library, the user is | |
79 * redirected to the configure form for that block in the given theme. The | |
80 * theme is stored against the block when the custom block add form is shown. | |
81 * | |
82 * @var string | |
83 */ | |
84 protected $theme; | |
85 | |
86 /** | |
87 * {@inheritdoc} | |
88 */ | |
89 public function createDuplicate() { | |
90 $duplicate = parent::createDuplicate(); | |
91 $duplicate->revision_id->value = NULL; | |
92 $duplicate->id->value = NULL; | |
93 return $duplicate; | |
94 } | |
95 | |
96 /** | |
97 * {@inheritdoc} | |
98 */ | |
99 public function setTheme($theme) { | |
100 $this->theme = $theme; | |
101 return $this; | |
102 } | |
103 | |
104 /** | |
105 * {@inheritdoc} | |
106 */ | |
107 public function getTheme() { | |
108 return $this->theme; | |
109 } | |
110 | |
111 /** | |
112 * {@inheritdoc} | |
113 */ | |
114 public function postSave(EntityStorageInterface $storage, $update = TRUE) { | |
115 parent::postSave($storage, $update); | |
116 static::invalidateBlockPluginCache(); | |
117 } | |
118 | |
119 /** | |
120 * {@inheritdoc} | |
121 */ | |
122 public static function postDelete(EntityStorageInterface $storage, array $entities) { | |
123 parent::postDelete($storage, $entities); | |
124 static::invalidateBlockPluginCache(); | |
125 } | |
126 | |
127 /** | |
128 * {@inheritdoc} | |
129 */ | |
130 public function getInstances() { | |
131 return \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['plugin' => 'block_content:' . $this->uuid()]); | |
132 } | |
133 | |
134 /** | |
135 * {@inheritdoc} | |
136 */ | |
137 public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) { | |
138 parent::preSaveRevision($storage, $record); | |
139 | |
140 if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) { | |
141 // If we are updating an existing block_content without adding a new | |
142 // revision and the user did not supply a revision log, keep the existing | |
143 // one. | |
144 $record->revision_log = $this->original->getRevisionLogMessage(); | |
145 } | |
146 } | |
147 | |
148 /** | |
149 * {@inheritdoc} | |
150 */ | |
151 public function delete() { | |
152 foreach ($this->getInstances() as $instance) { | |
153 $instance->delete(); | |
154 } | |
155 parent::delete(); | |
156 } | |
157 | |
158 /** | |
159 * {@inheritdoc} | |
160 */ | |
161 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { | |
162 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */ | |
163 $fields = parent::baseFieldDefinitions($entity_type); | |
164 | |
165 $fields['id']->setLabel(t('Custom block ID')) | |
166 ->setDescription(t('The custom block ID.')); | |
167 | |
168 $fields['uuid']->setDescription(t('The custom block UUID.')); | |
169 | |
170 $fields['revision_id']->setDescription(t('The revision ID.')); | |
171 | |
172 $fields['langcode']->setDescription(t('The custom block language code.')); | |
173 | |
174 $fields['type']->setLabel(t('Block type')) | |
175 ->setDescription(t('The block type.')); | |
176 | |
177 $fields['info'] = BaseFieldDefinition::create('string') | |
178 ->setLabel(t('Block description')) | |
179 ->setDescription(t('A brief description of your block.')) | |
180 ->setRevisionable(TRUE) | |
181 ->setTranslatable(TRUE) | |
182 ->setRequired(TRUE) | |
183 ->setDisplayOptions('form', [ | |
184 'type' => 'string_textfield', | |
185 'weight' => -5, | |
186 ]) | |
187 ->setDisplayConfigurable('form', TRUE) | |
188 ->addConstraint('UniqueField', []); | |
189 | |
190 $fields['revision_log'] = BaseFieldDefinition::create('string_long') | |
191 ->setLabel(t('Revision log message')) | |
192 ->setDescription(t('The log entry explaining the changes in this revision.')) | |
193 ->setRevisionable(TRUE) | |
194 ->setDisplayOptions('form', [ | |
195 'type' => 'string_textarea', | |
196 'weight' => 25, | |
197 'settings' => [ | |
198 'rows' => 4, | |
199 ], | |
200 ]); | |
201 | |
202 $fields['changed'] = BaseFieldDefinition::create('changed') | |
203 ->setLabel(t('Changed')) | |
204 ->setDescription(t('The time that the custom block was last edited.')) | |
205 ->setTranslatable(TRUE) | |
206 ->setRevisionable(TRUE); | |
207 | |
208 $fields['revision_created'] = BaseFieldDefinition::create('created') | |
209 ->setLabel(t('Revision create time')) | |
210 ->setDescription(t('The time that the current revision was created.')) | |
211 ->setRevisionable(TRUE); | |
212 | |
213 $fields['revision_user'] = BaseFieldDefinition::create('entity_reference') | |
214 ->setLabel(t('Revision user')) | |
215 ->setDescription(t('The user ID of the author of the current revision.')) | |
216 ->setSetting('target_type', 'user') | |
217 ->setRevisionable(TRUE); | |
218 | |
219 return $fields; | |
220 } | |
221 | |
222 /** | |
223 * {@inheritdoc} | |
224 */ | |
225 public function getRevisionLog() { | |
226 return $this->getRevisionLogMessage(); | |
227 } | |
228 | |
229 /** | |
230 * {@inheritdoc} | |
231 */ | |
232 public function setInfo($info) { | |
233 $this->set('info', $info); | |
234 return $this; | |
235 } | |
236 | |
237 /** | |
238 * {@inheritdoc} | |
239 */ | |
240 public function setRevisionLog($revision_log) { | |
241 return $this->setRevisionLogMessage($revision_log); | |
242 } | |
243 | |
244 /** | |
245 * {@inheritdoc} | |
246 */ | |
247 public function getRevisionCreationTime() { | |
248 return $this->get('revision_created')->value; | |
249 } | |
250 | |
251 /** | |
252 * {@inheritdoc} | |
253 */ | |
254 public function setRevisionCreationTime($timestamp) { | |
255 $this->set('revision_created', $timestamp); | |
256 return $this; | |
257 } | |
258 | |
259 /** | |
260 * {@inheritdoc} | |
261 */ | |
262 public function getRevisionUser() { | |
263 return $this->get('revision_user')->entity; | |
264 } | |
265 | |
266 public function setRevisionUser(UserInterface $account) { | |
267 $this->set('revision_user', $account); | |
268 return $this; | |
269 } | |
270 | |
271 /** | |
272 * {@inheritdoc} | |
273 */ | |
274 public function getRevisionUserId() { | |
275 return $this->get('revision_user')->entity->id(); | |
276 } | |
277 | |
278 /** | |
279 * {@inheritdoc} | |
280 */ | |
281 public function setRevisionUserId($user_id) { | |
282 $this->set('revision_user', $user_id); | |
283 return $this; | |
284 } | |
285 | |
286 /** | |
287 * {@inheritdoc} | |
288 */ | |
289 public function getRevisionLogMessage() { | |
290 return $this->get('revision_log')->value; | |
291 } | |
292 | |
293 /** | |
294 * {@inheritdoc} | |
295 */ | |
296 public function setRevisionLogMessage($revision_log_message) { | |
297 $this->set('revision_log', $revision_log_message); | |
298 return $this; | |
299 } | |
300 | |
301 /** | |
302 * Invalidates the block plugin cache after changes and deletions. | |
303 */ | |
304 protected static function invalidateBlockPluginCache() { | |
305 // Invalidate the block cache to update custom block-based derivatives. | |
306 \Drupal::service('plugin.manager.block')->clearCachedDefinitions(); | |
307 } | |
308 | |
309 } |