upgrades core to 8.4.2

This commit is contained in:
Bachir Soussi Chiadmi
2017-11-14 16:09:54 +01:00
parent bd60eff9b3
commit c2b4e25be4
3504 changed files with 140306 additions and 38684 deletions
@@ -46,7 +46,7 @@ class BcConfigSubscriber implements EventSubscriberInterface {
$saved_config = $event->getConfig();
if ($saved_config->getName() === 'serialization.settings') {
if ($event->isChanged('bc_primitives_as_strings')) {
if ($event->isChanged('bc_primitives_as_strings') || $event->isChanged('bc_timestamp_normalizer_unix')) {
$this->kernel->invalidateContainer();
}
}
@@ -47,6 +47,7 @@ class UserRouteAlterSubscriber implements EventSubscriberInterface {
'user.login_status.http',
'user.login.http',
'user.logout.http',
'user.pass.http',
];
$routes = $event->getRouteCollection();
foreach ($route_names as $route_name) {
@@ -26,9 +26,9 @@ class ComplexDataNormalizer extends NormalizerBase {
*/
public function normalize($object, $format = NULL, array $context = []) {
$attributes = [];
/** @var \Drupal\Core\TypedData\TypedDataInterface $field */
foreach ($object as $name => $field) {
$attributes[$name] = $this->serializer->normalize($field, $format, $context);
/** @var \Drupal\Core\TypedData\TypedDataInterface $property */
foreach ($object as $name => $property) {
$attributes[$name] = $this->serializer->normalize($property, $format, $context);
}
return $attributes;
}
@@ -15,15 +15,15 @@ class ContentEntityNormalizer extends EntityNormalizer {
/**
* {@inheritdoc}
*/
public function normalize($object, $format = NULL, array $context = []) {
public function normalize($entity, $format = NULL, array $context = []) {
$context += [
'account' => NULL,
];
$attributes = [];
foreach ($object as $name => $field) {
if ($field->access('view', $context['account'])) {
$attributes[$name] = $this->serializer->normalize($field, $format, $context);
foreach ($entity as $name => $field_items) {
if ($field_items->access('view', $context['account'])) {
$attributes[$name] = $this->serializer->normalize($field_items, $format, $context);
}
}
@@ -40,13 +40,20 @@ class EntityNormalizer extends ComplexDataNormalizer implements DenormalizerInte
// The bundle property will be required to denormalize a bundleable
// fieldable entity.
if ($entity_type_definition->hasKey('bundle') && $entity_type_definition->isSubclassOf(FieldableEntityInterface::class)) {
// Get an array containing the bundle only. This also remove the bundle
// key from the $data array.
$bundle_data = $this->extractBundleData($data, $entity_type_definition);
if ($entity_type_definition->isSubclassOf(FieldableEntityInterface::class)) {
// Extract bundle data to pass into entity creation if the entity type uses
// bundles.
if ($entity_type_definition->hasKey('bundle')) {
// Get an array containing the bundle only. This also remove the bundle
// key from the $data array.
$create_params = $this->extractBundleData($data, $entity_type_definition);
}
else {
$create_params = [];
}
// Create the entity from bundle data only, then apply field values after.
$entity = $this->entityManager->getStorage($entity_type_id)->create($bundle_data);
$entity = $this->entityManager->getStorage($entity_type_id)->create($create_params);
$this->denormalizeFieldData($data, $entity, $format, $context);
}
@@ -2,12 +2,15 @@
namespace Drupal\serialization\Normalizer;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
* Adds the file URI to embedded file entities.
*/
class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
class EntityReferenceFieldItemNormalizer extends FieldItemNormalizer {
/**
* The interface or class that this Normalizer supports.
@@ -16,6 +19,23 @@ class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
*/
protected $supportedInterfaceOrClass = EntityReferenceItem::class;
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a EntityReferenceFieldItemNormalizer object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(EntityRepositoryInterface $entity_repository) {
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
@@ -35,8 +55,32 @@ class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
$values['url'] = $url;
}
}
return $values;
}
/**
* {@inheritdoc}
*/
protected function constructValue($data, $context) {
if (isset($data['target_uuid'])) {
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $field_item */
$field_item = $context['target_instance'];
if (empty($data['target_uuid'])) {
throw new InvalidArgumentException(sprintf('If provided "target_uuid" cannot be empty for field "%s".', $data['target_type'], $data['target_uuid'], $field_item->getName()));
}
$target_type = $field_item->getFieldDefinition()->getSetting('target_type');
if (!empty($data['target_type']) && $target_type !== $data['target_type']) {
throw new UnexpectedValueException(sprintf('The field "%s" property "target_type" must be set to "%s" or omitted.', $field_item->getFieldDefinition()->getName(), $target_type));
}
if ($entity = $this->entityRepository->loadEntityByUuid($target_type, $data['target_uuid'])) {
return ['target_id' => $entity->id()];
}
else {
// Unable to load entity by uuid.
throw new InvalidArgumentException(sprintf('No "%s" entity found with UUID "%s" for field "%s".', $data['target_type'], $data['target_uuid'], $field_item->getName()));
}
}
return parent::constructValue($data, $context);
}
}
@@ -17,6 +17,13 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma
*/
protected $supportedInterfaceOrClass;
/**
* List of formats which supports (de-)normalization.
*
* @var string|string[]
*/
protected $format;
/**
* {@inheritdoc}
*/
@@ -29,7 +36,7 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma
$supported = (array) $this->supportedInterfaceOrClass;
return (bool) array_filter($supported, function($name) use ($data) {
return (bool) array_filter($supported, function ($name) use ($data) {
return $data instanceof $name;
});
}
@@ -49,7 +56,7 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma
$supported = (array) $this->supportedInterfaceOrClass;
$subclass_check = function($name) use ($type) {
$subclass_check = function ($name) use ($type) {
return (class_exists($name) || interface_exists($name)) && is_subclass_of($type, $name, TRUE);
};
@@ -71,7 +78,7 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma
return TRUE;
}
return in_array($format, (array) $this->format);
return in_array($format, (array) $this->format, TRUE);
}
}
@@ -0,0 +1,87 @@
<?php
namespace Drupal\serialization\Normalizer;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
* A trait for TimestampItem normalization functionality.
*/
trait TimeStampItemNormalizerTrait {
/**
* Allowed timestamps formats for the denormalizer.
*
* The denormalizer allows deserialization to timestamps from three
* different formats. Validation of the input data and creation of the
* numerical timestamp value is handled with \DateTime::createFromFormat().
* The list is chosen to be unambiguous and language neutral, but also common
* for data interchange.
*
* @var string[]
*
* @see http://php.net/manual/en/datetime.createfromformat.php
*/
protected $allowedFormats = [
'UNIX timestamp' => 'U',
'ISO 8601' => \DateTime::ISO8601,
'RFC 3339' => \DateTime::RFC3339,
];
/**
* Processes normalized timestamp values to add a formatted date and format.
*
* @param array $normalized
* The normalized field data to process.
* @return array
* The processed data.
*/
protected function processNormalizedValues(array $normalized) {
// Use a RFC 3339 timestamp with the time zone set to UTC to replace the
// timestamp value.
$date = new \DateTime();
$date->setTimestamp($normalized['value']);
$date->setTimezone(new \DateTimeZone('UTC'));
$normalized['value'] = $date->format(\DateTime::RFC3339);
// 'format' is not a property on TimestampItem fields. This is present to
// assist consumers of this data.
$normalized['format'] = \DateTime::RFC3339;
return $normalized;
}
/**
* {@inheritdoc}
*/
protected function constructValue($data, $context) {
// Loop through the allowed formats and create a TimestampItem from the
// input data if it matches the defined pattern. Since the formats are
// unambiguous (i.e., they reference an absolute time with a defined time
// zone), only one will ever match.
$timezone = new \DateTimeZone('UTC');
// First check for a provided format.
if (!empty($data['format']) && in_array($data['format'], $this->allowedFormats)) {
$date = \DateTime::createFromFormat($data['format'], $data['value'], $timezone);
return ['value' => $date->getTimestamp()];
}
// Otherwise, loop through formats.
else {
foreach ($this->allowedFormats as $format) {
if (($date = \DateTime::createFromFormat($format, $data['value'], $timezone)) !== FALSE) {
return ['value' => $date->getTimestamp()];
}
}
}
$format_strings = [];
foreach ($this->allowedFormats as $label => $format) {
$format_strings[] = "\"$format\" ($label)";
}
$formats = implode(', ', $format_strings);
throw new UnexpectedValueException(sprintf('The specified date "%s" is not in an accepted format: %s.', $data['value'], $formats));
}
}
@@ -0,0 +1,42 @@
<?php
namespace Drupal\serialization\Normalizer;
use Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
/**
* Converts values for TimestampItem to and from common formats.
*/
class TimestampItemNormalizer extends FieldItemNormalizer {
use TimeStampItemNormalizerTrait;
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = TimestampItem::class;
/**
* {@inheritdoc}
*/
public function normalize($field_item, $format = NULL, array $context = []) {
$data = parent::normalize($field_item, $format, $context);
return $this->processNormalizedValues($data);
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
if (empty($data['value'])) {
throw new InvalidArgumentException('No "value" attribute present');
}
return parent::denormalize($data, $class, $format, $context);
}
}
@@ -10,4 +10,4 @@ use Drupal\Tests\serialization\Kernel\NormalizerTestBase as SerializationNormali
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use \Drupal\Tests\serialization\Kernel\NormalizerTestBase instead.
*/
abstract class NormalizerTestBase extends SerializationNormalizerTestBase { }
abstract class NormalizerTestBase extends SerializationNormalizerTestBase {}