Entity.php 11.6 KB
Newer Older
1 2 3 4
<?php

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

8
namespace Drupal\Core\Entity;
9

10
use Drupal\Component\Uuid\Uuid;
11
use Drupal\Core\Language\Language;
12
use Drupal\Core\TypedData\ContextAwareInterface;
13
use IteratorAggregate;
14

15 16 17 18 19 20 21 22
/**
 * Defines a base entity class.
 *
 * Default implementation of EntityInterface.
 *
 * This class can be used as-is by simple entity types. Entity types requiring
 * special handling can extend the class.
 */
23
class Entity implements IteratorAggregate, EntityInterface {
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

  /**
   * The language code of the entity's default language.
   *
   * @var string
   */
  public $langcode = LANGUAGE_NOT_SPECIFIED;

  /**
   * The entity type.
   *
   * @var string
   */
  protected $entityType;

  /**
   * Boolean indicating whether the entity should be forced to be new.
   *
   * @var bool
   */
  protected $enforceIsNew;

46 47 48 49 50 51 52
  /**
   * Boolean indicating whether a new revision should be created on save.
   *
   * @var bool
   */
  protected $newRevision = FALSE;

53
  /**
54
   * Indicates whether this is the default revision.
55 56 57
   *
   * @var bool
   */
58
  protected $isDefaultRevision = TRUE;
59 60

  /**
61 62 63 64 65 66 67
   * Constructs an Entity object.
   *
   * @param array $values
   *   An array of values to set, keyed by property name. If the entity type
   *   has bundles, the bundle key has to be specified.
   * @param string $entity_type
   *   The type of the entity to create.
68
   */
69
  public function __construct(array $values, $entity_type) {
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    $this->entityType = $entity_type;
    // Set initial values.
    foreach ($values as $key => $value) {
      $this->$key = $value;
    }
  }

  /**
   * Implements EntityInterface::id().
   */
  public function id() {
    return isset($this->id) ? $this->id : NULL;
  }

  /**
   * Implements EntityInterface::uuid().
   */
  public function uuid() {
    return isset($this->uuid) ? $this->uuid : NULL;
  }

  /**
   * Implements EntityInterface::isNew().
   */
  public function isNew() {
95
    return !empty($this->enforceIsNew) || !$this->id();
96 97
  }

98 99 100 101 102
  /**
   * Implements EntityInterface::isNewRevision().
   */
  public function isNewRevision() {
    $info = $this->entityInfo();
103
    return $this->newRevision || (!empty($info['entity_keys']['revision']) && !$this->getRevisionId());
104 105
  }

106 107 108 109 110 111 112
  /**
   * Implements EntityInterface::enforceIsNew().
   */
  public function enforceIsNew($value = TRUE) {
    $this->enforceIsNew = $value;
  }

113 114 115 116 117 118 119
  /**
   * Implements EntityInterface::setNewRevision().
   */
  public function setNewRevision($value = TRUE) {
    $this->newRevision = $value;
  }

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  /**
   * Implements EntityInterface::entityType().
   */
  public function entityType() {
    return $this->entityType;
  }

  /**
   * Implements EntityInterface::bundle().
   */
  public function bundle() {
    return $this->entityType;
  }

  /**
   * Implements EntityInterface::label().
   */
  public function label($langcode = NULL) {
    $label = NULL;
    $entity_info = $this->entityInfo();
140 141
    if (isset($entity_info['label_callback']) && function_exists($entity_info['label_callback'])) {
      $label = $entity_info['label_callback']($this->entityType, $this, $langcode);
142
    }
143 144
    elseif (!empty($entity_info['entity_keys']['label']) && isset($this->{$entity_info['entity_keys']['label']})) {
      $label = $this->{$entity_info['entity_keys']['label']};
145 146 147 148 149 150 151 152 153 154 155 156
    }
    return $label;
  }

