revision_id->value = NULL; $duplicate->id->value = NULL; return $duplicate; } /** * {@inheritdoc} */ public function setTheme($theme) { $this->theme = $theme; return $this; } /** * {@inheritdoc} */ public function getTheme() { return $this->theme; } /** * {@inheritdoc} */ public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); static::invalidateBlockPluginCache(); } /** * {@inheritdoc} */ public static function postDelete(EntityStorageInterface $storage, array $entities) { parent::postDelete($storage, $entities); static::invalidateBlockPluginCache(); } /** * {@inheritdoc} */ public function getInstances() { return \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['plugin' => 'block_content:' . $this->uuid()]); } /** * {@inheritdoc} */ public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) { parent::preSaveRevision($storage, $record); if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) { // If we are updating an existing block_content without adding a new // revision and the user did not supply a revision log, keep the existing // one. $record->revision_log = $this->original->getRevisionLogMessage(); } } /** * {@inheritdoc} */ public function delete() { foreach ($this->getInstances() as $instance) { $instance->delete(); } parent::delete(); } /** * {@inheritdoc} */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */ $fields = parent::baseFieldDefinitions($entity_type); $fields['id']->setLabel(t('Custom block ID')) ->setDescription(t('The custom block ID.')); $fields['uuid']->setDescription(t('The custom block UUID.')); $fields['revision_id']->setDescription(t('The revision ID.')); $fields['langcode']->setDescription(t('The custom block language code.')); $fields['type']->setLabel(t('Block type')) ->setDescription(t('The block type.')); $fields['info'] = BaseFieldDefinition::create('string') ->setLabel(t('Block description')) ->setDescription(t('A brief description of your block.')) ->setRevisionable(TRUE) ->setTranslatable(TRUE) ->setRequired(TRUE) ->setDisplayOptions('form', [ 'type' => 'string_textfield', 'weight' => -5, ]) ->setDisplayConfigurable('form', TRUE) ->addConstraint('UniqueField', []); $fields['revision_log'] = BaseFieldDefinition::create('string_long') ->setLabel(t('Revision log message')) ->setDescription(t('The log entry explaining the changes in this revision.')) ->setRevisionable(TRUE) ->setDisplayOptions('form', [ 'type' => 'string_textarea', 'weight' => 25, 'settings' => [ 'rows' => 4, ], ]); $fields['changed'] = BaseFieldDefinition::create('changed') ->setLabel(t('Changed')) ->setDescription(t('The time that the custom block was last edited.')) ->setTranslatable(TRUE) ->setRevisionable(TRUE); $fields['revision_created'] = BaseFieldDefinition::create('created') ->setLabel(t('Revision create time')) ->setDescription(t('The time that the current revision was created.')) ->setRevisionable(TRUE); $fields['revision_user'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Revision user')) ->setDescription(t('The user ID of the author of the current revision.')) ->setSetting('target_type', 'user') ->setRevisionable(TRUE); $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean') ->setLabel(t('Revision translation affected')) ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.')) ->setReadOnly(TRUE) ->setRevisionable(TRUE) ->setTranslatable(TRUE); return $fields; } /** * {@inheritdoc} */ public function getRevisionLog() { return $this->getRevisionLogMessage(); } /** * {@inheritdoc} */ public function setInfo($info) { $this->set('info', $info); return $this; } /** * {@inheritdoc} */ public function setRevisionLog($revision_log) { return $this->setRevisionLogMessage($revision_log); } /** * {@inheritdoc} */ public function getRevisionCreationTime() { return $this->get('revision_created')->value; } /** * {@inheritdoc} */ public function setRevisionCreationTime($timestamp) { $this->set('revision_created', $timestamp); return $this; } /** * {@inheritdoc} */ public function getRevisionUser() { return $this->get('revision_user')->entity; } public function setRevisionUser(UserInterface $account) { $this->set('revision_user', $account); return $this; } /** * {@inheritdoc} */ public function getRevisionUserId() { return $this->get('revision_user')->entity->id(); } /** * {@inheritdoc} */ public function setRevisionUserId($user_id) { $this->set('revision_user', $user_id); return $this; } /** * {@inheritdoc} */ public function getRevisionLogMessage() { return $this->get('revision_log')->value; } /** * {@inheritdoc} */ public function setRevisionLogMessage($revision_log_message) { $this->set('revision_log', $revision_log_message); return $this; } /** * Invalidates the block plugin cache after changes and deletions. */ protected static function invalidateBlockPluginCache() { // Invalidate the block cache to update custom block-based derivatives. \Drupal::service('plugin.manager.block')->clearCachedDefinitions(); } }