upgrades core to 8.4.2
This commit is contained in:
@@ -31,7 +31,7 @@ interface TypeLinkManagerInterface extends ConfigurableLinkManagerInterface {
|
||||
* @param array $context
|
||||
* Context from the normalizer/serializer operation.
|
||||
*
|
||||
* @return array | boolean
|
||||
* @return array|bool
|
||||
* If the URI matches a bundle, returns an array containing entity_type and
|
||||
* bundle. Otherwise, returns false.
|
||||
*/
|
||||
|
||||
@@ -74,15 +74,15 @@ class ContentEntityNormalizer extends NormalizerBase {
|
||||
|
||||
// If the fields to use were specified, only output those field values.
|
||||
if (isset($context['included_fields'])) {
|
||||
$fields = [];
|
||||
$field_items = [];
|
||||
foreach ($context['included_fields'] as $field_name) {
|
||||
$fields[] = $entity->get($field_name);
|
||||
$field_items[] = $entity->get($field_name);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$fields = $entity->getFields();
|
||||
$field_items = $entity->getFields();
|
||||
}
|
||||
foreach ($fields as $field) {
|
||||
foreach ($field_items as $field) {
|
||||
// Continue if the current user does not have access to view this field.
|
||||
if (!$field->access('view', $context['account'])) {
|
||||
continue;
|
||||
|
||||
@@ -21,25 +21,13 @@ class FieldItemNormalizer extends NormalizerBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($field_item, $format = NULL, array $context = []) {
|
||||
$values = [];
|
||||
// We normalize each individual property, so each can do their own casting,
|
||||
// if needed.
|
||||
/** @var \Drupal\Core\TypedData\TypedDataInterface $property */
|
||||
foreach ($field_item as $property_name => $property) {
|
||||
$values[$property_name] = $this->serializer->normalize($property, $format, $context);
|
||||
}
|
||||
|
||||
if (isset($context['langcode'])) {
|
||||
$values['lang'] = $context['langcode'];
|
||||
}
|
||||
|
||||
// The values are wrapped in an array, and then wrapped in another array
|
||||
// keyed by field name so that field items can be merged by the
|
||||
// FieldNormalizer. This is necessary for the EntityReferenceItemNormalizer
|
||||
// to be able to place values in the '_links' array.
|
||||
$field = $field_item->getParent();
|
||||
return [
|
||||
$field->getName() => [$values],
|
||||
$field->getName() => [$this->normalizedFieldValues($field_item, $format, $context)],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -85,6 +73,35 @@ class FieldItemNormalizer extends NormalizerBase {
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes field values for an item.
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldItemInterface $field_item
|
||||
* The field item instance.
|
||||
* @param string|null $format
|
||||
* The normalization format.
|
||||
* @param array $context
|
||||
* The context passed into the normalizer.
|
||||
*
|
||||
* @return array
|
||||
* An array of field item values, keyed by property name.
|
||||
*/
|
||||
protected function normalizedFieldValues(FieldItemInterface $field_item, $format, array $context) {
|
||||
$normalized = [];
|
||||
// We normalize each individual property, so each can do their own casting,
|
||||
// if needed.
|
||||
/** @var \Drupal\Core\TypedData\TypedDataInterface $property */
|
||||
foreach ($field_item as $property_name => $property) {
|
||||
$normalized[$property_name] = $this->serializer->normalize($property, $format, $context);
|
||||
}
|
||||
|
||||
if (isset($context['langcode'])) {
|
||||
$normalized['lang'] = $context['langcode'];
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a translated version of the field item instance.
|
||||
*
|
||||
@@ -93,7 +110,7 @@ class FieldItemNormalizer extends NormalizerBase {
|
||||
* entity. This is the reason for using target_instances, from which the
|
||||
* property path can be traversed up to the root.
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldItemInterface $field_item
|
||||
* @param \Drupal\Core\Field\FieldItemInterface $item
|
||||
* The untranslated field item instance.
|
||||
* @param $langcode
|
||||
* The langcode.
|
||||
|
||||
@@ -11,27 +11,25 @@ use Drupal\serialization\Normalizer\FieldNormalizer as SerializationFieldNormali
|
||||
class FieldNormalizer extends SerializationFieldNormalizer {
|
||||
|
||||
/**
|
||||
* The formats that the Normalizer can handle.
|
||||
*
|
||||
* @var array
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $format = ['hal_json'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($field, $format = NULL, array $context = []) {
|
||||
public function normalize($field_items, $format = NULL, array $context = []) {
|
||||
$normalized_field_items = [];
|
||||
|
||||
// Get the field definition.
|
||||
$entity = $field->getEntity();
|
||||
$field_name = $field->getName();
|
||||
$field_definition = $field->getFieldDefinition();
|
||||
$entity = $field_items->getEntity();
|
||||
$field_name = $field_items->getName();
|
||||
$field_definition = $field_items->getFieldDefinition();
|
||||
|
||||
// If this field is not translatable, it can simply be normalized without
|
||||
// separating it into different translations.
|
||||
if (!$field_definition->isTranslatable()) {
|
||||
$normalized_field_items = $this->normalizeFieldItems($field, $format, $context);
|
||||
$normalized_field_items = $this->normalizeFieldItems($field_items, $format, $context);
|
||||
}
|
||||
// Otherwise, the languages have to be extracted from the entity and passed
|
||||
// in to the field item normalizer in the context. The langcode is appended
|
||||
@@ -40,22 +38,21 @@ class FieldNormalizer extends SerializationFieldNormalizer {
|
||||
foreach ($entity->getTranslationLanguages() as $language) {
|
||||
$context['langcode'] = $language->getId();
|
||||
$translation = $entity->getTranslation($language->getId());
|
||||
$translated_field = $translation->get($field_name);
|
||||
$normalized_field_items = array_merge($normalized_field_items, $this->normalizeFieldItems($translated_field, $format, $context));
|
||||
$translated_field_items = $translation->get($field_name);
|
||||
$normalized_field_items = array_merge($normalized_field_items, $this->normalizeFieldItems($translated_field_items, $format, $context));
|
||||
}
|
||||
}
|
||||
|
||||
// Merge deep so that links set in entity reference normalizers are merged
|
||||
// into the links property.
|
||||
$normalized = NestedArray::mergeDeepArray($normalized_field_items);
|
||||
return $normalized;
|
||||
return NestedArray::mergeDeepArray($normalized_field_items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to normalize field items.
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldItemListInterface $field
|
||||
* The field object.
|
||||
* @param \Drupal\Core\Field\FieldItemListInterface $field_items
|
||||
* The field item list object.
|
||||
* @param string $format
|
||||
* The format.
|
||||
* @param array $context
|
||||
@@ -64,10 +61,10 @@ class FieldNormalizer extends SerializationFieldNormalizer {
|
||||
* @return array
|
||||
* The array of normalized field items.
|
||||
*/
|
||||
protected function normalizeFieldItems($field, $format, $context) {
|
||||
protected function normalizeFieldItems($field_items, $format, $context) {
|
||||
$normalized_field_items = [];
|
||||
if (!$field->isEmpty()) {
|
||||
foreach ($field as $field_item) {
|
||||
if (!$field_items->isEmpty()) {
|
||||
foreach ($field_items as $field_item) {
|
||||
$normalized_field_items[] = $this->serializer->normalize($field_item, $format, $context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,35 +11,21 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
abstract class NormalizerBase extends SerializationNormalizerBase implements DenormalizerInterface {
|
||||
|
||||
/**
|
||||
* The formats that the Normalizer can handle.
|
||||
*
|
||||
* @var array
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $formats = ['hal_json'];
|
||||
protected $format = ['hal_json'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsNormalization($data, $format = NULL) {
|
||||
return in_array($format, $this->formats) && parent::supportsNormalization($data, $format);
|
||||
}
|
||||
protected function checkFormat($format = NULL) {
|
||||
if (isset($this->formats)) {
|
||||
@trigger_error('::formats is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use ::$format instead. See https://www.drupal.org/node/2868275', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDenormalization($data, $type, $format = NULL) {
|
||||
if (in_array($format, $this->formats) && (class_exists($this->supportedInterfaceOrClass) || interface_exists($this->supportedInterfaceOrClass))) {
|
||||
$target = new \ReflectionClass($type);
|
||||
$supported = new \ReflectionClass($this->supportedInterfaceOrClass);
|
||||
if ($supported->isInterface()) {
|
||||
return $target->implementsInterface($this->supportedInterfaceOrClass);
|
||||
}
|
||||
else {
|
||||
return ($target->getName() == $this->supportedInterfaceOrClass || $target->isSubclassOf($this->supportedInterfaceOrClass));
|
||||
}
|
||||
$this->format = $this->formats;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
return parent::checkFormat($format);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem;
|
||||
use Drupal\serialization\Normalizer\TimeStampItemNormalizerTrait;
|
||||
|
||||
/**
|
||||
* Converts values for TimestampItem to and from common formats for hal.
|
||||
*/
|
||||
class TimestampItemNormalizer extends FieldItemNormalizer {
|
||||
|
||||
use TimeStampItemNormalizerTrait;
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $supportedInterfaceOrClass = TimestampItem::class;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function normalizedFieldValues(FieldItemInterface $field_item, $format, array $context) {
|
||||
$normalized = parent::normalizedFieldValues($field_item, $format, $context);
|
||||
return $this->processNormalizedValues($normalized);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user