comparison core/modules/block_content/src/Entity/BlockContent.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\block_content\Entity;
4
5 use Drupal\Core\Entity\EditorialContentEntityBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Field\BaseFieldDefinition;
9 use Drupal\block_content\BlockContentInterface;
10 use Drupal\user\UserInterface;
11
12 /**
13 * Defines the custom block entity class.
14 *
15 * @ContentEntityType(
16 * id = "block_content",
17 * label = @Translation("Custom block"),
18 * bundle_label = @Translation("Custom block type"),
19 * handlers = {
20 * "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
21 * "access" = "Drupal\block_content\BlockContentAccessControlHandler",
22 * "list_builder" = "Drupal\block_content\BlockContentListBuilder",
23 * "view_builder" = "Drupal\block_content\BlockContentViewBuilder",
24 * "views_data" = "Drupal\block_content\BlockContentViewsData",
25 * "form" = {
26 * "add" = "Drupal\block_content\BlockContentForm",
27 * "edit" = "Drupal\block_content\BlockContentForm",
28 * "delete" = "Drupal\block_content\Form\BlockContentDeleteForm",
29 * "default" = "Drupal\block_content\BlockContentForm"
30 * },
31 * "translation" = "Drupal\block_content\BlockContentTranslationHandler"
32 * },
33 * admin_permission = "administer blocks",
34 * base_table = "block_content",
35 * revision_table = "block_content_revision",
36 * data_table = "block_content_field_data",
37 * revision_data_table = "block_content_field_revision",
38 * show_revision_ui = TRUE,
39 * links = {
40 * "canonical" = "/block/{block_content}",
41 * "delete-form" = "/block/{block_content}/delete",
42 * "edit-form" = "/block/{block_content}",
43 * "collection" = "/admin/structure/block/block-content",
44 * "create" = "/block",
45 * },
46 * translatable = TRUE,
47 * entity_keys = {
48 * "id" = "id",
49 * "revision" = "revision_id",
50 * "bundle" = "type",
51 * "label" = "info",
52 * "langcode" = "langcode",
53 * "uuid" = "uuid",
54 * "published" = "status",
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 EditorialContentEntityBase implements BlockContentInterface {
72
73 /**
74 * The theme the block is being created in.
75 *
76 * 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
78 * theme is stored against the block when the custom block add form is shown.
79 *
80 * @var string
81 */
82 protected $theme;
83
84 /**
85 * {@inheritdoc}
86 */
87 public function createDuplicate() {
88 $duplicate = parent::createDuplicate();
89 $duplicate->revision_id->value = NULL;
90 $duplicate->id->value = NULL;
91 return $duplicate;
92 }
93
94 /**
95 * {@inheritdoc}
96 */
97 public function setTheme($theme) {
98 $this->theme = $theme;
99 return $this;
100 }
101
102 /**
103 * {@inheritdoc}
104 */
105 public function getTheme() {
106 return $this->theme;
107 }
108
109 /**
110 * {@inheritdoc}
111 */
112 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
113 parent::postSave($storage, $update);
114 static::invalidateBlockPluginCache();
115 }
116
117 /**
118 * {@inheritdoc}
119 */
120 public static function postDelete(EntityStorageInterface $storage, array $entities) {
121 parent::postDelete($storage, $entities);
122 static::invalidateBlockPluginCache();
123 }
124
125 /**
126 * {@inheritdoc}
127 */
128 public function getInstances() {
129 return \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['plugin' => 'block_content:' . $this->uuid()]);
130 }
131
132 /**
133 * {@inheritdoc}
134 */
135 public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
136 parent::preSaveRevision($storage, $record);
137
138 if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
139 // If we are updating an existing block_content without adding a new
140 // revision and the user did not supply a revision log, keep the existing
141 // one.
142 $record->revision_log = $this->original->getRevisionLogMessage();
143 }
144 }
145
146 /**
147 * {@inheritdoc}
148 */
149 public function delete() {
150 foreach ($this->getInstances() as $instance) {
151 $instance->delete();
152 }
153 parent::delete();
154 }
155
156 /**
157 * {@inheritdoc}
158 */
159 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
160 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
161 $fields = parent::baseFieldDefinitions($entity_type);
162
163 $fields['id']->setLabel(t('Custom block ID'))
164 ->setDescription(t('The custom block ID.'));
165
166 $fields['uuid']->setDescription(t('The custom block UUID.'));
167
168 $fields['revision_id']->setDescription(t('The revision ID.'));
169
170 $fields['langcode']->setDescription(t('The custom block language code.'));
171
172 $fields['type']->setLabel(t('Block type'))
173 ->setDescription(t('The block type.'));
174
175 $fields['revision_log']->setDescription(t('The log entry explaining the changes in this revision.'));
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['changed'] = BaseFieldDefinition::create('changed')
191 ->setLabel(t('Changed'))
192 ->setDescription(t('The time that the custom block was last edited.'))
193 ->setTranslatable(TRUE)
194 ->setRevisionable(TRUE);
195
196 return $fields;
197 }
198
199 /**
200 * {@inheritdoc}
201 */
202 public function getRevisionLog() {
203 return $this->getRevisionLogMessage();
204 }
205
206 /**
207 * {@inheritdoc}
208 */
209 public function setInfo($info) {
210 $this->set('info', $info);
211 return $this;
212 }
213
214 /**
215 * {@inheritdoc}
216 */
217 public function setRevisionLog($revision_log) {
218 return $this->setRevisionLogMessage($revision_log);
219 }
220
221 /**
222 * {@inheritdoc}
223 */
224 public function getRevisionCreationTime() {
225 return $this->get('revision_created')->value;
226 }
227
228 /**
229 * {@inheritdoc}
230 */
231 public function setRevisionCreationTime($timestamp) {
232 $this->set('revision_created', $timestamp);
233 return $this;
234 }
235
236 /**
237 * {@inheritdoc}
238 */
239 public function getRevisionUser() {
240 return $this->get('revision_user')->entity;
241 }
242
243 public function setRevisionUser(UserInterface $account) {
244 $this->set('revision_user', $account);
245 return $this;
246 }
247
248 /**
249 * {@inheritdoc}
250 */
251 public function getRevisionUserId() {
252 return $this->get('revision_user')->entity->id();
253 }
254
255 /**
256 * {@inheritdoc}
257 */
258 public function setRevisionUserId($user_id) {
259 $this->set('revision_user', $user_id);
260 return $this;
261 }
262
263 /**
264 * {@inheritdoc}
265 */
266 public function getRevisionLogMessage() {
267 return $this->get('revision_log')->value;
268 }
269
270 /**
271 * {@inheritdoc}
272 */
273 public function setRevisionLogMessage($revision_log_message) {
274 $this->set('revision_log', $revision_log_message);
275 return $this;
276 }
277
278 /**
279 * Invalidates the block plugin cache after changes and deletions.
280 */
281 protected static function invalidateBlockPluginCache() {
282 // Invalidate the block cache to update custom block-based derivatives.
283 \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
284 }
285
286 }