diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index a02c803..54ba225 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -353,8 +353,8 @@ protected function getTranslatedField($name, $langcode) { if (isset($this->values[$name][$langcode])) { $value = $this->values[$name][$langcode]; } - $typed_entity = $this->getTypedData(); - $field = \Drupal::typedDataManager()->getPropertyInstance($typed_entity, $name, $value); + $entity_adapter = $this->getTypedData(); + $field = \Drupal::typedDataManager()->getPropertyInstance($entity_adapter, $name, $value); if ($default) { // $this->defaultLangcode might not be set if we are initializing the // default language code cache, in which case there is no valid diff --git a/core/lib/Drupal/Core/Entity/Plugin/DataType/Entity.php b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php similarity index 98% rename from core/lib/Drupal/Core/Entity/Plugin/DataType/Entity.php rename to core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php index 17f50f6..6e55e5d 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/DataType/Entity.php +++ b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityAdapter.php @@ -32,7 +32,7 @@ * definition_class = "\Drupal\Core\Entity\TypedData\EntityDataDefinition" * ) */ -class Entity extends TypedData implements \IteratorAggregate, ComplexDataInterface { +class EntityAdapter extends TypedData implements \IteratorAggregate, ComplexDataInterface { /** * The wrapped entity object. diff --git a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php index 5cb0430..3ecce46 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php +++ b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php @@ -60,7 +60,7 @@ public function getTargetDefinition() { */ public function getTarget() { if (!isset($this->target) && isset($this->id)) { - // If we have a valid reference, return the typed entity object. + // If we have a valid reference, return the entity's TypedData adapter. $entity = entity_load($this->getTargetDefinition()->getEntityTypeId(), $this->id); $this->target = isset($entity) ? $entity->getTypedData() : NULL; } diff --git a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraintValidator.php b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraintValidator.php index 6bc55d2..746eb95 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraintValidator.php +++ b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/BundleConstraintValidator.php @@ -19,16 +19,18 @@ class BundleConstraintValidator extends ConstraintValidator { /** * {@inheritdoc} */ - public function validate($typed_entity, Constraint $constraint) { - if (!isset($typed_entity)) { + public function validate($entity, Constraint $constraint) { + if (!isset($entity)) { return; } // The constraint is used on both entity references and typed data entity - // objects. While entities are not automatically unwrapped, entity - // references are. - $entity = $typed_entity instanceof TypedDataInterface ? $typed_entity->getValue() : $typed_entity; + // adapters. For the latter, we need to unwrap the actual entity. + if ($entity instanceof TypedDataInterface) { + $entity = $entity->getValue(); + } if (!in_array($entity->bundle(), $constraint->getBundleOption())) { $this->context->addViolation($constraint->message, array('%bundle' => implode(', ', $constraint->getBundleOption()))); } } + } diff --git a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityTypeConstraintValidator.php b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityTypeConstraintValidator.php index a307b24..a74f20d 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityTypeConstraintValidator.php +++ b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityTypeConstraintValidator.php @@ -19,17 +19,19 @@ class EntityTypeConstraintValidator extends ConstraintValidator { /** * Implements \Symfony\Component\Validator\ConstraintValidatorInterface::validate(). */ - public function validate($typed_entity, Constraint $constraint) { - if (!isset($typed_entity)) { + public function validate($entity, Constraint $constraint) { + if (!isset($entity)) { return; } // The constraint is used on both entity references and typed data entity - // objects. While entities are not automatically unwrapped, entity - // references are. - $entity = $typed_entity instanceof TypedDataInterface ? $typed_entity->getValue() : $typed_entity; + // adapters. For the latter, we need to unwrap the actual entity. + if ($entity instanceof TypedDataInterface) { + $entity = $entity->getValue(); + } /** @var $entity \Drupal\Core\Entity\EntityInterface */ if ($entity->getEntityTypeId() != $constraint->type) { $this->context->addViolation($constraint->message, array('%type' => $constraint->type)); } } + } diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php index fad286d..5b21b6b 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php @@ -1632,8 +1632,8 @@ protected function readFieldItemsToPurge(FieldDefinitionInterface $field_definit // Create field item objects and return. foreach ($items_by_entity as $revision_id => $values) { - $typed_entity = $entities[$revision_id]->getTypedData(); - $items_by_entity[$revision_id] = \Drupal::typedDataManager()->create($field_definition, $values, $field_definition->getName(), $typed_entity); + $entity_adapter = $entities[$revision_id]->getTypedData(); + $items_by_entity[$revision_id] = \Drupal::typedDataManager()->create($field_definition, $values, $field_definition->getName(), $entity_adapter); } return $items_by_entity; } diff --git a/core/modules/system/src/Tests/Entity/EntityFieldTest.php b/core/modules/system/src/Tests/Entity/EntityFieldTest.php index de00520..dd9b2ef 100644 --- a/core/modules/system/src/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/src/Tests/Entity/EntityFieldTest.php @@ -416,11 +416,11 @@ protected function checkIntrospection($entity_type) { $this->assertEqual($textfield_properties['processed']->getDataType(), 'string', $entity_type .': String processed property of the test-text field found.'); // Make sure provided contextual information is right. - $typed_entity = $entity->getTypedData(); - $this->assertIdentical($typed_entity->getRoot(), $typed_entity, 'Entity is root object.'); - $this->assertEqual($typed_entity->getPropertyPath(), ''); - $this->assertEqual($typed_entity->getName(), ''); - $this->assertEqual($typed_entity->getParent(), NULL); + $entity_adapter = $entity->getTypedData(); + $this->assertIdentical($entity_adapter->getRoot(), $entity_adapter, 'Entity is root object.'); + $this->assertEqual($entity_adapter->getPropertyPath(), ''); + $this->assertEqual($entity_adapter->getName(), ''); + $this->assertEqual($entity_adapter->getParent(), NULL); $field = $entity->user_id; $this->assertIdentical($field->getRoot()->getValue(), $entity, 'Entity is root object.'); diff --git a/core/tests/Drupal/Tests/Core/Entity/TypedData/TypedEntityUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php similarity index 81% rename from core/tests/Drupal/Tests/Core/Entity/TypedData/TypedEntityUnitTest.php rename to core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php index 34a5528..74b9b44 100644 --- a/core/tests/Drupal/Tests/Core/Entity/TypedData/TypedEntityUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php @@ -2,14 +2,14 @@ /** * @file - * Contains \Drupal\Tests\Core\Entity\TypedEntityUnitTest. + * Contains \Drupal\Tests\Core\Entity\EntityAdapterUnitTest. */ namespace Drupal\Tests\Core\Entity\TypedData; use Drupal\Core\Access\AccessResult; use Drupal\Core\DependencyInjection\ContainerBuilder; -use Drupal\Core\Entity\Plugin\DataType\Entity; +use Drupal\Core\Entity\Plugin\DataType\EntityAdapter; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Language\LanguageInterface; @@ -17,11 +17,11 @@ use Drupal\Core\Language\Language; /** - * @coversDefaultClass \Drupal\Core\Entity\Plugin\DataType\Entity + * @coversDefaultClass \Drupal\Core\Entity\Plugin\DataType\EntityAdapter * @group Entity * @group TypedData */ -class TypedEntityUnitTest extends UnitTestCase { +class EntityAdapterUnitTest extends UnitTestCase { /** * The bundle used for testing. @@ -38,11 +38,11 @@ class TypedEntityUnitTest extends UnitTestCase { protected $entity; /** - * The typed entity under test. + * The entity adapter under test. * - * @var \Drupal\Core\Entity\Plugin\DataType\Entity + * @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter */ - protected $typedEntity; + protected $entityAdapter; /** * The entity type used for testing. @@ -209,42 +209,42 @@ protected function setUp() { ->will($this->returnValue($this->fieldDefinitions)); $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', array($values, $this->entityTypeId, $this->bundle)); - $this->typedEntity = Entity::createFromEntity($this->entity); + $this->entityAdapter = EntityAdapter::createFromEntity($this->entity); } /** * @covers ::getConstraints */ public function testGetConstraints() { - $this->assertInternalType('array', $this->typedEntity->getConstraints()); + $this->assertInternalType('array', $this->entityAdapter->getConstraints()); } /** * @covers ::getName */ public function testGetName() { - $this->assertNull($this->typedEntity->getName()); + $this->assertNull($this->entityAdapter->getName()); } /** * @covers ::getRoot */ public function testGetRoot() { - $this->assertSame(spl_object_hash($this->typedEntity), spl_object_hash($this->typedEntity->getRoot())); + $this->assertSame(spl_object_hash($this->entityAdapter), spl_object_hash($this->entityAdapter->getRoot())); } /** * @covers ::getPropertyPath */ public function testGetPropertyPath() { - $this->assertSame('', $this->typedEntity->getPropertyPath()); + $this->assertSame('', $this->entityAdapter->getPropertyPath()); } /** * @covers ::getParent */ public function testGetParent() { - $this->assertNull($this->typedEntity->getParent()); + $this->assertNull($this->entityAdapter->getParent()); } /** @@ -254,31 +254,31 @@ public function testSetContext() { $name = $this->randomMachineName(); $parent = $this->getMock('\Drupal\Core\TypedData\TypedDataInterface'); // Our mocked entity->setContext() returns NULL, so assert that. - $this->assertNull($this->typedEntity->setContext($name, $parent)); - $this->assertEquals($name, $this->typedEntity->getName()); - $this->assertEquals($parent, $this->typedEntity->getParent()); + $this->assertNull($this->entityAdapter->setContext($name, $parent)); + $this->assertEquals($name, $this->entityAdapter->getName()); + $this->assertEquals($parent, $this->entityAdapter->getParent()); } /** * @covers ::getValue */ public function testGetValue() { - $this->assertEquals($this->entity, $this->typedEntity->getValue()); + $this->assertEquals($this->entity, $this->entityAdapter->getValue()); } /** * @covers ::setValue */ public function testSetValue() { - $this->typedEntity->setValue(NULL); - $this->assertNull($this->typedEntity->getValue()); + $this->entityAdapter->setValue(NULL); + $this->assertNull($this->entityAdapter->getValue()); } /** * @covers ::get */ public function testGet() { - $this->assertInstanceOf('\Drupal\Core\Field\FieldItemListInterface', $this->typedEntity->get('id')); + $this->assertInstanceOf('\Drupal\Core\Field\FieldItemListInterface', $this->entityAdapter->get('id')); } /** @@ -286,7 +286,7 @@ public function testGet() { * @expectedException \InvalidArgumentException */ public function testGetInvalidField() { - $this->typedEntity->get('invalid'); + $this->entityAdapter->get('invalid'); } /** @@ -294,8 +294,8 @@ public function testGetInvalidField() { * @expectedException \Drupal\Core\TypedData\Exception\MissingDataException */ public function testGetWithoutData() { - $this->typedEntity->setValue(NULL); - $this->typedEntity->get('id'); + $this->entityAdapter->setValue(NULL); + $this->entityAdapter->get('id'); } /** @@ -308,7 +308,7 @@ public function testSet() { ->method('setValue') ->with($id_items); - $this->typedEntity->set('id', $id_items); + $this->entityAdapter->set('id', $id_items); } /** @@ -316,16 +316,16 @@ public function testSet() { * @expectedException \Drupal\Core\TypedData\Exception\MissingDataException */ public function testSetWithoutData() { - $this->typedEntity->setValue(NULL); + $this->entityAdapter->setValue(NULL); $id_items = [ array('value' => $this->id + 1) ]; - $this->typedEntity->set('id', $id_items); + $this->entityAdapter->set('id', $id_items); } /** * @covers ::getProperties */ public function testGetProperties() { - $fields = $this->typedEntity->getProperties(); + $fields = $this->entityAdapter->getProperties(); $this->assertInstanceOf('Drupal\Core\Field\FieldItemListInterface', $fields['id']); $this->assertInstanceOf('Drupal\Core\Field\FieldItemListInterface', $fields['revision_id']); } @@ -334,7 +334,7 @@ public function testGetProperties() { * @covers ::toArray */ public function testToArray() { - $array = $this->typedEntity->toArray(); + $array = $this->entityAdapter->toArray(); // Mock field objects return NULL values, so test keys only. $this->assertArrayHasKey('id', $array); $this->assertArrayHasKey('revision_id', $array); @@ -346,17 +346,17 @@ public function testToArray() { * @expectedException \Drupal\Core\TypedData\Exception\MissingDataException */ public function testToArrayWithoutData() { - $this->typedEntity->setValue(NULL); - $this->typedEntity->toArray(); + $this->entityAdapter->setValue(NULL); + $this->entityAdapter->toArray(); } /** * @covers ::isEmpty */ public function testIsEmpty() { - $this->assertFalse($this->typedEntity->isEmpty()); - $this->typedEntity->setValue(NULL); - $this->assertTrue($this->typedEntity->isEmpty()); + $this->assertFalse($this->entityAdapter->isEmpty()); + $this->entityAdapter->setValue(NULL); + $this->assertTrue($this->entityAdapter->isEmpty()); } /** @@ -368,15 +368,15 @@ public function testOnChange() { ->method('onChange') ->with('foo') ->willReturn(NULL); - $this->typedEntity->setValue($entity); - $this->typedEntity->onChange('foo'); + $this->entityAdapter->setValue($entity); + $this->entityAdapter->onChange('foo'); } /** * @covers ::getDataDefinition */ public function testGetDataDefinition() { - $definition = $this->typedEntity->getDataDefinition(); + $definition = $this->entityAdapter->getDataDefinition(); $this->assertInstanceOf('\Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface', $definition); $this->assertEquals($definition->getEntityTypeId(), $this->entityTypeId); $this->assertEquals($definition->getBundles(), [ $this->bundle ]); @@ -390,10 +390,10 @@ public function testGetString() { $entity->expects($this->once()) ->method('label') ->willReturn('foo'); - $this->typedEntity->setValue($entity); - $this->assertEquals('foo', $this->typedEntity->getString()); - $this->typedEntity->setValue(NULL); - $this->assertEquals('', $this->typedEntity->getString()); + $this->entityAdapter->setValue($entity); + $this->assertEquals('foo', $this->entityAdapter->getString()); + $this->entityAdapter->setValue(NULL); + $this->assertEquals('', $this->entityAdapter->getString()); } /** @@ -403,20 +403,20 @@ public function testApplyDefaultValue() { // For each field on the entity the mock method has to be invoked once. $this->fieldItemList->expects($this->exactly(2)) ->method('applyDefaultValue'); - $this->typedEntity->applyDefaultValue(); + $this->entityAdapter->applyDefaultValue(); } /** * @covers ::getIterator */ public function testGetIterator() { - $iterator = $this->typedEntity->getIterator(); + $iterator = $this->entityAdapter->getIterator(); $fields = iterator_to_array($iterator); $this->assertArrayHasKey('id', $fields); $this->assertArrayHasKey('revision_id', $fields); $this->assertEquals(count($fields), 2); - $this->typedEntity->setValue(NULL); - $this->assertEquals(new \ArrayIterator([]), $this->typedEntity->getIterator()); + $this->entityAdapter->setValue(NULL); + $this->assertEquals(new \ArrayIterator([]), $this->entityAdapter->getIterator()); } }