  /**
   * Implements EntityInterface::uri().
   */
  public function uri() {
    $bundle = $this->bundle();
    // A bundle-specific callback takes precedence over the generic one for the
    // entity type.
    $entity_info = $this->entityInfo();
157 158
    if (isset($entity_info['bundles'][$bundle]['uri_callback'])) {
      $uri_callback = $entity_info['bundles'][$bundle]['uri_callback'];
159
    }
160 161
    elseif (isset($entity_info['uri_callback'])) {
      $uri_callback = $entity_info['uri_callback'];
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    }
    else {
      return NULL;
    }

    // Invoke the callback to get the URI. If there is no callback, return NULL.
    if (isset($uri_callback) && function_exists($uri_callback)) {
      $uri = $uri_callback($this);
      // Pass the entity data to url() so that alter functions do not need to
      // look up this entity again.
      $uri['options']['entity_type'] = $this->entityType;
      $uri['options']['entity'] = $this;
      return $uri;
    }
  }

  /**
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
   * Implements EntityInterface::get().
   */
  public function get($property_name, $langcode = NULL) {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
    return isset($this->{$property_name}) ? $this->{$property_name} : NULL;
  }

  /**
   * Implements ComplexDataInterface::set().
   */
  public function set($property_name, $value) {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
    $this->{$property_name} = $value;
  }

  /**
   * Implements ComplexDataInterface::getProperties().
   */
  public function getProperties($include_computed = FALSE) {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
  }

  /**
   * Implements ComplexDataInterface::getPropertyValues().
   */
  public function getPropertyValues() {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
  }

  /**
   * Implements ComplexDataInterface::setPropertyValues().
   */
  public function setPropertyValues($values) {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
  }

  /**
   * Implements ComplexDataInterface::getPropertyDefinition().
   */
  public function getPropertyDefinition($name) {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
  }

  /**
   * Implements ComplexDataInterface::getPropertyDefinitions().
   */
  public function getPropertyDefinitions() {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
  }

  /**
   * Implements ComplexDataInterface::isEmpty().
   */
  public function isEmpty() {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
  }

  /**
   * Implements ComplexDataInterface::getIterator().
   */
  public function getIterator() {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
  }

  /**
   * Implements AccessibleInterface::access().
   */
255 256 257
  public function access($operation = 'view', \Drupal\user\Plugin\Core\Entity\User $account = NULL) {
    $method = $operation . 'Access';
    return entity_access_controller($this->entityType)->$method($this, LANGUAGE_DEFAULT, $account);
258 259 260 261
  }

  /**
   * Implements TranslatableInterface::language().
262 263
   */
  public function language() {
264 265
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
266 267 268 269 270 271
    $language = language_load($this->langcode);
    if (!$language) {
      // Make sure we return a proper language object.
      $language = new Language(array('langcode' => LANGUAGE_NOT_SPECIFIED));
    }
    return $language;
272 273 274
  }

  /**
275 276 277 278 279 280 281 282 283 284
   * Implements TranslatableInterface::getTranslation().
   */
  public function getTranslation($langcode, $strict = TRUE) {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
  }

  /**
   * Returns the languages the entity is translated to.
   *
285 286 287
   * @todo: Remove once all entity types implement the entity field API.
   *   This is deprecated by
   *   Drupal\Core\TypedData\TranslatableInterface::getTranslationLanguages().
288 289
   */
  public function translations() {
290 291 292 293 294 295 296 297 298 299 300
    return $this->getTranslationLanguages(FALSE);
  }

