3 namespace Drupal\block_content\Entity;
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;
13 * Defines the custom block entity class.
16 * id = "block_content",
17 * label = @Translation("Custom block"),
18 * bundle_label = @Translation("Custom block type"),
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",
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"
31 * "translation" = "Drupal\block_content\BlockContentTranslationHandler"
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,
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",
46 * translatable = TRUE,
49 * "revision" = "revision_id",
52 * "langcode" = "langcode",
54 * "published" = "status",
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 EditorialContentEntityBase implements BlockContentInterface {
74 * The theme the block is being created in.
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.
87 public function createDuplicate() {
88 $duplicate = parent::createDuplicate();
89 $duplicate->revision_id->value = NULL;
90 $duplicate->id->value = NULL;
97 public function setTheme($theme) {
98 $this->theme = $theme;
105 public function getTheme() {
112 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
113 parent::postSave($storage, $update);
114 static::invalidateBlockPluginCache();
120 public static function postDelete(EntityStorageInterface $storage, array $entities) {
121 parent::postDelete($storage, $entities);
122 static::invalidateBlockPluginCache();
128 public function getInstances() {
129 return \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['plugin' => 'block_content:' . $this->uuid()]);
135 public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
136 parent::preSaveRevision($storage, $record);
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
142 $record->revision_log = $this->original->getRevisionLogMessage();
149 public function delete() {
150 foreach ($this->getInstances() as $instance) {
159 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
160 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
161 $fields = parent::baseFieldDefinitions($entity_type);
163 $fields['id']->setLabel(t('Custom block ID'))
164 ->setDescription(t('The custom block ID.'));
166 $fields['uuid']->setDescription(t('The custom block UUID.'));
168 $fields['revision_id']->setDescription(t('The revision ID.'));
170 $fields['langcode']->setDescription(t('The custom block language code.'));
172 $fields['type']->setLabel(t('Block type'))
173 ->setDescription(t('The block type.'));
175 $fields['revision_log']->setDescription(t('The log entry explaining the changes in this revision.'));
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['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);
202 public function getRevisionLog() {
203 return $this->getRevisionLogMessage();
209 public function setInfo($info) {
210 $this->set('info', $info);
217 public function setRevisionLog($revision_log) {
218 return $this->setRevisionLogMessage($revision_log);
224 public function getRevisionCreationTime() {
225 return $this->get('revision_created')->value;
231 public function setRevisionCreationTime($timestamp) {
232 $this->set('revision_created', $timestamp);
239 public function getRevisionUser() {
240 return $this->get('revision_user')->entity;
243 public function setRevisionUser(UserInterface $account) {
244 $this->set('revision_user', $account);
251 public function getRevisionUserId() {
252 return $this->get('revision_user')->entity->id();
258 public function setRevisionUserId($user_id) {
259 $this->set('revision_user', $user_id);
266 public function getRevisionLogMessage() {
267 return $this->get('revision_log')->value;
273 public function setRevisionLogMessage($revision_log_message) {
274 $this->set('revision_log', $revision_log_message);
279 * Invalidates the block plugin cache after changes and deletions.
281 protected static function invalidateBlockPluginCache() {
282 // Invalidate the block cache to update custom block-based derivatives.
283 \Drupal::service('plugin.manager.block')->clearCachedDefinitions();