Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663

This commit is contained in:
Greg Anderson 2015-10-08 11:40:12 -07:00
parent eb34d130a8
commit f32e58e4b1
8476 changed files with 211648 additions and 170042 deletions

View file

@ -34,7 +34,7 @@ class ComplexDataNormalizer extends NormalizerBase {
public function normalize($object, $format = NULL, array $context = array()) {
$attributes = array();
foreach ($object as $name => $field) {
$attributes[$name] = $this->serializer->normalize($field, $format);
$attributes[$name] = $this->serializer->normalize($field, $format, $context);
}
return $attributes;
}

View file

@ -43,22 +43,54 @@ class EntityNormalizer extends ComplexDataNormalizer implements DenormalizerInte
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = array()) {
if (empty($context['entity_type'])) {
throw new UnexpectedValueException('Entity type parameter must be included in context.');
public function denormalize($data, $class, $format = NULL, array $context = []) {
// Get the entity type ID while letting context override the $class param.
$entity_type_id = !empty($context['entity_type']) ? $context['entity_type'] : $this->entityManager->getEntityTypeFromClass($class);
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type_definition */
// Get the entity type definition.
$entity_type_definition = $this->entityManager->getDefinition($entity_type_id, FALSE);
// Don't try to create an entity without an entity type id.
if (!$entity_type_definition) {
throw new UnexpectedValueException(sprintf('The specified entity type "%s" does not exist. A valid etnity type is required for denormalization', $entity_type_id));
}
$entity_type = $this->entityManager->getDefinition($context['entity_type']);
// The bundle property will be required to denormalize a bundleable entity.
if ($entity_type_definition->hasKey('bundle')) {
$bundle_key = $entity_type_definition->getKey('bundle');
// Get the base field definitions for this entity type.
$base_field_definitions = $this->entityManager->getBaseFieldDefinitions($entity_type_id);
// The bundle property behaves differently from other entity properties.
// i.e. the nested structure with a 'value' key does not work.
if ($entity_type->hasKey('bundle')) {
$bundle_key = $entity_type->getKey('bundle');
$type = $data[$bundle_key][0]['value'];
$data[$bundle_key] = $type;
// Get the ID key from the base field definition for the bundle key or
// default to 'value'.
$key_id = isset($base_field_definitions[$bundle_key]) ? $base_field_definitions[$bundle_key]->getFieldStorageDefinition()->getMainPropertyName() : 'value';
// Normalize the bundle if it is not explicitly set.
$data[$bundle_key] = isset($data[$bundle_key][0][$key_id]) ? $data[$bundle_key][0][$key_id] : (isset($data[$bundle_key]) ? $data[$bundle_key] : NULL);
// Get the bundle entity type from the entity type definition.
$bundle_type_id = $entity_type_definition->getBundleEntityType();
$bundle_types = $bundle_type_id ? $this->entityManager->getStorage($bundle_type_id)->getQuery()->execute() : [];
// Make sure a bundle has been provided.
if (!is_string($data[$bundle_key])) {
throw new UnexpectedValueException('A string must be provided as a bundle value.');
}
// Make sure the submitted bundle is a valid bundle for the entity type.
if ($bundle_types && !in_array($data[$bundle_key], $bundle_types)) {
throw new UnexpectedValueException(sprintf('"%s" is not a valid bundle type for denormalization.', $data[$bundle_key]));
}
}
return $this->entityManager->getStorage($context['entity_type'])->create($data);
// Create the entity from data.
$entity = $this->entityManager->getStorage($entity_type_id)->create($data);
// Pass the names of the fields whose values can be merged.
// @todo https://www.drupal.org/node/2456257 remove this.
$entity->_restSubmittedFields = array_keys($data);
return $entity;
}
}

View file

@ -0,0 +1,41 @@
<?php
/**
* @file
* Contains \Drupal\serialization\Normalizer\FileFieldItemNormalizer.
*/
namespace Drupal\serialization\Normalizer;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
/**
* Adds the file URI to embedded file entities.
*/
class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = EntityReferenceItem::class;
/**
* {@inheritdoc}
*/
public function normalize($field_item, $format = NULL, array $context = []) {
$values = parent::normalize($field_item, $format, $context);
// Add a 'url' value if there is a reference and a canonical URL. Hard code
// 'canonical' here as config entities override the default $rel parameter
// value to 'edit-form.
/** @var \Drupal\Core\Entity\EntityInterface $entity */
if (($entity = $field_item->get('entity')->getValue()) && ($url = $entity->url('canonical'))) {
$values['url'] = $url;
}
return $values;
}
}

View file

@ -2,22 +2,22 @@
/**
* @file
* Contains \Drupal\serialization\Normalizer\SafeStringNormalizer.
* Contains \Drupal\serialization\Normalizer\MarkupNormalizer.
*/
namespace Drupal\serialization\Normalizer;
/**
* Normalizes SafeStringInterface objects into a string.
* Normalizes MarkupInterface objects into a string.
*/
class SafeStringNormalizer extends NormalizerBase {
class MarkupNormalizer extends NormalizerBase {
/**
* The interface or class that this Normalizer supports.
*
* @var array
*/
protected $supportedInterfaceOrClass = array('Drupal\Component\Utility\SafeStringInterface');
protected $supportedInterfaceOrClass = array('Drupal\Component\Render\MarkupInterface');
/**
* {@inheritdoc}

View file

@ -20,7 +20,7 @@ class EntityResolverTest extends NormalizerTestBase {
*
* @var array
*/
public static $modules = array('entity_reference', 'hal', 'rest');
public static $modules = ['hal', 'rest'];
/**
* The format being tested.

View file

@ -39,6 +39,13 @@ class EntitySerializationTest extends NormalizerTestBase {
*/
protected $entity;
/**
* The test user.
*
* @var \Drupal\user\Entity\User
*/
protected $user;
/**
* The serializer service.
*
@ -58,10 +65,19 @@ class EntitySerializationTest extends NormalizerTestBase {
// User create needs sequence table.
$this->installSchema('system', array('sequences'));
// Create a test user to use as the entity owner.
$this->user = \Drupal::entityManager()->getStorage('user')->create([
'name' => 'serialization_test_user',
'mail' => 'foo@example.com',
'pass' => '123456',
]);
$this->user->save();
// Create a test entity to serialize.
$this->values = array(
'name' => $this->randomMachineName(),
'user_id' => \Drupal::currentUser()->id(),
'user_id' => $this->user->id(),
'field_test_text' => array(
'value' => $this->randomMachineName(),
'format' => 'full_html',
@ -99,7 +115,10 @@ class EntitySerializationTest extends NormalizerTestBase {
array('value' => $this->entity->created->value),
),
'user_id' => array(
array('target_id' => $this->values['user_id']),
array(
'target_id' => $this->user->id(),
'url' => $this->user->url(),
),
),
'revision_id' => array(
array('value' => 1),
@ -128,22 +147,15 @@ class EntitySerializationTest extends NormalizerTestBase {
* override some default access controls.
*/
public function testUserNormalize() {
$account = User::create([
'name' => 'serialization_test_user',
'mail' => 'foo@example.com',
'pass' => '123456',
]);
$account->save();
// Test password isn't available.
$normalized = $this->serializer->normalize($account);
$normalized = $this->serializer->normalize($this->user);
$this->assertFalse(array_key_exists('pass', $normalized), '"pass" key does not exist in normalized user');
$this->assertFalse(array_key_exists('mail', $normalized), '"mail" key does not exist in normalized user');
// Test again using our test user, so that our access control override will
// allow password viewing.
$normalized = $this->serializer->normalize($account, NULL, ['account' => $account]);
$normalized = $this->serializer->normalize($this->user, NULL, ['account' => $this->user]);
// The key 'pass' will now exist, but the password value should be
// normalized to NULL.
@ -179,7 +191,7 @@ class EntitySerializationTest extends NormalizerTestBase {
'name' => '<name><value>' . $this->values['name'] . '</value></name>',
'type' => '<type><value>entity_test_mulrev</value></type>',
'created' => '<created><value>' . $this->entity->created->value . '</value></created>',
'user_id' => '<user_id><target_id>' . $this->values['user_id'] . '</target_id></user_id>',
'user_id' => '<user_id><target_id>' . $this->user->id() . '</target_id><url>' . $this->user->url() . '</url></user_id>',
'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>',
'default_langcode' => '<default_langcode><value>1</value></default_langcode>',
'field_test_text' => '<field_test_text><value>' . $this->values['field_test_text']['value'] . '</value><format>' . $this->values['field_test_text']['format'] . '</format></field_test_text>',