CustomBlock.php 6.04 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\custom_block\Entity\CustomBlock.
6 7
 */

8
namespace Drupal\custom_block\Entity;
9

10
use Drupal\Core\Entity\ContentEntityBase;
11
use Drupal\Core\Entity\EntityStorageInterface;
12
use Drupal\Core\Entity\EntityTypeInterface;
13
use Drupal\Core\Field\FieldDefinition;
14
use Drupal\custom_block\CustomBlockInterface;
15 16 17 18

/**
 * Defines the custom block entity class.
 *
19
 * @ContentEntityType(
20 21 22
 *   id = "custom_block",
 *   label = @Translation("Custom Block"),
 *   bundle_label = @Translation("Custom Block type"),
23
 *   controllers = {
24
 *     "storage" = "Drupal\custom_block\CustomBlockStorage",
25
 *     "access" = "Drupal\custom_block\CustomBlockAccessController",
26
 *     "list_builder" = "Drupal\custom_block\CustomBlockListBuilder",
27
 *     "view_builder" = "Drupal\custom_block\CustomBlockViewBuilder",
28
 *     "form" = {
29 30
 *       "add" = "Drupal\custom_block\CustomBlockForm",
 *       "edit" = "Drupal\custom_block\CustomBlockForm",
31
 *       "delete" = "Drupal\custom_block\Form\CustomBlockDeleteForm",
32
 *       "default" = "Drupal\custom_block\CustomBlockForm"
33
 *     },
34
 *     "translation" = "Drupal\custom_block\CustomBlockTranslationHandler"
35
 *   },
36
 *   admin_permission = "administer blocks",
37 38
 *   base_table = "custom_block",
 *   revision_table = "custom_block_revision",
39
 *   links = {
40
 *     "canonical" = "custom_block.edit",
41
 *     "delete-form" = "custom_block.delete",
42 43
 *     "edit-form" = "custom_block.edit",
 *     "admin-form" = "custom_block.type_edit"
44
 *   },
45
 *   fieldable = TRUE,
46
 *   translatable = TRUE,
47 48 49 50 51 52 53
 *   entity_keys = {
 *     "id" = "id",
 *     "revision" = "revision_id",
 *     "bundle" = "type",
 *     "label" = "info",
 *     "uuid" = "uuid"
 *   },
54
 *   bundle_entity_type = "custom_block_type"
55 56
 * )
 */
57
class CustomBlock extends ContentEntityBase implements CustomBlockInterface {
58 59 60 61 62 63 64 65 66 67 68 69 70

  /**
   * 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;

  /**
71
   * {@inheritdoc}
72 73 74 75 76 77 78 79 80
   */
  public function createDuplicate() {
    $duplicate = parent::createDuplicate();
    $duplicate->revision_id->value = NULL;
    $duplicate->id->value = NULL;
    return $duplicate;
  }

  /**
81
   * {@inheritdoc}
82 83 84
   */
  public function setTheme($theme) {
    $this->theme = $theme;
85
    return $this;
86 87 88
  }

  /**
89
   * {@inheritdoc}
90 91 92 93 94
   */
  public function getTheme() {
    return $this->theme;
  }

95 96 97
  /**
   * {@inheritdoc}
   */
98 99
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);
100

101 102 103 104
    // Invalidate the block cache to update custom block-based derivatives.
    \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
  }

105 106 107 108
  /**
   * {@inheritdoc}
   */
  public function getInstances() {
109
    return entity_load_multiple_by_properties('block', array('plugin' => 'custom_block:' . $this->uuid()));
110 111
  }

112 113 114
  /**
   * {@inheritdoc}
   */
115 116
  public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
    parent::preSaveRevision($storage, $record);
117

118
    if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
119
      // If we are updating an existing custom_block without adding a new
120 121 122
      // revision and the user did not supply a revision log, keep the existing
      // one.
      $record->revision_log = $this->original->getRevisionLog();
123 124 125
    }
  }

126 127 128 129 130 131 132 133 134 135
  /**
   * {@inheritdoc}
   */
  public function delete() {
    foreach ($this->getInstances() as $instance) {
      $instance->delete();
    }
    parent::delete();
  }

136 137 138
  /**
   * {@inheritdoc}
   */
139
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
140 141 142
    $fields['id'] = FieldDefinition::create('integer')
      ->setLabel(t('Custom block ID'))
      ->setDescription(t('The custom block ID.'))
143 144
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
145 146 147 148 149 150 151 152 153

    $fields['uuid'] = FieldDefinition::create('uuid')
      ->setLabel(t('UUID'))
      ->setDescription(t('The custom block UUID.'))
      ->setReadOnly(TRUE);

    $fields['revision_id'] = FieldDefinition::create('integer')
      ->setLabel(t('Revision ID'))
      ->setDescription(t('The revision ID.'))
154 155
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
156 157 158 159 160 161

    $fields['langcode'] = FieldDefinition::create('language')
      ->setLabel(t('Language code'))
      ->setDescription(t('The custom block language code.'));

    $fields['info'] = FieldDefinition::create('string')
162 163 164 165 166 167 168 169 170
      ->setLabel(t('Block description'))
      ->setDescription(t('A brief description of your block.'))
      ->setRevisionable(TRUE)
      ->setRequired(TRUE)
      ->setDisplayOptions('form', array(
        'type' => 'string',
        'weight' => -5,
      ))
      ->setDisplayConfigurable('form', TRUE);
171

172
    $fields['type'] = FieldDefinition::create('entity_reference')
173
      ->setLabel(t('Block type'))
174
      ->setDescription(t('The block type.'))
175
      ->setSetting('target_type', 'custom_block_type');
176

177
    $fields['revision_log'] = FieldDefinition::create('string_long')
178
      ->setLabel(t('Revision log message'))
179
      ->setDescription(t('The log entry explaining the changes in this revision.'))
180
      ->setRevisionable(TRUE);
181

182
    $fields['changed'] = FieldDefinition::create('changed')
183
      ->setLabel(t('Changed'))
184 185
      ->setDescription(t('The time that the custom block was last edited.'))
      ->setRevisionable(TRUE);
186 187

    return $fields;
188 189
  }

190 191 192 193 194 195 196
  /**
   * {@inheritdoc}
   */
  public function getChangedTime() {
    return $this->get('changed')->value;
  }

197 198 199 200
  /**
   * {@inheritdoc}
   */
  public function getRevisionLog() {
201
    return $this->get('revision_log')->value;
202 203 204 205 206 207 208 209 210 211 212 213 214
  }

  /**
   * {@inheritdoc}
   */
  public function setInfo($info) {
    $this->set('info', $info);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
215 216
  public function setRevisionLog($revision_log) {
    $this->set('revision_log', $revision_log);
217 218 219
    return $this;
  }

220
}