Version 1
[yaffs-website] / web / core / modules / block_content / src / Entity / BlockContent.php
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  *   },
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  *   },
55  *   bundle_entity_type = "block_content_type",
56  *   field_ui_base_route = "entity.block_content_type.edit_form",
57  *   render_cache = FALSE,
58  * )
59  *
60  * Note that render caching of block_content entities is disabled because they
61  * are always rendered as blocks, and blocks already have their own render
62  * caching.
63  * See https://www.drupal.org/node/2284917#comment-9132521 for more information.
64  */
65 class BlockContent extends ContentEntityBase implements BlockContentInterface {
66
67   use EntityChangedTrait;
68
69   /**
70    * The theme the block is being created in.
71    *
72    * When creating a new custom block from the block library, the user is
73    * redirected to the configure form for that block in the given theme. The
74    * theme is stored against the block when the custom block add form is shown.
75    *
76    * @var string
77    */
78   protected $theme;
79
80   /**
81    * {@inheritdoc}
82    */
83   public function createDuplicate() {
84     $duplicate = parent::createDuplicate();
85     $duplicate->revision_id->value = NULL;
86     $duplicate->id->value = NULL;
87     return $duplicate;
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function setTheme($theme) {
94     $this->theme = $theme;
95     return $this;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function getTheme() {
102     return $this->theme;
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
109     parent::postSave($storage, $update);
110     static::invalidateBlockPluginCache();
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public static function postDelete(EntityStorageInterface $storage, array $entities) {
117     parent::postDelete($storage, $entities);
118     static::invalidateBlockPluginCache();
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function getInstances() {
125     return \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['plugin' => 'block_content:' . $this->uuid()]);
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
132     parent::preSaveRevision($storage, $record);
133
134     if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
135       // If we are updating an existing block_content without adding a new
136       // revision and the user did not supply a revision log, keep the existing
137       // one.
138       $record->revision_log = $this->original->getRevisionLogMessage();
139     }
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function delete() {
146     foreach ($this->getInstances() as $instance) {
147       $instance->delete();
148     }
149     parent::delete();
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
156     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
157     $fields = parent::baseFieldDefinitions($entity_type);
158
159     $fields['id']->setLabel(t('Custom block ID'))
160       ->setDescription(t('The custom block ID.'));
161
162     $fields['uuid']->setDescription(t('The custom block UUID.'));
163
164     $fields['revision_id']->setDescription(t('The revision ID.'));
165
166     $fields['langcode']->setDescription(t('The custom block language code.'));
167
168     $fields['type']->setLabel(t('Block type'))
169       ->setDescription(t('The block type.'));
170
171     $fields['info'] = BaseFieldDefinition::create('string')
172       ->setLabel(t('Block description'))
173       ->setDescription(t('A brief description of your block.'))
174       ->setRevisionable(TRUE)
175       ->setTranslatable(TRUE)
176       ->setRequired(TRUE)
177       ->setDisplayOptions('form', [
178         'type' => 'string_textfield',
179         'weight' => -5,
180       ])
181       ->setDisplayConfigurable('form', TRUE)
182       ->addConstraint('UniqueField', []);
183
184     $fields['revision_log'] = BaseFieldDefinition::create('string_long')
185       ->setLabel(t('Revision log message'))
186       ->setDescription(t('The log entry explaining the changes in this revision.'))
187       ->setRevisionable(TRUE)
188       ->setDisplayOptions('form', [
189         'type' => 'string_textarea',
190         'weight' => 25,
191         'settings' => [
192           'rows' => 4,
193         ],
194       ]);
195
196     $fields['changed'] = BaseFieldDefinition::create('changed')
197       ->setLabel(t('Changed'))
198       ->setDescription(t('The time that the custom block was last edited.'))
199       ->setTranslatable(TRUE)
200       ->setRevisionable(TRUE);
201
202     $fields['revision_created'] = BaseFieldDefinition::create('created')
203       ->setLabel(t('Revision create time'))
204       ->setDescription(t('The time that the current revision was created.'))
205       ->setRevisionable(TRUE);
206
207     $fields['revision_user'] = BaseFieldDefinition::create('entity_reference')
208       ->setLabel(t('Revision user'))
209       ->setDescription(t('The user ID of the author of the current revision.'))
210       ->setSetting('target_type', 'user')
211       ->setRevisionable(TRUE);
212
213     $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
214       ->setLabel(t('Revision translation affected'))
215       ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
216       ->setReadOnly(TRUE)
217       ->setRevisionable(TRUE)
218       ->setTranslatable(TRUE);
219
220     return $fields;
221   }
222
223   /**
224    * {@inheritdoc}
225    */
226   public function getRevisionLog() {
227     return $this->getRevisionLogMessage();
228   }
229
230   /**
231    * {@inheritdoc}
232    */
233   public function setInfo($info) {
234     $this->set('info', $info);
235     return $this;
236   }
237
238   /**
239    * {@inheritdoc}
240    */
241   public function setRevisionLog($revision_log) {
242     return $this->setRevisionLogMessage($revision_log);
243   }
244
245   /**
246    * {@inheritdoc}
247    */
248   public function getRevisionCreationTime() {
249     return $this->get('revision_created')->value;
250   }
251
252   /**
253    * {@inheritdoc}
254    */
255   public function setRevisionCreationTime($timestamp) {
256     $this->set('revision_created', $timestamp);
257     return $this;
258   }
259
260   /**
261    * {@inheritdoc}
262    */
263   public function getRevisionUser() {
264     return $this->get('revision_user')->entity;
265   }
266
267   public function setRevisionUser(UserInterface $account) {
268     $this->set('revision_user', $account);
269     return $this;
270   }
271
272   /**
273    * {@inheritdoc}
274    */
275   public function getRevisionUserId() {
276     return $this->get('revision_user')->entity->id();
277   }
278
279   /**
280    * {@inheritdoc}
281    */
282   public function setRevisionUserId($user_id) {
283     $this->set('revision_user', $user_id);
284     return $this;
285   }
286
287   /**
288    * {@inheritdoc}
289    */
290   public function getRevisionLogMessage() {
291     return $this->get('revision_log')->value;
292   }
293
294   /**
295    * {@inheritdoc}
296    */
297   public function setRevisionLogMessage($revision_log_message) {
298     $this->set('revision_log', $revision_log_message);
299     return $this;
300   }
301
302   /**
303    * Invalidates the block plugin cache after changes and deletions.
304    */
305   protected static function invalidateBlockPluginCache() {
306     // Invalidate the block cache to update custom block-based derivatives.
307     \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
308   }
309
310 }