CustomBlock.php 6.76 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\EntityStorageControllerInterface;
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 24
 *   controllers = {
 *     "access" = "Drupal\custom_block\CustomBlockAccessController",
25
 *     "list" = "Drupal\custom_block\CustomBlockListController",
26
 *     "view_builder" = "Drupal\custom_block\CustomBlockViewBuilder",
27
 *     "form" = {
28 29
 *       "add" = "Drupal\custom_block\CustomBlockFormController",
 *       "edit" = "Drupal\custom_block\CustomBlockFormController",
30
 *       "delete" = "Drupal\custom_block\Form\CustomBlockDeleteForm",
31 32 33
 *       "default" = "Drupal\custom_block\CustomBlockFormController"
 *     },
 *     "translation" = "Drupal\custom_block\CustomBlockTranslationController"
34
 *   },
35
 *   admin_permission = "administer blocks",
36 37
 *   base_table = "custom_block",
 *   revision_table = "custom_block_revision",
38
 *   links = {
39
 *     "canonical" = "custom_block.edit",
40
 *     "delete-form" = "custom_block.delete",
41 42
 *     "edit-form" = "custom_block.edit",
 *     "admin-form" = "custom_block.type_edit"
43
 *   },
44
 *   fieldable = TRUE,
45
 *   translatable = TRUE,
46 47 48 49 50 51 52
 *   entity_keys = {
 *     "id" = "id",
 *     "revision" = "revision_id",
 *     "bundle" = "type",
 *     "label" = "info",
 *     "uuid" = "uuid"
 *   },
53
 *   bundle_entity_type = "custom_block_type"
54 55
 * )
 */
56
class CustomBlock extends ContentEntityBase implements CustomBlockInterface {
57 58 59 60 61 62 63 64 65 66 67 68 69

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

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

  /**
80
   * {@inheritdoc}
81 82
   */
  public function getRevisionId() {
83
    return $this->get('revision_id')->value;
84 85 86
  }

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

  /**
95
   * {@inheritdoc}
96 97 98 99 100
   */
  public function getTheme() {
    return $this->theme;
  }

101 102 103 104
  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageControllerInterface $storage_controller) {
105 106
    parent::preSave($storage_controller);

107
    // Before saving the custom block, set changed time.
108
    $this->set('changed', REQUEST_TIME);
109 110
  }

111 112 113 114
  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) {
115 116
    parent::postSave($storage_controller, $update);

117 118 119 120
    // Invalidate the block cache to update custom block-based derivatives.
    \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
  }

121 122 123 124
  /**
   * {@inheritdoc}
   */
  public function getInstances() {
125
    return entity_load_multiple_by_properties('block', array('plugin' => 'custom_block:' . $this->uuid()));
126 127
  }

128 129 130 131
  /**
   * {@inheritdoc}
   */
  public function preSaveRevision(EntityStorageControllerInterface $storage_controller, \stdClass $record) {
132 133
    parent::preSaveRevision($storage_controller, $record);

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    if ($this->isNewRevision()) {
      // When inserting either a new custom block or a new custom_block
      // revision, $entity->log must be set because {block_custom_revision}.log
      // is a text column and therefore cannot have a default value. However,
      // it might not be set at this point (for example, if the user submitting
      // the form does not have permission to create revisions), so we ensure
      // that it is at least an empty string in that case.
      // @todo: Make the {block_custom_revision}.log column nullable so that we
      // can remove this check.
      if (!isset($record->log)) {
        $record->log = '';
      }
    }
    elseif (isset($this->original) && (!isset($record->log) || $record->log === '')) {
      // If we are updating an existing custom_block without adding a new
      // revision and the user did not supply a log, keep the existing one.
150
      $record->log = $this->original->getRevisionLog();
151 152 153
    }
  }

154 155 156 157 158 159 160 161 162 163
  /**
   * {@inheritdoc}
   */
  public function delete() {
    foreach ($this->getInstances() as $instance) {
      $instance->delete();
    }
    parent::delete();
  }

164 165 166
  /**
   * {@inheritdoc}
   */
167
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    $fields['id'] = FieldDefinition::create('integer')
      ->setLabel(t('Custom block ID'))
      ->setDescription(t('The custom block ID.'))
      ->setReadOnly(TRUE);

    $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.'))
      ->setReadOnly(TRUE);

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

    $fields['info'] = FieldDefinition::create('string')
      ->setLabel(t('Subject'))
      ->setDescription(t('The custom block name.'));

191
    $fields['type'] = FieldDefinition::create('entity_reference')
192
      ->setLabel(t('Block type'))
193 194
      ->setDescription(t('The block type.'))
      ->setSetting('target_type', 'custom_block_type');
195 196 197 198 199 200 201 202 203 204 205 206

    $fields['log'] = FieldDefinition::create('string')
      ->setLabel(t('Revision log message'))
      ->setDescription(t('The revision log message.'));

    // @todo Convert to a "changed" field in https://drupal.org/node/2145103.
    $fields['changed'] = FieldDefinition::create('integer')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the custom block was last edited.'))
      ->setPropertyConstraints('value', array('EntityChanged' => array()));

    return $fields;
207 208
  }

209 210 211 212 213 214 215
  /**
   * {@inheritdoc}
   */
  public function getChangedTime() {
    return $this->get('changed')->value;
  }

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  /**
   * {@inheritdoc}
   */
  public function getRevisionLog() {
    return $this->get('log')->value;
  }

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

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

239
}