ConfigEntityBase.php 2.62 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

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

  /**
18
   * The original ID of the configuration entity.
19
   *
20
21
   * 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
22
23
24
25
26
27
28
29
30
   * original ID needs to be known.
   *
   * @var string
   */
  protected $originalID;

  /**
   * Overrides Entity::__construct().
   */
31
  public function __construct(array $values, $entity_type) {
32
33
34
35
36
37
38
39
40
    parent::__construct($values, $entity_type);

    // Backup the original ID, if any.
    if ($original_id = $this->id()) {
      $this->originalID = $original_id;
    }
  }

  /**
41
   * Implements ConfigEntityInterface::getOriginalID().
42
43
44
45
46
47
48
49
   */
  public function getOriginalID() {
    return $this->originalID;
  }

  /**
   * Overrides Entity::isNew().
   *
50
51
   * EntityInterface::enforceIsNew() is not supported by configuration entities,
   * since each configuration entity is unique.
52
53
54
55
56
57
58
59
   */
  final public function isNew() {
    return !$this->id();
  }

  /**
   * Overrides Entity::bundle().
   *
60
61
   * EntityInterface::bundle() is not supported by configuration entities, since
   * a configuration entity is a bundle.
62
63
64
65
66
67
68
69
70
   */
  final public function bundle() {
    return $this->entityType;
  }

  /**
   * Overrides Entity::get().
   *
   * EntityInterface::get() implements support for fieldable entities, but
71
   * configuration entities are not fieldable.
72
73
74
75
76
77
78
79
80
81
   */
  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
82
   * configuration entities are not fieldable.
83
84
85
86
87
88
89
   */
  public function set($property_name, $value, $langcode = NULL) {
    // @todo: Add support for translatable properties being not fields.
    $this->{$property_name} = $value;
  }

  /**
90
   * Helper callback for uasort() to sort configuration entities by weight and label.
91
92
93
94
95
96
97
98
99
100
101
102
   */
  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;
  }
}