diff --git a/core/modules/config/lib/Drupal/config/ConfigurableBase.php b/core/modules/config/lib/Drupal/config/ConfigurableBase.php index f53be7b..a8bc318 100644 --- a/core/modules/config/lib/Drupal/config/ConfigurableBase.php +++ b/core/modules/config/lib/Drupal/config/ConfigurableBase.php @@ -7,13 +7,12 @@ namespace Drupal\config; -use Drupal\config\ConfigurableInterface; -use Drupal\entity\Entity; +use Drupal\entity\StorableBase; /** * Defines a base configurable entity class. */ -abstract class ConfigurableBase extends Entity implements ConfigurableInterface { +abstract class ConfigurableBase extends StorableBase implements ConfigurableInterface { /** * The original ID of the configurable entity. diff --git a/core/modules/config/lib/Drupal/config/ConfigurableInterface.php b/core/modules/config/lib/Drupal/config/ConfigurableInterface.php index 7d6075b..48a5f85 100644 --- a/core/modules/config/lib/Drupal/config/ConfigurableInterface.php +++ b/core/modules/config/lib/Drupal/config/ConfigurableInterface.php @@ -10,7 +10,7 @@ namespace Drupal\config; /** * Defines the interface common for all configurable entities. */ -interface ConfigurableInterface { +interface ConfigurableInterface extends StorableInterface { /** * Returns the original ID. diff --git a/core/modules/entity/lib/Drupal/entity/Entity.php b/core/modules/entity/lib/Drupal/entity/Entity.php index 1da0cef..6660fc7 100644 --- a/core/modules/entity/lib/Drupal/entity/Entity.php +++ b/core/modules/entity/lib/Drupal/entity/Entity.php @@ -17,269 +17,6 @@ use Drupal\Component\Uuid\Uuid; * This class can be used as-is by simple entity types. Entity types requiring * special handling can extend the class. */ -class Entity implements EntityInterface { +class Entity extends StorableBase implements EntityInterface { - /** - * 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; - - /** - * Indicates whether this is the current revision. - * - * @var bool - */ - protected $isCurrentRevision = TRUE; - - /** - * Constructs a new entity object. - */ - public function __construct(array $values = array(), $entity_type) { - $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::isNew(). - */ - public function isNew() { - return !empty($this->enforceIsNew) || !$this->id(); - } - - /** - * Implements EntityInterface::enforceIsNew(). - */ - public function enforceIsNew($value = TRUE) { - $this->enforceIsNew = $value; - } - - /** - * 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 = FALSE; - $entity_info = $this->entityInfo(); - if (isset($entity_info['label callback']) && function_exists($entity_info['label callback'])) { - $label = $entity_info['label callback']($this->entityType, $this, $langcode); - } - elseif (!empty($entity_info['entity keys']['label']) && isset($this->{$entity_info['entity keys']['label']})) { - $label = $this->{$entity_info['entity keys']['label']}; - } - return $label; - } - - /** - * Implements EntityInterface::uri(). - * - * @see entity_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(); - if (isset($entity_info['bundles'][$bundle]['uri callback'])) { - $uri_callback = $entity_info['bundles'][$bundle]['uri callback']; - } - elseif (isset($entity_info['uri callback'])) { - $uri_callback = $entity_info['uri callback']; - } - 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; - } - } - - /** - * Implements EntityInterface::language(). - */ - public function language() { - // @todo: Check for language.module instead, once Field API language - // handling depends upon it too. - return module_exists('locale') ? language_load($this->langcode) : FALSE; - } - - /** - * Implements EntityInterface::translations(). - */ - public function translations() { - $languages = array(); - $entity_info = $this->entityInfo(); - if ($entity_info['fieldable'] && ($default_language = $this->language())) { - // 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)) { - foreach ($this->$field_name as $langcode => $value) { - $languages[$langcode] = TRUE; - } - } - } - // Remove the default language from the translations. - unset($languages[$default_language->langcode]); - $languages = array_intersect_key(language_list(), $languages); - } - return $languages; - } - - /** - * Implements EntityInterface::get(). - */ - public function get($property_name, $langcode = NULL) { - // Handle fields. - $entity_info = $this->entityInfo(); - if ($entity_info['fieldable'] && field_info_instance($this->entityType, $property_name, $this->bundle())) { - $field = field_info_field($property_name); - $langcode = $this->getFieldLangcode($field, $langcode); - return isset($this->{$property_name}[$langcode]) ? $this->{$property_name}[$langcode] : NULL; - } - else { - // Handle properties being not fields. - // @todo: Add support for translatable properties being not fields. - return isset($this->{$property_name}) ? $this->{$property_name} : NULL; - } - } - - /** - * Implements EntityInterface::set(). - */ - public function set($property_name, $value, $langcode = NULL) { - // Handle fields. - $entity_info = $this->entityInfo(); - if ($entity_info['fieldable'] && field_info_instance($this->entityType, $property_name, $this->bundle())) { - $field = field_info_field($property_name); - $langcode = $this->getFieldLangcode($field, $langcode); - $this->{$property_name}[$langcode] = $value; - } - else { - // Handle properties being not fields. - // @todo: Add support for translatable properties being not fields. - $this->{$property_name} = $value; - } - } - - /** - * Determines the language code to use for accessing a field value in a certain language. - */ - protected function getFieldLangcode($field, $langcode = NULL) { - // Only apply the given langcode if the entity is language-specific. - // Otherwise translatable fields are handled as non-translatable fields. - if (field_is_translatable($this->entityType, $field) && ($default_language = $this->language()) && !language_is_locked($this->langcode)) { - // For translatable fields the values in default language are stored using - // the language code of the default language. - return isset($langcode) ? $langcode : $default_language->langcode; - } - else { - // If there is a langcode defined for this field, just return it. Otherwise - // return LANGUAGE_NOT_SPECIFIED. - return (isset($this->langcode) ? $this->langcode : LANGUAGE_NOT_SPECIFIED); - } - } - - /** - * Implements EntityInterface::save(). - */ - public function save() { - return entity_get_controller($this->entityType)->save($this); - } - - /** - * Implements EntityInterface::delete(). - */ - public function delete() { - if (!$this->isNew()) { - entity_get_controller($this->entityType)->delete(array($this->id())); - } - } - - /** - * Implements EntityInterface::createDuplicate(). - */ - public function createDuplicate() { - $duplicate = clone $this; - $entity_info = $this->entityInfo(); - $this->{$entity_info['entity keys']['id']} = NULL; - - // Check if the entity type supports UUIDs and generate a new one if so. - if (!empty($entity_info['entity keys']['uuid'])) { - $uuid = new Uuid(); - $duplicate->{$entity_info['entity keys']['uuid']} = $uuid->generate(); - } - return $duplicate; - } - - /** - * Implements EntityInterface::entityInfo(). - */ - public function entityInfo() { - return entity_get_info($this->entityType); - } - - /** - * Implements Drupal\entity\EntityInterface::getRevisionId(). - */ - public function getRevisionId() { - return NULL; - } - - /** - * Implements Drupal\entity\EntityInterface::isCurrentRevision(). - */ - public function isCurrentRevision($new_value = NULL) { - $return = $this->isCurrentRevision; - if (isset($new_value)) { - $this->isCurrentRevision = (bool) $new_value; - } - return $return; - } } diff --git a/core/modules/entity/lib/Drupal/entity/EntityInterface.php b/core/modules/entity/lib/Drupal/entity/EntityInterface.php index 0b89e12..1fc1d0a 100644 --- a/core/modules/entity/lib/Drupal/entity/EntityInterface.php +++ b/core/modules/entity/lib/Drupal/entity/EntityInterface.php @@ -10,203 +10,6 @@ namespace Drupal\entity; /** * Defines a common interface for all entity objects. */ -interface EntityInterface { - - /** - * Constructs a new entity object. - * - * @param $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 $entity_type - * The type of the entity to create. - */ - public function __construct(array $values, $entity_type); - - /** - * Returns the entity identifier (the entity's machine name or numeric ID). - * - * @return - * The identifier of the entity, or NULL if the entity does not yet have - * an identifier. - */ - public function id(); - - /** - * Returns whether the entity is new. - * - * Usually an entity is new if no ID exists for it yet. However, entities may - * be enforced to be new with existing IDs too. - * - * @return - * TRUE if the entity is new, or FALSE if the entity has already been saved. - * - * @see Drupal\entity\EntityInterface::enforceIsNew() - */ - public function isNew(); - - /** - * Enforces an entity to be new. - * - * Allows migrations to create entities with pre-defined IDs by forcing the - * entity to be new before saving. - * - * @param bool $value - * (optional) Whether the entity should be forced to be new. Defaults to - * TRUE. - * - * @see Drupal\entity\EntityInterface::isNew() - */ - public function enforceIsNew($value = TRUE); - - /** - * Returns the type of the entity. - * - * @return - * The type of the entity. - */ - public function entityType(); - - /** - * Returns the bundle of the entity. - * - * @return - * The bundle of the entity. Defaults to the entity type if the entity type - * does not make use of different bundles. - */ - public function bundle(); - - /** - * Returns the label of the entity. - * - * @param $langcode - * (optional) The language code of the language that should be used for - * getting the label. If set to NULL, the entity's default language is - * used. - * - * @return - * The label of the entity, or NULL if there is no label defined. - */ - public function label($langcode = NULL); - - /** - * Returns the URI elements of the entity. - * - * @return - * An array containing the 'path' and 'options' keys used to build the URI - * of the entity, and matching the signature of url(). NULL if the entity - * has no URI of its own. - */ - public function uri(); - - /** - * Returns the default language of a language-specific entity. - * - * @return - * The language object of the entity's default language, or FALSE if the - * entity is not language-specific. - * - * @see Drupal\entity\EntityInterface::translations() - */ - public function language(); - - /** - * Returns the languages the entity is translated to. - * - * @return - * An array of language objects, keyed by language codes. - * - * @see Drupal\entity\EntityInterface::language() - */ - public function translations(); - - /** - * Returns the value of an entity property. - * - * @param $property_name - * The name of the property to return; e.g., 'title'. - * @param $langcode - * (optional) If the property is translatable, the language code of the - * language that should be used for getting the property. If set to NULL, - * the entity's default language is being used. - * - * @return - * The property value, or NULL if it is not defined. - * - * @see Drupal\entity\EntityInterface::language() - */ - public function get($property_name, $langcode = NULL); - - /** - * Sets the value of an entity property. - * - * @param $property_name - * The name of the property to set; e.g., 'title'. - * @param $value - * The value to set, or NULL to unset the property. - * @param $langcode - * (optional) If the property is translatable, the language code of the - * language that should be used for getting the property. If set to - * NULL, the entity's default language is being used. - * - * @see Drupal\entity\EntityInterface::language() - */ - public function set($property_name, $value, $langcode = NULL); - - /** - * Saves an entity permanently. - * - * @return - * Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed. - * - * @throws Drupal\entity\EntityStorageException - * In case of failures an exception is thrown. - */ - public function save(); - - /** - * Deletes an entity permanently. - * - * @throws Drupal\entity\EntityStorageException - * In case of failures an exception is thrown. - */ - public function delete(); - - /** - * Creates a duplicate of the entity. - * - * @return Drupal\entity\EntityInterface - * A clone of the current entity with all identifiers unset, so saving - * it inserts a new entity into the storage system. - */ - public function createDuplicate(); - - /** - * Returns the info of the type of the entity. - * - * @see entity_get_info() - */ - public function entityInfo(); - - /** - * Returns the revision identifier of the entity. - * - * @return - * The revision identifier of the entity, or NULL if the entity does not - * have a revision identifier. - */ - public function getRevisionId(); - - /** - * Checks if this entity is the current revision. - * - * @param bool $new_value - * (optional) A Boolean to (re)set the isCurrentRevision flag. - * - * @return bool - * TRUE if the entity is the current revision, FALSE otherwise. If - * $new_value was passed, the previous value is returned. - */ - public function isCurrentRevision($new_value = NULL); +interface EntityInterface extends StorableInterface { } diff --git a/core/modules/entity/lib/Drupal/entity/Entity.php b/core/modules/entity/lib/Drupal/entity/StorableBase.php similarity index 88% copy from core/modules/entity/lib/Drupal/entity/Entity.php copy to core/modules/entity/lib/Drupal/entity/StorableBase.php index 1da0cef..25cc1d7 100644 --- a/core/modules/entity/lib/Drupal/entity/Entity.php +++ b/core/modules/entity/lib/Drupal/entity/StorableBase.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\entity\Entity. + * Definition of Drupal\entity\StorableBase. */ namespace Drupal\entity; @@ -12,12 +12,12 @@ use Drupal\Component\Uuid\Uuid; /** * Defines a base entity class. * - * Default implementation of EntityInterface. + * Default implementation of StorableInterface. * * This class can be used as-is by simple entity types. Entity types requiring * special handling can extend the class. */ -class Entity implements EntityInterface { +abstract class StorableBase implements StorableInterface { /** * The language code of the entity's default language. @@ -59,42 +59,42 @@ class Entity implements EntityInterface { } /** - * Implements EntityInterface::id(). + * Implements StorableInterface::id(). */ public function id() { return isset($this->id) ? $this->id : NULL; } /** - * Implements EntityInterface::isNew(). + * Implements StorableInterface::isNew(). */ public function isNew() { return !empty($this->enforceIsNew) || !$this->id(); } /** - * Implements EntityInterface::enforceIsNew(). + * Implements StorableInterface::enforceIsNew(). */ public function enforceIsNew($value = TRUE) { $this->enforceIsNew = $value; } /** - * Implements EntityInterface::entityType(). + * Implements StorableInterface::entityType(). */ public function entityType() { return $this->entityType; } /** - * Implements EntityInterface::bundle(). + * Implements StorableInterface::bundle(). */ public function bundle() { return $this->entityType; } /** - * Implements EntityInterface::label(). + * Implements StorableInterface::label(). */ public function label($langcode = NULL) { $label = FALSE; @@ -109,7 +109,7 @@ class Entity implements EntityInterface { } /** - * Implements EntityInterface::uri(). + * Implements StorableInterface::uri(). * * @see entity_uri() */ @@ -140,7 +140,7 @@ class Entity implements EntityInterface { } /** - * Implements EntityInterface::language(). + * Implements StorableInterface::language(). */ public function language() { // @todo: Check for language.module instead, once Field API language @@ -149,7 +149,7 @@ class Entity implements EntityInterface { } /** - * Implements EntityInterface::translations(). + * Implements StorableInterface::translations(). */ public function translations() { $languages = array(); @@ -173,7 +173,7 @@ class Entity implements EntityInterface { } /** - * Implements EntityInterface::get(). + * Implements StorableInterface::get(). */ public function get($property_name, $langcode = NULL) { // Handle fields. @@ -191,7 +191,7 @@ class Entity implements EntityInterface { } /** - * Implements EntityInterface::set(). + * Implements StorableInterface::set(). */ public function set($property_name, $value, $langcode = NULL) { // Handle fields. @@ -227,14 +227,14 @@ class Entity implements EntityInterface { } /** - * Implements EntityInterface::save(). + * Implements StorableInterface::save(). */ public function save() { return entity_get_controller($this->entityType)->save($this); } /** - * Implements EntityInterface::delete(). + * Implements StorableInterface::delete(). */ public function delete() { if (!$this->isNew()) { @@ -243,7 +243,7 @@ class Entity implements EntityInterface { } /** - * Implements EntityInterface::createDuplicate(). + * Implements StorableInterface::createDuplicate(). */ public function createDuplicate() { $duplicate = clone $this; @@ -259,21 +259,21 @@ class Entity implements EntityInterface { } /** - * Implements EntityInterface::entityInfo(). + * Implements StorableInterface::entityInfo(). */ public function entityInfo() { return entity_get_info($this->entityType); } /** - * Implements Drupal\entity\EntityInterface::getRevisionId(). + * Implements Drupal\entity\StorableInterface::getRevisionId(). */ public function getRevisionId() { return NULL; } /** - * Implements Drupal\entity\EntityInterface::isCurrentRevision(). + * Implements Drupal\entity\StorableInterface::isCurrentRevision(). */ public function isCurrentRevision($new_value = NULL) { $return = $this->isCurrentRevision; diff --git a/core/modules/entity/lib/Drupal/entity/EntityInterface.php b/core/modules/entity/lib/Drupal/entity/StorableInterface.php similarity index 92% copy from core/modules/entity/lib/Drupal/entity/EntityInterface.php copy to core/modules/entity/lib/Drupal/entity/StorableInterface.php index 0b89e12..d101998 100644 --- a/core/modules/entity/lib/Drupal/entity/EntityInterface.php +++ b/core/modules/entity/lib/Drupal/entity/StorableInterface.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\entity\EntityInterface. + * Definition of Drupal\entity\StorableInterface. */ namespace Drupal\entity; @@ -10,7 +10,7 @@ namespace Drupal\entity; /** * Defines a common interface for all entity objects. */ -interface EntityInterface { +interface StorableInterface { /** * Constructs a new entity object. @@ -41,7 +41,7 @@ interface EntityInterface { * @return * TRUE if the entity is new, or FALSE if the entity has already been saved. * - * @see Drupal\entity\EntityInterface::enforceIsNew() + * @see Drupal\entity\StorableInterface::enforceIsNew() */ public function isNew(); @@ -55,7 +55,7 @@ interface EntityInterface { * (optional) Whether the entity should be forced to be new. Defaults to * TRUE. * - * @see Drupal\entity\EntityInterface::isNew() + * @see Drupal\entity\StorableInterface::isNew() */ public function enforceIsNew($value = TRUE); @@ -106,7 +106,7 @@ interface EntityInterface { * The language object of the entity's default language, or FALSE if the * entity is not language-specific. * - * @see Drupal\entity\EntityInterface::translations() + * @see Drupal\entity\StorableInterface::translations() */ public function language(); @@ -116,7 +116,7 @@ interface EntityInterface { * @return * An array of language objects, keyed by language codes. * - * @see Drupal\entity\EntityInterface::language() + * @see Drupal\entity\StorableInterface::language() */ public function translations(); @@ -133,7 +133,7 @@ interface EntityInterface { * @return * The property value, or NULL if it is not defined. * - * @see Drupal\entity\EntityInterface::language() + * @see Drupal\entity\StorableInterface::language() */ public function get($property_name, $langcode = NULL); @@ -149,7 +149,7 @@ interface EntityInterface { * language that should be used for getting the property. If set to * NULL, the entity's default language is being used. * - * @see Drupal\entity\EntityInterface::language() + * @see Drupal\entity\StorableInterface::language() */ public function set($property_name, $value, $langcode = NULL); @@ -175,7 +175,7 @@ interface EntityInterface { /** * Creates a duplicate of the entity. * - * @return Drupal\entity\EntityInterface + * @return Drupal\entity\StorableInterface * A clone of the current entity with all identifiers unset, so saving * it inserts a new entity into the storage system. */