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",
45 * "create" = "/block",
47 * translatable = TRUE,
50 * "revision" = "revision_id",
53 * "langcode" = "langcode",
56 * revision_metadata_keys = {
57 * "revision_user" = "revision_user",
58 * "revision_created" = "revision_created",
59 * "revision_log_message" = "revision_log"
61 * bundle_entity_type = "block_content_type",
62 * field_ui_base_route = "entity.block_content_type.edit_form",
63 * render_cache = FALSE,
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
69 * See https://www.drupal.org/node/2284917#comment-9132521 for more information.
71 class BlockContent extends ContentEntityBase implements BlockContentInterface {
73 use EntityChangedTrait;
76 * The theme the block is being created in.
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.
89 public function createDuplicate() {
90 $duplicate = parent::createDuplicate();
91 $duplicate->revision_id->value = NULL;
92 $duplicate->id->value = NULL;
99 public function setTheme($theme) {
100 $this->theme = $theme;
107 public function getTheme() {
114 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
115 parent::postSave($storage, $update);
116 static::invalidateBlockPluginCache();
122 public static function postDelete(EntityStorageInterface $storage, array $entities) {
123 parent::postDelete($storage, $entities);
124 static::invalidateBlockPluginCache();
130 public function getInstances() {
131 return \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['plugin' => 'block_content:' . $this->uuid()]);
137 public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
138 parent::preSaveRevision($storage, $record);
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
144 $record->revision_log = $this->original->getRevisionLogMessage();
151 public function delete() {
152 foreach ($this->getInstances() as $instance) {
161 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
162 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
163 $fields = parent::baseFieldDefinitions($entity_type);
165 $fields['id']->setLabel(t('Custom block ID'))
166 ->setDescription(t('The custom block ID.'));
168 $fields['uuid']->setDescription(t('The custom block UUID.'));
170 $fields['revision_id']->setDescription(t('The revision ID.'));
172 $fields['langcode']->setDescription(t('The custom block language code.'));
174 $fields['type']->setLabel(t('Block type'))
175 ->setDescription(t('The block type.'));
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)
183 ->setDisplayOptions('form', [
184 'type' => 'string_textfield',
187 ->setDisplayConfigurable('form', TRUE)
188 ->addConstraint('UniqueField', []);
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',
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);
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);
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);
225 public function getRevisionLog() {
226 return $this->getRevisionLogMessage();
232 public function setInfo($info) {
233 $this->set('info', $info);
240 public function setRevisionLog($revision_log) {
241 return $this->setRevisionLogMessage($revision_log);
247 public function getRevisionCreationTime() {
248 return $this->get('revision_created')->value;
254 public function setRevisionCreationTime($timestamp) {
255 $this->set('revision_created', $timestamp);
262 public function getRevisionUser() {
263 return $this->get('revision_user')->entity;
266 public function setRevisionUser(UserInterface $account) {
267 $this->set('revision_user', $account);
274 public function getRevisionUserId() {
275 return $this->get('revision_user')->entity->id();
281 public function setRevisionUserId($user_id) {
282 $this->set('revision_user', $user_id);
289 public function getRevisionLogMessage() {
290 return $this->get('revision_log')->value;
296 public function setRevisionLogMessage($revision_log_message) {
297 $this->set('revision_log', $revision_log_message);
302 * Invalidates the block plugin cache after changes and deletions.
304 protected static function invalidateBlockPluginCache() {
305 // Invalidate the block cache to update custom block-based derivatives.
306 \Drupal::service('plugin.manager.block')->clearCachedDefinitions();