diff --git a/core/lib/Drupal/Core/Entity/EntityCompatibilityDecorator.php b/core/lib/Drupal/Core/Entity/EntityCompatibilityDecorator.php new file mode 100644 index 0000000..5977852 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/EntityCompatibilityDecorator.php @@ -0,0 +1,254 @@ +decorated = $decorated; + } + + /** + * Forwards direct access to entity properties to regular entity getters. + */ + function __get($name) { + return $this->getPropertyDefinition($name) ? $this->decorated->get($name) : $this->decorated->__get($name); + } + + /** + * Forwards direct access to entity properties to regular entity setters. + */ + function __set($name, $value) { + return $this->getPropertyDefinition($name) ? $this->decorated->set($name, $value) : $this->decorated->__set($name, $value); + } + + /** + * Forwards the call to the decorated entity. + */ + public function access(\Drupal\user\User $account = NULL) { + return $this->decorated->access($account); + } + + /** + * Forwards the call to the decorated entity. + */ + public function get($property_name) { + return $this->decorated->get($property_name); + } + + /** + * Forwards the call to the decorated entity. + */ + public function set($property_name, $value) { + return $this->decorated->set($property_name, $value); + } + + /** + * Forwards the call to the decorated entity. + */ + public function getProperties($include_computed = FALSE) { + return $this->decorated->getProperties($include_computed); + } + + /** + * Forwards the call to the decorated entity. + */ + public function getPropertyValues() { + return $this->decorated->getPropertyValues(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function setPropertyValues($values) { + return $this->decorated->setPropertyValues($values); + } + + /** + * Forwards the call to the decorated entity. + */ + public function getPropertyDefinition($name) { + return $this->decorated->getPropertyDefinition($name); + } + + /** + * Forwards the call to the decorated entity. + */ + public function getPropertyDefinitions() { + return $this->decorated->getPropertyDefinitions(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function isEmpty() { + return $this->decorated->isEmpty(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function getIterator() { + return $this->decorated->getIterator(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function id() { + return $this->decorated->id(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function uuid() { + return $this->decorated->uuid(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function isNew() { + return $this->decorated->isNew(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function isNewRevision() { + return $this->decorated->isNewRevision(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function setNewRevision($value = TRUE) { + return $this->decorated->setNewRevision($value); + } + + /** + * Forwards the call to the decorated entity. + */ + public function enforceIsNew($value = TRUE) { + return $this->decorated->enforceIsNew($value); + } + + /** + * Forwards the call to the decorated entity. + */ + public function entityType() { + return $this->decorated->entityType(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function bundle() { + return $this->decorated->bundle(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function label($langcode = NULL) { + return $this->decorated->label(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function uri() { + return $this->decorated->uri(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function save() { + return $this->decorated->save(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function delete() { + return $this->decorated->delete(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function createDuplicate() { + return $this->decorated->createDuplicate(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function entityInfo() { + return $this->decorated->entityInfo(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function getRevisionId() { + return $this->decorated->getRevisionId(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function isDefaultRevision($new_value = NULL) { + return $this->decorated->isDefaultRevision(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function language() { + return $this->decorated->language(); + } + + /** + * Forwards the call to the decorated entity. + */ + public function getTranslationLanguages($include_default = TRUE) { + return $this->decorated->getTranslationLanguages($include_default); + } + + /** + * Forwards the call to the decorated entity. + */ + public function getTranslation($langcode, $strict = TRUE) { + return $this->decorated->getTranslation($langcode, $strict); + } +} diff --git a/core/lib/Drupal/Core/Entity/EntityNG.php b/core/lib/Drupal/Core/Entity/EntityNG.php index 2fc7764..736db60 100644 --- a/core/lib/Drupal/Core/Entity/EntityNG.php +++ b/core/lib/Drupal/Core/Entity/EntityNG.php @@ -421,4 +421,23 @@ public function __clone() { } } } + + /** + * Interim helper for getting an entity object working with the new field API. + * + * If the entity is in compatibility mode, this helper returns an instance + * of a decorator class, such that the returned entity still supports using + * the new entity field API via the usual magic getters. + * + * @return \Drupal\Core\Entity\EntityInterface + * An entity support the new entity field API via the magic getters/setters. + */ + public function ng() { + if ($this->compatibilityMode) { + return new EntityCompatibilityDecorator($this); + } + else { + return $this; + } + } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php index a152bfa..77ccecc 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php @@ -249,6 +249,15 @@ public function testReadWrite() { )); $this->assertNotNull($entity->user_id->value, 'User id is not NULL'); $this->assertIdentical($entity->user_id->value, 0, 'User id has been set to 0'); + + // Test using the API while compatibility mode is enabled. + $entity->name->value = 'foo'; + $entity->setCompatibilityMode(TRUE); + $entity_ng = $entity->ng(); + $this->assertEqual($entity_ng->name->value, 'foo', 'Using getter while compatibility mode is enabled.'); + + $entity_ng->name = array('value' => 'bar'); + $this->assertEqual($entity_ng->name->value, 'bar', 'Using setter while compatibility mode is enabled.'); } /**