3 namespace Drupal\block_content\Entity;
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;
14 * Defines the custom block entity class.
17 * id = "block_content",
18 * label = @Translation("Custom block"),
19 * bundle_label = @Translation("Custom block type"),
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",
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"
32 * "translation" = "Drupal\block_content\BlockContentTranslationHandler"
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,
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",
46 * translatable = TRUE,
49 * "revision" = "revision_id",
52 * "langcode" = "langcode",
55 * bundle_entity_type = "block_content_type",
56 * field_ui_base_route = "entity.block_content_type.edit_form",
57 * render_cache = FALSE,
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
63 * See https://www.drupal.org/node/2284917#comment-9132521 for more information.
65 class BlockContent extends ContentEntityBase implements BlockContentInterface {
67 use EntityChangedTrait;
70 * The theme the block is being created in.
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.
83 public function createDuplicate() {
84 $duplicate = parent::createDuplicate();
85 $duplicate->revision_id->value = NULL;
86 $duplicate->id->value = NULL;
93 public function setTheme($theme) {
94 $this->theme = $theme;
101 public function getTheme() {
108 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
109 parent::postSave($storage, $update);
110 static::invalidateBlockPluginCache();
116 public static function postDelete(EntityStorageInterface $storage, array $entities) {
117 parent::postDelete($storage, $entities);
118 static::invalidateBlockPluginCache();
124 public function getInstances() {
125 return \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['plugin' => 'block_content:' . $this->uuid()]);
131 public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
132 parent::preSaveRevision($storage, $record);
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
138 $record->revision_log = $this->original->getRevisionLogMessage();
145 public function delete() {
146 foreach ($this->getInstances() as $instance) {
155 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
156 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
157 $fields = parent::baseFieldDefinitions($entity_type);
159 $fields['id']->setLabel(t('Custom block ID'))
160 ->setDescription(t('The custom block ID.'));
162 $fields['uuid']->setDescription(t('The custom block UUID.'));
164 $fields['revision_id']->setDescription(t('The revision ID.'));
166 $fields['langcode']->setDescription(t('The custom block language code.'));
168 $fields['type']->setLabel(t('Block type'))
169 ->setDescription(t('The block type.'));
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)
177 ->setDisplayOptions('form', [
178 'type' => 'string_textfield',
181 ->setDisplayConfigurable('form', TRUE)
182 ->addConstraint('UniqueField', []);
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',
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);
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);
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);
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.'))
217 ->setRevisionable(TRUE)
218 ->setTranslatable(TRUE);
226 public function getRevisionLog() {
227 return $this->getRevisionLogMessage();
233 public function setInfo($info) {
234 $this->set('info', $info);
241 public function setRevisionLog($revision_log) {
242 return $this->setRevisionLogMessage($revision_log);
248 public function getRevisionCreationTime() {
249 return $this->get('revision_created')->value;
255 public function setRevisionCreationTime($timestamp) {
256 $this->set('revision_created', $timestamp);
263 public function getRevisionUser() {
264 return $this->get('revision_user')->entity;
267 public function setRevisionUser(UserInterface $account) {
268 $this->set('revision_user', $account);
275 public function getRevisionUserId() {
276 return $this->get('revision_user')->entity->id();
282 public function setRevisionUserId($user_id) {
283 $this->set('revision_user', $user_id);
290 public function getRevisionLogMessage() {
291 return $this->get('revision_log')->value;
297 public function setRevisionLogMessage($revision_log_message) {
298 $this->set('revision_log', $revision_log_message);
303 * Invalidates the block plugin cache after changes and deletions.
305 protected static function invalidateBlockPluginCache() {
306 // Invalidate the block cache to update custom block-based derivatives.
307 \Drupal::service('plugin.manager.block')->clearCachedDefinitions();