diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 292de7a..1ae9772 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -395,13 +395,13 @@ public function set($name, $value, $notify = TRUE) { * {@inheritdoc} */ public function getFields($include_computed = FALSE) { - $properties = array(); + $fields = array(); foreach ($this->getFieldDefinitions() as $name => $definition) { if ($include_computed || !$definition->isComputed()) { - $properties[$name] = $this->get($name); + $fields[$name] = $this->get($name); } } - return $properties; + return $fields; } /** diff --git a/core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php b/core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php index 860bc86..819a255 100644 --- a/core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php +++ b/core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Entity; use Drupal\Component\Utility\String; +use Drupal\Core\Entity\Plugin\DataType\EntityTypedDataWrapper; use Drupal\Core\Field\PrepareCacheInterface; use Drupal\field\FieldConfigInterface; use Drupal\field\FieldInstanceConfigInterface; @@ -341,7 +342,7 @@ public function onBundleDelete($bundle) { } */ public function onFieldItemsPurge(EntityInterface $entity, FieldInstanceConfigInterface $instance) { if ($values = $this->readFieldItemsToPurge($entity, $instance)) { - $items = \Drupal::typedDataManager()->create($instance, $values, $instance->getName(), $entity); + $items = \Drupal::typedDataManager()->create($instance, $values, $instance->getName(), new EntityTypedDataWrapper($entity)); $items->delete(); } $this->purgeFieldItems($entity, $instance); diff --git a/core/lib/Drupal/Core/Field/FieldItemInterface.php b/core/lib/Drupal/Core/Field/FieldItemInterface.php index ae7bf6c..31d403c 100644 --- a/core/lib/Drupal/Core/Field/FieldItemInterface.php +++ b/core/lib/Drupal/Core/Field/FieldItemInterface.php @@ -86,7 +86,7 @@ public static function schema(FieldDefinitionInterface $field_definition); /** * Gets the entity that field belongs to. * - * @return \Drupal\Core\Entity\EntityInterface + * @return \Drupal\Core\Entity\ContentEntityInterface * The entity object. */ public function getEntity(); diff --git a/core/lib/Drupal/Core/TypedData/TranslatableInterface.php b/core/lib/Drupal/Core/TypedData/TranslatableInterface.php index af66bfe..fa75f4f 100644 --- a/core/lib/Drupal/Core/TypedData/TranslatableInterface.php +++ b/core/lib/Drupal/Core/TypedData/TranslatableInterface.php @@ -45,7 +45,7 @@ public function getTranslationLanguages($include_default = TRUE); * The language code of the translation to get or Language::LANGCODE_DEFAULT * to get the data in default language. * - * @return \Drupal\Core\TypedData\TypedDataInterface + * @return $this * A typed data object for the translated data. */ public function getTranslation($langcode); @@ -53,7 +53,7 @@ public function getTranslation($langcode); /** * Returns the translatable object referring to the original language. * - * @return \Drupal\Core\TypedData\TranslatableInterface + * @return $this * The translation object referring to the original language. */ public function getUntranslated(); @@ -78,7 +78,7 @@ public function hasTranslation($langcode); * (optional) An array of initial values to be assigned to the translatable * fields. Defaults to none. * - * @return \Drupal\Core\TypedData\TranslatableInterface + * @return $this */ public function addTranslation($langcode, array $values = array()); diff --git a/core/modules/content_translation/content_translation.pages.inc b/core/modules/content_translation/content_translation.pages.inc index ac36ff7..27abe9f 100644 --- a/core/modules/content_translation/content_translation.pages.inc +++ b/core/modules/content_translation/content_translation.pages.inc @@ -229,6 +229,6 @@ function content_translation_edit_page(EntityInterface $entity, Language $langua function content_translation_prepare_translation(EntityInterface $entity, Language $source, Language $target) { if ($entity instanceof ContentEntityInterface) { $source_translation = $entity->getTranslation($source->id); - $entity->addTranslation($target->id, $source_translation->getPropertyValues()); + $entity->addTranslation($target->id, $source_translation->getFieldValues()); } } diff --git a/core/modules/hal/hal.services.yml b/core/modules/hal/hal.services.yml index f23f8b8..8a28cc8 100644 --- a/core/modules/hal/hal.services.yml +++ b/core/modules/hal/hal.services.yml @@ -18,7 +18,7 @@ services: - { name: normalizer, priority: 20 } arguments: ['@entity.manager', '@http_default_client', '@rest.link_manager'] serializer.normalizer.entity.hal: - class: Drupal\hal\Normalizer\EntityNormalizer + class: Drupal\hal\Normalizer\ContentEntityNormalizer arguments: ['@rest.link_manager'] tags: - { name: normalizer, priority: 10 } diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/ContentEntityNormalizer.php similarity index 90% rename from core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php rename to core/modules/hal/lib/Drupal/hal/Normalizer/ContentEntityNormalizer.php index 01f2f01..8a1cfd0 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/ContentEntityNormalizer.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\hal\Normalizer\EntityNormalizer. + * Contains \Drupal\hal\Normalizer\ContentEntityNormalizer. */ namespace Drupal\hal\Normalizer; @@ -15,14 +15,14 @@ /** * Converts the Drupal entity object structure to a HAL array structure. */ -class EntityNormalizer extends NormalizerBase { +class ContentEntityNormalizer extends NormalizerBase { /** * The interface or class that this Normalizer supports. * * @var string */ - protected $supportedInterfaceOrClass = 'Drupal\Core\Entity\EntityInterface'; + protected $supportedInterfaceOrClass = 'Drupal\Core\Entity\ContentEntityInterface'; /** * The hypermedia link manager. @@ -32,7 +32,7 @@ class EntityNormalizer extends NormalizerBase { protected $linkManager; /** - * Constructs an EntityNormalizer object. + * Constructs an ContentEntityNormalizer object. * * @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager * The hypermedia link manager. @@ -61,16 +61,20 @@ public function normalize($entity, $format = NULL, array $context = array()) { // If the properties to use were specified, only output those properties. // Otherwise, output all properties except internal ID. if (isset($context['included_fields'])) { - $properties = array(); - foreach ($context['included_fields'] as $property_name) { - $properties[] = $entity->get($property_name); + $fields = array(); + foreach ($context['included_fields'] as $field_name) { + $fields[] = $entity->get($field_name); } } else { - $properties = $entity->getFields(); + $fields = $entity->getFields(); } - foreach ($properties as $property) { - $normalized_property = $this->serializer->normalize($property, $format, $context); + foreach ($fields as $field) { + // Ignore the entity ID. + if ($field->getFieldDefinition()->getName() == $entity->getEntityType()->getKey('id')) { + continue; + } + $normalized_property = $this->serializer->normalize($field, $format, $context); $normalized = NestedArray::mergeDeep($normalized, $normalized_property); } diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php index 1ffc72f..e4f8162 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php @@ -7,6 +7,7 @@ namespace Drupal\hal\Normalizer; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\rest\LinkManager\LinkManagerInterface; use Drupal\serialization\EntityResolver\EntityResolverInterface; use Drupal\serialization\EntityResolver\UuidReferenceInterface; @@ -51,6 +52,18 @@ public function __construct(LinkManagerInterface $link_manager, EntityResolverIn } /** + * {@inheritdoc} + */ + public function supportsNormalization($data, $format = NULL) { + // Normalization is only supported if the referenced entity is a content + // entity. + if (parent::supportsNormalization($data, $format)) { + return $data->entity instanceof ContentEntityInterface; + } + return FALSE; + } + + /** * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize() */ public function normalize($field_item, $format = NULL, array $context = array()) { diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/FieldItemNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/FieldItemNormalizer.php index 44e6787..2a14f9c 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/FieldItemNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/FieldItemNormalizer.php @@ -99,28 +99,19 @@ protected function constructValue($data, $context) { * The translated field item instance. */ protected function createTranslatedInstance(FieldItemInterface $field_item, $langcode) { - $parent = $field_item->getParent(); - $ancestors = array(); + $field_items = $field_item->getParent(); // Remove the untranslated instance from the field's list of items. - $parent->offsetUnset($field_item->getName()); + $field_items->offsetUnset($field_item->getName()); - // Get the property path. - while (!method_exists($parent, 'getTranslation')) { - array_unshift($ancestors, $parent); - $parent = $parent->getParent(); - } + // Get the entity in the requested language and the field's item list from + // that. + $entity_translation = $field_item->getEntity()->getTranslation($langcode); + $field_items_translation = $entity_translation->get($field_item->getFieldDefinition()->getName()); - // Recreate the property path with translations. - $translation = $parent->getTranslation($langcode); - foreach ($ancestors as $ancestor) { - $ancestor_name = $ancestor->getName(); - $translation = $translation->get($ancestor_name); - } - - // Create a new instance at the end of the property path and return it. - $count = $translation->isEmpty() ? 0 : $translation->count(); - return $translation->get($count); + // Create a new instance and return it. + $count = $field_items_translation->isEmpty() ? 0 : $field_items_translation->count(); + return $field_items_translation->get($count); } } diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/FileEntityNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/FileEntityNormalizer.php index 815ed6e..e8d0bc6 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/FileEntityNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/FileEntityNormalizer.php @@ -15,7 +15,7 @@ /** * Converts the Drupal entity object structure to a HAL array structure. */ -class FileEntityNormalizer extends EntityNormalizer { +class FileEntityNormalizer extends ContentEntityNormalizer { /** * The interface or class that this Normalizer supports. diff --git a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php index 99e8de7..aa2473d 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php @@ -10,7 +10,7 @@ use Drupal\Core\Cache\MemoryBackend; use Drupal\Core\Language\Language; use Drupal\hal\Encoder\JsonEncoder; -use Drupal\hal\Normalizer\EntityNormalizer; +use Drupal\hal\Normalizer\ContentEntityNormalizer; use Drupal\hal\Normalizer\EntityReferenceItemNormalizer; use Drupal\hal\Normalizer\FieldItemNormalizer; use Drupal\hal\Normalizer\FieldNormalizer; @@ -124,7 +124,7 @@ function setUp() { // Set up the mock serializer. $normalizers = array( - new EntityNormalizer($link_manager), + new ContentEntityNormalizer($link_manager), new EntityReferenceItemNormalizer($link_manager, new UuidResolver()), new FieldItemNormalizer(), new FieldNormalizer(), diff --git a/core/modules/node/lib/Drupal/node/Plugin/Condition/NodeType.php b/core/modules/node/lib/Drupal/node/Plugin/Condition/NodeType.php index 3739209..3a499c8 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/Condition/NodeType.php +++ b/core/modules/node/lib/Drupal/node/Plugin/Condition/NodeType.php @@ -84,7 +84,7 @@ public function summary() { */ public function evaluate() { $node = $this->getContextValue('node'); - return in_array($node->getType(), $this->configuration['bundles']); + return in_array($node->getEntity()->getType(), $this->configuration['bundles']); } } diff --git a/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php b/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php index 065cdca..34f4c3c 100644 --- a/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php @@ -7,6 +7,7 @@ namespace Drupal\node\Tests\Condition; +use Drupal\Core\Entity\Plugin\DataType\EntityTypedDataWrapper; use Drupal\system\Tests\Entity\EntityUnitTestBase; /** @@ -56,7 +57,7 @@ function testConditions() { // of 'article' and set the context to the page type node. $condition = $manager->createInstance('node_type') ->setConfig('bundles', array('article')) - ->setContextValue('node', $page); + ->setContextValue('node', new EntityTypedDataWrapper($page)); $this->assertFalse($condition->execute(), 'Page type nodes fail node type checks for articles.'); // Check for the proper summary. $this->assertEqual('The node bundle is article', $condition->summary()); @@ -74,11 +75,11 @@ function testConditions() { $this->assertEqual('The node bundle is page or article', $condition->summary()); // Set the context to the article node. - $condition->setContextValue('node', $article); + $condition->setContextValue('node', new EntityTypedDataWrapper($article)); $this->assertTrue($condition->execute(), 'Article type nodes pass node type checks for pages or articles'); // Set the context to the test node. - $condition->setContextValue('node', $test); + $condition->setContextValue('node', new EntityTypedDataWrapper($test)); $this->assertFalse($condition->execute(), 'Test type nodes pass node type checks for pages or articles'); // Check a greater than 2 bundles summary scenario. @@ -86,7 +87,7 @@ function testConditions() { $this->assertEqual('The node bundle is page, article or test', $condition->summary()); // Test Constructor injection. - $condition = $manager->createInstance('node_type', array('bundles' => array('article'), 'context' => array('node' => $article))); + $condition = $manager->createInstance('node_type', array('bundles' => array('article'), 'context' => array('node' => new EntityTypedDataWrapper($article)))); $this->assertTrue($condition->execute(), 'Constructor injection of context and configuration working as anticipated.'); } } diff --git a/core/modules/user/lib/Drupal/user/Plugin/Validation/Constraint/UserUniqueValidator.php b/core/modules/user/lib/Drupal/user/Plugin/Validation/Constraint/UserUniqueValidator.php index f169cae..4d92787 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/Validation/Constraint/UserUniqueValidator.php +++ b/core/modules/user/lib/Drupal/user/Plugin/Validation/Constraint/UserUniqueValidator.php @@ -20,7 +20,7 @@ class UserUniqueValidator extends ConstraintValidator { */ public function validate($value, Constraint $constraint) { $field = $this->context->getMetadata()->getTypedData()->getParent(); - $uid = $field->getParent()->id(); + $uid = $field->getEntity()->id(); $value_taken = (bool) \Drupal::entityQuery('user') // The UID could be NULL, so we cast it to 0 in that case.