Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\block_content\Entity\BlockContent.
Dries Buytaert
committed
*/
namespace Drupal\block_content\Entity;
Dries Buytaert
committed
use Drupal\Core\Entity\ContentEntityBase;
catch
committed
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
Alex Pott
committed
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\block_content\BlockContentInterface;
Dries Buytaert
committed
/**
* Defines the custom block entity class.
*
Alex Pott
committed
* @ContentEntityType(
* id = "block_content",
Alex Pott
committed
* label = @Translation("Custom block"),
* bundle_label = @Translation("Custom block type"),
Alex Pott
committed
* handlers = {
catch
committed
* "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
* "storage_schema" = "Drupal\block_content\BlockContentStorageSchema",
* "access" = "Drupal\block_content\BlockContentAccessControlHandler",
* "list_builder" = "Drupal\block_content\BlockContentListBuilder",
* "view_builder" = "Drupal\block_content\BlockContentViewBuilder",
Alex Pott
committed
* "views_data" = "Drupal\block_content\BlockContentViewsData",
* "add" = "Drupal\block_content\BlockContentForm",
* "edit" = "Drupal\block_content\BlockContentForm",
* "delete" = "Drupal\block_content\Form\BlockContentDeleteForm",
* "default" = "Drupal\block_content\BlockContentForm"
* "translation" = "Drupal\block_content\BlockContentTranslationHandler"
Dries Buytaert
committed
* },
* admin_permission = "administer blocks",
* base_table = "block_content",
* revision_table = "block_content_revision",
* data_table = "block_content_field_data",
* links = {
* "canonical" = "/block/{block_content}",
* "delete-form" = "/block/{block_content}/delete",
* "edit-form" = "/block/{block_content}",
Alex Pott
committed
* "collection" = "/admin/structure/block/block-content",
* },
Angie Byron
committed
* translatable = TRUE,
Dries Buytaert
committed
* entity_keys = {
* "id" = "id",
* "revision" = "revision_id",
* "bundle" = "type",
* "label" = "info",
* "langcode" = "langcode",
Dries Buytaert
committed
* "uuid" = "uuid"
* },
Alex Pott
committed
* bundle_entity_type = "block_content_type",
* field_ui_base_route = "entity.block_content_type.edit_form",
Alex Pott
committed
* render_cache = FALSE,
Dries Buytaert
committed
* )
Alex Pott
committed
*
* Note that render caching of block_content entities is disabled because they
* are always rendered as blocks, and blocks already have their own render
* caching.
* See https://www.drupal.org/node/2284917#comment-9132521 for more information.
Dries Buytaert
committed
*/
class BlockContent extends ContentEntityBase implements BlockContentInterface {
Dries Buytaert
committed
/**
* The theme the block is being created in.
*
* When creating a new custom block from the block library, the user is
* redirected to the configure form for that block in the given theme. The
* theme is stored against the block when the custom block add form is shown.
*
* @var string
*/
protected $theme;
/**
Jennifer Hodgdon
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function createDuplicate() {
$duplicate = parent::createDuplicate();
$duplicate->revision_id->value = NULL;
$duplicate->id->value = NULL;
return $duplicate;
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function setTheme($theme) {
$this->theme = $theme;
return $this;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getTheme() {
return $this->theme;
}
/**
* {@inheritdoc}
*/
catch
committed
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
Angie Byron
committed
// Invalidate the block cache to update custom block-based derivatives.
\Drupal::service('plugin.manager.block')->clearCachedDefinitions();
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function getInstances() {
return entity_load_multiple_by_properties('block', array('plugin' => 'block_content:' . $this->uuid()));
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
catch
committed
public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
parent::preSaveRevision($storage, $record);
Angie Byron
committed
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->getRevisionLog();
}
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function delete() {
foreach ($this->getInstances() as $instance) {
$instance->delete();
}
parent::delete();
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
Alex Pott
committed
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Custom block ID'))
->setDescription(t('The custom block ID.'))
catch
committed
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
Alex Pott
committed
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The custom block UUID.'))
->setReadOnly(TRUE);
Alex Pott
committed
$fields['revision_id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The revision ID.'))
catch
committed
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
Alex Pott
committed
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language'))
->setDescription(t('The custom block language code.'))
Alex Pott
committed
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDisplayOptions('view', array(
'type' => 'hidden',
))
->setDisplayOptions('form', array(
'type' => 'language_select',
'weight' => 2,
));
Alex Pott
committed
$fields['info'] = BaseFieldDefinition::create('string')
catch
committed
->setLabel(t('Block description'))
->setDescription(t('A brief description of your block.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
catch
committed
->setRequired(TRUE)
->setDisplayOptions('form', array(
'type' => 'string_textfield',
catch
committed
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE);
Alex Pott
committed
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Block type'))
->setDescription(t('The block type.'))
->setSetting('target_type', 'block_content_type');
Alex Pott
committed
$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);
Alex Pott
committed
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the custom block was last edited.'))
->setRevisionable(TRUE);
return $fields;
Alex Pott
committed
}
Dries Buytaert
committed
/**
* {@inheritdoc}
*/
public function getChangedTime() {
return $this->get('changed')->value;
}
/**
* {@inheritdoc}
*/
public function getRevisionLog() {
return $this->get('revision_log')->value;
}
/**
* {@inheritdoc}
*/
public function setInfo($info) {
$this->set('info', $info);
return $this;
}
/**
* {@inheritdoc}
*/
public function setRevisionLog($revision_log) {
$this->set('revision_log', $revision_log);
return $this;
}