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); if ($this->isReusable() || (isset($this->original) && $this->original->isReusable())) { static::invalidateBlockPluginCache(); } } /** * {@inheritdoc} */ public static function preDelete(EntityStorageInterface $storage, array $entities) { parent::preDelete($storage, $entities); /** @var \Drupal\block_content\BlockContentInterface $block */ foreach ($entities as $block) { foreach ($block->getInstances() as $instance) { $instance->delete(); } } } /** * {@inheritdoc} */ public static function postDelete(EntityStorageInterface $storage, array $entities) { parent::postDelete($storage, $entities); /** @var \Drupal\block_content\BlockContentInterface $block */ foreach ($entities as $block) { if ($block->isReusable()) { // If any deleted blocks are reusable clear the block cache. static::invalidateBlockPluginCache(); return; } } } /** * {@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) && empty($record->revision_log_message)) { // 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 static function baseFieldDefinitions(EntityTypeInterface $entity_type) { /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */ $fields = parent::baseFieldDefinitions($entity_type); $fields['id']->setLabel(t('Content block ID')) ->setDescription(t('The content block ID.')); $fields['uuid']->setDescription(t('The content block UUID.')); $fields['revision_id']->setDescription(t('The revision ID.')); $fields['langcode']->setDescription(t('The content block language code.')); $fields['type']->setLabel(t('Block type')) ->setDescription(t('The block type.')); $fields['revision_log']->setDescription(t('The log entry explaining the changes in this revision.')); $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); $fields['changed'] = BaseFieldDefinition::create('changed') ->setLabel(t('Changed')) ->setDescription(t('The time that the content block was last edited.')) ->setTranslatable(TRUE) ->setRevisionable(TRUE); $fields['reusable'] = BaseFieldDefinition::create('boolean') ->setLabel(t('Reusable')) ->setDescription(t('A boolean indicating whether this block is reusable.')) ->setTranslatable(FALSE) ->setRevisionable(FALSE) ->setDefaultValue(TRUE); return $fields; } /** * {@inheritdoc} */ public function setInfo($info) { $this->set('info', $info); return $this; } /** * {@inheritdoc} */ public function isReusable() { return (bool) $this->get('reusable')->value; } /** * {@inheritdoc} */ public function setReusable() { return $this->set('reusable', TRUE); } /** * {@inheritdoc} */ public function setNonReusable() { return $this->set('reusable', FALSE); } /** * Invalidates the block plugin cache after changes and deletions. */ protected static function invalidateBlockPluginCache() { // Invalidate the block cache to update content block-based derivatives. \Drupal::service('plugin.manager.block')->clearCachedDefinitions(); } }