ConfigEntityBase.php 5.7 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\Core\Config\Entity\ConfigEntityBase.
6 7
 */

8
namespace Drupal\Core\Config\Entity;
9

10
use Drupal\Core\Entity\Entity;
11 12
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Config\ConfigDuplicateUUIDException;
13 14

/**
15
 * Defines a base configuration entity class.
16
 */
17
abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface {
18 19

  /**
20
   * The original ID of the configuration entity.
21
   *
22 23
   * The ID of a configuration entity is a unique string (machine name). When a
   * configuration entity is updated and its machine name is renamed, the
24 25 26 27 28 29
   * original ID needs to be known.
   *
   * @var string
   */
  protected $originalID;

30 31 32 33 34
  /**
   * The enabled/disabled status of the configuration entity.
   *
   * @var bool
   */
35
  public $status = TRUE;
36

37 38 39
  /**
   * Overrides Entity::__construct().
   */
40
  public function __construct(array $values, $entity_type) {
41 42 43
    parent::__construct($values, $entity_type);

    // Backup the original ID, if any.
44 45 46 47
    // Configuration entity IDs are strings, and '0' is a valid ID.
    $original_id = $this->id();
    if ($original_id !== NULL && $original_id !== '') {
      $this->setOriginalID($original_id);
48 49 50 51
    }
  }

  /**
52
   * Implements ConfigEntityInterface::getOriginalID().
53 54 55 56 57 58
   */
  public function getOriginalID() {
    return $this->originalID;
  }

  /**
59
   * Implements ConfigEntityInterface::setOriginalID().
60
   */
61 62
  public function setOriginalID($id) {
    $this->originalID = $id;
63 64 65
  }

  /**
66
   * Overrides Entity::isNew().
67
   *
68 69 70
   * EntityInterface::enforceIsNew() is only supported for newly created
   * configuration entities but has no effect after saving, since each
   * configuration entity is unique.
71
   */
72
  public function isNew() {
73
    return !empty($this->enforceIsNew);
74 75 76 77 78 79
  }

  /**
   * Overrides Entity::get().
   *
   * EntityInterface::get() implements support for fieldable entities, but
80
   * configuration entities are not fieldable.
81 82 83 84 85 86 87 88 89 90
   */
  public function get($property_name, $langcode = NULL) {
    // @todo: Add support for translatable properties being not fields.
    return isset($this->{$property_name}) ? $this->{$property_name} : NULL;
  }

  /**
   * Overrides Entity::set().
   *
   * EntityInterface::set() implements support for fieldable entities, but
91
   * configuration entities are not fieldable.
92
   */
93
  public function set($property_name, $value, $langcode = NULL, $notify = TRUE) {
94 95 96 97
    // @todo: Add support for translatable properties being not fields.
    $this->{$property_name} = $value;
  }

98 99 100 101
  /**
   * Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::enable().
   */
  public function enable() {
102
    return $this->setStatus(TRUE);
103 104 105 106 107 108
  }

  /**
   * Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::disable().
   */
  public function disable() {
109 110 111 112 113 114
    return $this->setStatus(FALSE);
  }

  /**
   * Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::setStatus().
   */
115 116
  public function setStatus($status) {
    $this->status = (bool) $status;
117 118 119 120 121 122 123 124 125 126
    return $this;
  }

  /**
   * Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::status().
   */
  public function status() {
    return !empty($this->status);
  }

127 128 129 130 131 132 133 134 135 136
  /**
   * Overrides Entity::createDuplicate().
   */
  public function createDuplicate() {
    $duplicate = parent::createDuplicate();
    // Prevent the new duplicate from being misinterpreted as a rename.
    $duplicate->setOriginalID(NULL);
    return $duplicate;
  }

137
  /**
138
   * Helper callback for uasort() to sort configuration entities by weight and label.
139 140 141 142 143 144 145 146 147 148 149
   */
  public static function sort($a, $b) {
    $a_weight = isset($a->weight) ? $a->weight : 0;
    $b_weight = isset($b->weight) ? $b->weight : 0;
    if ($a_weight == $b_weight) {
      $a_label = $a->label();
      $b_label = $b->label();
      return strnatcasecmp($a_label, $b_label);
    }
    return ($a_weight < $b_weight) ? -1 : 1;
  }
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

  /**
   * Overrides \Drupal\Core\Entity\Entity::getExportProperties().
   */
  public function getExportProperties() {
    // Configuration objects do not have a schema. Extract all key names from
    // class properties.
    $class_info = new \ReflectionClass($this);
    $properties = array();
    foreach ($class_info->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
      $name = $property->getName();
      $properties[$name] = $this->get($name);
    }
    return $properties;
  }
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageControllerInterface $storage_controller) {
    parent::preSave($storage_controller);

    // Ensure this entity's UUID does not exist with a different ID, regardless
    // of whether it's new or updated.
    $matching_entities = $storage_controller->getQuery()
      ->condition('uuid', $this->uuid())
      ->execute();
    $matched_entity = reset($matching_entities);
    if (!empty($matched_entity) && ($matched_entity != $this->id())) {
      throw new ConfigDuplicateUUIDException(format_string('Attempt to save a configuration entity %id with UUID %uuid when this UUID is already used for %matched', array('%id' => $this->id(), '%uuid' => $this->uuid(), '%matched' => $matched_entity)));
    }

    if (!$this->isNew()) {
      $original = $storage_controller->loadUnchanged($this->id());
      // Ensure that the UUID cannot be changed for an existing entity.
      if ($original && ($original->uuid() != $this->uuid())) {
        throw new ConfigDuplicateUUIDException(format_string('Attempt to save a configuration entity %id with UUID %uuid when this entity already exists with UUID %original_uuid', array('%id' => $this->id(), '%uuid' => $this->uuid(), '%original_uuid' => $original->uuid())));
      }
    }
  }

191 192 193 194 195 196 197
  /**
   * {@inheritdoc}
   */
  public function uri() {
    return parent::uri('edit-form');
  }

198
}