  /**
   * Implements TranslatableInterface::getTranslationLanguages().
   */
  public function getTranslationLanguages($include_default = TRUE) {
    // @todo: Replace by EntityNG implementation once all entity types have been
    // converted to use the entity field API.
    $default_language = $this->language();
    $languages = array($default_language->langcode => $default_language);
301
    $entity_info = $this->entityInfo();
302 303

    if ($entity_info['fieldable']) {
304 305 306 307 308
      // Go through translatable properties and determine all languages for
      // which translated values are available.
      foreach (field_info_instances($this->entityType, $this->bundle()) as $field_name => $instance) {
        $field = field_info_field($field_name);
        if (field_is_translatable($this->entityType, $field) && isset($this->$field_name)) {
309
          foreach (array_filter($this->$field_name) as $langcode => $value)  {
310 311 312 313
            $languages[$langcode] = TRUE;
          }
        }
      }
314 315 316 317
      $languages = array_intersect_key(language_list(LANGUAGE_ALL), $languages);
    }

    if (empty($include_default)) {
318 319 320
      unset($languages[$default_language->langcode]);
    }

321
    return $languages;
322 323 324 325 326 327 328 329 330 331 332 333 334 335
  }

  /**
   * Implements EntityInterface::save().
   */
  public function save() {
    return entity_get_controller($this->entityType)->save($this);
  }

  /**
   * Implements EntityInterface::delete().
   */
  public function delete() {
    if (!$this->isNew()) {
336
      entity_get_controller($this->entityType)->delete(array($this->id() => $this));
337 338 339 340 341 342 343 344 345
    }
  }

  /**
   * Implements EntityInterface::createDuplicate().
   */
  public function createDuplicate() {
    $duplicate = clone $this;
    $entity_info = $this->entityInfo();
346
    $duplicate->{$entity_info['entity_keys']['id']} = NULL;
347 348

    // Check if the entity type supports UUIDs and generate a new one if so.
349
    if (!empty($entity_info['entity_keys']['uuid'])) {
350
      $uuid = new Uuid();
351
      $duplicate->{$entity_info['entity_keys']['uuid']} = $uuid->generate();
352 353 354 355 356 357 358 359 360 361 362 363
    }
    return $duplicate;
  }

  /**
   * Implements EntityInterface::entityInfo().
   */
  public function entityInfo() {
    return entity_get_info($this->entityType);
  }

  /**
364
   * Implements Drupal\Core\Entity\EntityInterface::getRevisionId().
365 366 367 368 369 370
   */
  public function getRevisionId() {
    return NULL;
  }

  /**
371
   * Implements Drupal\Core\Entity\EntityInterface::isDefaultRevision().
372
   */
373 374
  public function isDefaultRevision($new_value = NULL) {
    $return = $this->isDefaultRevision;
375
    if (isset($new_value)) {
376
      $this->isDefaultRevision = (bool) $new_value;
377 378
    }
    return $return;
379
  }
380 381 382 383 384 385 386 387

  /**
   * Implements Drupal\Core\Entity\EntityInterface::getExportProperties().
   */
  public function getExportProperties() {
    return array();
  }

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
  /**
   * Implements Drupal\Core\Entity\EntityInterface::getBCEntity().
   */
  public function getBCEntity() {
    return $this;
  }

  /**
   * Implements Drupal\Core\Entity\EntityInterface::getOriginalEntity().
   */
  public function getOriginalEntity() {
    return $this;
  }

  /**
   * Implements \Drupal\Core\TypedData\ContextAwareInterface::getName().
   */
  public function getName() {
    return NULL;
  }

  /**
   * Implements \Drupal\Core\TypedData\ContextAwareInterface::getRoot().
   */
  public function getRoot() {
    return $this;
  }

  /**
   * Implements \Drupal\Core\TypedData\ContextAwareInterface::getPropertyPath().
   */
  public function getPropertyPath() {
    return '';
  }

  /**
   * Implements \Drupal\Core\TypedData\ContextAwareInterface::getParent().
   */
  public function getParent() {
    return NULL;
  }

  /**
   * Implements \Drupal\Core\TypedData\ContextAwareInterface::setContext().
   */
  public function setContext($name = NULL, ContextAwareInterface $parent = NULL) {
    // As entities are always the root of the tree, we do not need to set any
    // context.
  }
437
}