Update core 8.3.0

This commit is contained in:
Rob Davies 2017-04-13 15:53:35 +01:00
parent da7a7918f8
commit cd7a898e66
6144 changed files with 132297 additions and 87747 deletions

View file

@ -2,34 +2,20 @@
namespace Drupal\hal\Encoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder as SymfonyJsonEncoder;
use Drupal\serialization\Encoder\JsonEncoder as SerializationJsonEncoder;
/**
* Encodes HAL data in JSON.
*
* Simply respond to hal_json format requests using the JSON encoder.
*/
class JsonEncoder extends SymfonyJsonEncoder {
class JsonEncoder extends SerializationJsonEncoder {
/**
* The formats that this Encoder supports.
*
* @var string
*/
protected $format = 'hal_json';
/**
* {@inheritdoc}
*/
public function supportsEncoding($format) {
return $format == $this->format;
}
/**
* {@inheritdoc}
*/
public function supportsDecoding($format) {
return $format == $this->format;
}
protected static $format = ['hal_json'];
}

View file

@ -1,19 +0,0 @@
<?php
namespace Drupal\hal\EventSubscriber;
use Drupal\Core\EventSubscriber\ExceptionJsonSubscriber;
/**
* Handle HAL JSON exceptions the same as JSON exceptions.
*/
class ExceptionHalJsonSubscriber extends ExceptionJsonSubscriber {
/**
* {@inheritdoc}
*/
protected function getHandledFormats() {
return ['hal_json'];
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Drupal\hal\LinkManager;
/**
* Defines an interface for a link manager with a configurable domain.
*/
interface ConfigurableLinkManagerInterface {
/**
* Sets the link domain used in constructing link URIs.
*
* @param string $domain
* The link domain to use for constructing link URIs.
*
* @return $this
*/
public function setLinkDomain($domain);
}

View file

@ -0,0 +1,71 @@
<?php
namespace Drupal\hal\LinkManager;
class LinkManager implements LinkManagerInterface {
/**
* The type link manager.
*
* @var \Drupal\hal\LinkManager\TypeLinkManagerInterface
*/
protected $typeLinkManager;
/**
* The relation link manager.
*
* @var \Drupal\hal\LinkManager\RelationLinkManagerInterface
*/
protected $relationLinkManager;
/**
* Constructor.
*
* @param \Drupal\hal\LinkManager\TypeLinkManagerInterface $type_link_manager
* Manager for handling bundle URIs.
* @param \Drupal\hal\LinkManager\RelationLinkManagerInterface $relation_link_manager
* Manager for handling bundle URIs.
*/
public function __construct(TypeLinkManagerInterface $type_link_manager, RelationLinkManagerInterface $relation_link_manager) {
$this->typeLinkManager = $type_link_manager;
$this->relationLinkManager = $relation_link_manager;
}
/**
* {@inheritdoc}
*/
public function getTypeUri($entity_type, $bundle, $context = []) {
return $this->typeLinkManager->getTypeUri($entity_type, $bundle, $context);
}
/**
* {@inheritdoc}
*/
public function getTypeInternalIds($type_uri, $context = []) {
return $this->typeLinkManager->getTypeInternalIds($type_uri, $context);
}
/**
* {@inheritdoc}
*/
public function getRelationUri($entity_type, $bundle, $field_name, $context = []) {
return $this->relationLinkManager->getRelationUri($entity_type, $bundle, $field_name, $context);
}
/**
* {@inheritdoc}
*/
public function getRelationInternalIds($relation_uri) {
return $this->relationLinkManager->getRelationInternalIds($relation_uri);
}
/**
* {@inheritdoc}
*/
public function setLinkDomain($domain) {
$this->relationLinkManager->setLinkDomain($domain);
$this->typeLinkManager->setLinkDomain($domain);
return $this;
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace Drupal\hal\LinkManager;
/**
* Defines an abstract base-class for HAL link manager objects.
*/
abstract class LinkManagerBase {
/**
* Link domain used for type links URIs.
*
* @var string
*/
protected $linkDomain;
/**
* Config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* {@inheritdoc}
*/
public function setLinkDomain($domain) {
$this->linkDomain = rtrim($domain, '/');
return $this;
}
/**
* Gets the link domain.
*
* @return string
* The link domain.
*/
protected function getLinkDomain() {
if (empty($this->linkDomain)) {
if ($domain = $this->configFactory->get('hal.settings')->get('link_domain')) {
$this->linkDomain = rtrim($domain, '/');
}
else {
$request = $this->requestStack->getCurrentRequest();
$this->linkDomain = $request->getSchemeAndHttpHost() . $request->getBasePath();
}
}
return $this->linkDomain;
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Drupal\hal\LinkManager;
/**
* Interface implemented by link managers.
*
* There are no explicit methods on the manager interface. Instead link managers
* broker the interactions of the different components, and therefore must
* implement each component interface, which is enforced by this interface
* extending all of the component ones.
*
* While a link manager may directly implement these interface methods with
* custom logic, it is expected to be more common for plugin managers to proxy
* the method invocations to the respective components.
*/
interface LinkManagerInterface extends TypeLinkManagerInterface, RelationLinkManagerInterface {
}

View file

@ -0,0 +1,144 @@
<?php
namespace Drupal\hal\LinkManager;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class RelationLinkManager extends LinkManagerBase implements RelationLinkManagerInterface {
/**
* @var \Drupal\Core\Cache\CacheBackendInterface;
*/
protected $cache;
/**
* Entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructor.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache of relation URIs and their associated Typed Data IDs.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(CacheBackendInterface $cache, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, RequestStack $request_stack) {
$this->cache = $cache;
$this->entityManager = $entity_manager;
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$this->requestStack = $request_stack;
}
/**
* {@inheritdoc}
*/
public function getRelationUri($entity_type, $bundle, $field_name, $context = []) {
// Per the interface documentation of this method, the returned URI may
// optionally also serve as the URL of a documentation page about this
// field. However, Drupal does not currently implement such a documentation
// page. Therefore, we return a URI assembled relative to the site's base
// URL, which is sufficient to uniquely identify the site's entity type +
// bundle + field for use in hypermedia formats, but we do not take into
// account unclean URLs, language prefixing, or anything else that would be
// required for Drupal to be able to respond with content at this URL. If a
// module is installed that adds such content, but requires this URL to be
// different (e.g., include a language prefix), then the module must also
// override the RelationLinkManager class/service to return the desired URL.
$uri = $this->getLinkDomain() . "/rest/relation/$entity_type/$bundle/$field_name";
$this->moduleHandler->alter('hal_relation_uri', $uri, $context);
// @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This
// hook is invoked to maintain backwards compatibility
$this->moduleHandler->alter('rest_relation_uri', $uri, $context);
return $uri;
}
/**
* {@inheritdoc}
*/
public function getRelationInternalIds($relation_uri, $context = []) {
$relations = $this->getRelations($context);
if (isset($relations[$relation_uri])) {
return $relations[$relation_uri];
}
return FALSE;
}
/**
* Get the array of relation links.
*
* Any field can be handled as a relation simply by changing how it is
* normalized. Therefore, there is no prior knowledge that can be used here
* to determine which fields to assign relation URIs. Instead, each field,
* even primitives, are given a relation URI. It is up to the caller to
* determine which URIs to use.
*
* @param array $context
* Context from the normalizer/serializer operation.
*
* @return array
* An array of typed data ids (entity_type, bundle, and field name) keyed
* by corresponding relation URI.
*/
protected function getRelations($context = []) {
$cid = 'hal:links:relations';
$cache = $this->cache->get($cid);
if (!$cache) {
$this->writeCache($context);
$cache = $this->cache->get($cid);
}
return $cache->data;
}
/**
* Writes the cache of relation links.
*
* @param array $context
* Context from the normalizer/serializer operation.
*/
protected function writeCache($context = []) {
$data = [];
foreach ($this->entityManager->getDefinitions() as $entity_type) {
if ($entity_type instanceof ContentEntityTypeInterface) {
foreach ($this->entityManager->getBundleInfo($entity_type->id()) as $bundle => $bundle_info) {
foreach ($this->entityManager->getFieldDefinitions($entity_type->id(), $bundle) as $field_definition) {
$relation_uri = $this->getRelationUri($entity_type->id(), $bundle, $field_definition->getName(), $context);
$data[$relation_uri] = [
'entity_type' => $entity_type,
'bundle' => $bundle,
'field_name' => $field_definition->getName(),
];
}
}
}
}
// These URIs only change when field info changes, so cache it permanently
// and only clear it when the fields cache is cleared.
$this->cache->set('hal:links:relations', $data, Cache::PERMANENT, ['entity_field_info']);
}
}

View file

@ -0,0 +1,39 @@
<?php
namespace Drupal\hal\LinkManager;
interface RelationLinkManagerInterface extends ConfigurableLinkManagerInterface {
/**
* Gets the URI that corresponds to a field.
*
* When using hypermedia formats, this URI can be used to indicate which
* field the data represents. Documentation about this field can also be
* provided at this URI.
*
* @param string $entity_type
* The bundle's entity type.
* @param string $bundle
* The bundle name.
* @param string $field_name
* The field name.
* @param array $context
* (optional) Optional serializer/normalizer context.
*
* @return string
* The corresponding URI for the field.
*/
public function getRelationUri($entity_type, $bundle, $field_name, $context = []);
/**
* Translates a REST URI into internal IDs.
*
* @param string $relation_uri
* Relation URI to transform into internal IDs
*
* @return array
* Array with keys 'entity_type', 'bundle' and 'field_name'.
*/
public function getRelationInternalIds($relation_uri);
}

View file

@ -0,0 +1,150 @@
<?php
namespace Drupal\hal\LinkManager;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class TypeLinkManager extends LinkManagerBase implements TypeLinkManagerInterface {
/**
* Injected cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface;
*/
protected $cache;
/**
* Module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $bundleInfoService;
/**
* Constructor.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The injected cache backend for caching type URIs.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info_service
* The bundle info service.
*/
public function __construct(CacheBackendInterface $cache, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, RequestStack $request_stack, EntityTypeBundleInfoInterface $bundle_info_service) {
$this->cache = $cache;
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$this->requestStack = $request_stack;
$this->bundleInfoService = $bundle_info_service;
}
/**
* {@inheritdoc}
*/
public function getTypeUri($entity_type, $bundle, $context = []) {
// Per the interface documentation of this method, the returned URI may
// optionally also serve as the URL of a documentation page about this
// bundle. However, Drupal does not currently implement such a documentation
// page. Therefore, we return a URI assembled relative to the site's base
// URL, which is sufficient to uniquely identify the site's entity type and
// bundle for use in hypermedia formats, but we do not take into account
// unclean URLs, language prefixing, or anything else that would be required
// for Drupal to be able to respond with content at this URL. If a module is
// installed that adds such content, but requires this URL to be different
// (e.g., include a language prefix), then the module must also override the
// TypeLinkManager class/service to return the desired URL.
$uri = $this->getLinkDomain() . "/rest/type/$entity_type/$bundle";
$this->moduleHandler->alter('hal_type_uri', $uri, $context);
// @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This
// hook is invoked to maintain backwards compatibility
$this->moduleHandler->alter('rest_type_uri', $uri, $context);
return $uri;
}
/**
* {@inheritdoc}
*/
public function getTypeInternalIds($type_uri, $context = []) {
$types = $this->getTypes($context);
if (isset($types[$type_uri])) {
return $types[$type_uri];
}
return FALSE;
}
/**
* Get the array of type links.
*
* @param array $context
* Context from the normalizer/serializer operation.
*
* @return array
* An array of typed data ids (entity_type and bundle) keyed by
* corresponding type URI.
*/
protected function getTypes($context = []) {
$cid = 'hal:links:types';
$cache = $this->cache->get($cid);
if (!$cache) {
$data = $this->writeCache($context);
}
else {
$data = $cache->data;
}
return $data;
}
/**
* Writes the cache of type links.
*
* @param array $context
* Context from the normalizer/serializer operation.
*
* @return array
* An array of typed data ids (entity_type and bundle) keyed by
* corresponding type URI.
*/
protected function writeCache($context = []) {
$data = [];
// Type URIs correspond to bundles. Iterate through the bundles to get the
// URI and data for them.
$entity_types = \Drupal::entityManager()->getDefinitions();
foreach ($this->bundleInfoService->getAllBundleInfo() as $entity_type_id => $bundles) {
// Only content entities are supported currently.
// @todo Consider supporting config entities.
if ($entity_types[$entity_type_id]->entityClassImplements(ConfigEntityInterface::class)) {
continue;
}
foreach ($bundles as $bundle => $bundle_info) {
// Get a type URI for the bundle.
$bundle_uri = $this->getTypeUri($entity_type_id, $bundle, $context);
$data[$bundle_uri] = [
'entity_type' => $entity_type_id,
'bundle' => $bundle,
];
}
}
// These URIs only change when entity info changes, so cache it permanently
// and only clear it when entity_info is cleared.
$this->cache->set('hal:links:types', $data, Cache::PERMANENT, ['entity_types']);
return $data;
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Drupal\hal\LinkManager;
interface TypeLinkManagerInterface extends ConfigurableLinkManagerInterface {
/**
* Gets the URI that corresponds to a bundle.
*
* When using hypermedia formats, this URI can be used to indicate which
* bundle the data represents. Documentation about required and optional
* fields can also be provided at this URI.
*
* @param $entity_type
* The bundle's entity type.
* @param $bundle
* The bundle name.
* @param array $context
* (optional) Optional serializer/normalizer context.
*
* @return string
* The corresponding URI for the bundle.
*/
public function getTypeUri($entity_type, $bundle, $context = []);
/**
* Get a bundle's Typed Data IDs based on a URI.
*
* @param string $type_uri
* The type URI.
* @param array $context
* Context from the normalizer/serializer operation.
*
* @return array | boolean
* If the URI matches a bundle, returns an array containing entity_type and
* bundle. Otherwise, returns false.
*/
public function getTypeInternalIds($type_uri, $context = []);
}

View file

@ -6,7 +6,8 @@ use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\rest\LinkManager\LinkManagerInterface;
use Drupal\hal\LinkManager\LinkManagerInterface;
use Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
@ -14,6 +15,8 @@ use Symfony\Component\Serializer\Exception\UnexpectedValueException;
*/
class ContentEntityNormalizer extends NormalizerBase {
use FieldableEntityNormalizerTrait;
/**
* The interface or class that this Normalizer supports.
*
@ -24,17 +27,10 @@ class ContentEntityNormalizer extends NormalizerBase {
/**
* The hypermedia link manager.
*
* @var \Drupal\rest\LinkManager\LinkManagerInterface
* @var \Drupal\hal\LinkManager\LinkManagerInterface
*/
protected $linkManager;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The module handler.
*
@ -42,11 +38,10 @@ class ContentEntityNormalizer extends NormalizerBase {
*/
protected $moduleHandler;
/**
* Constructs an ContentEntityNormalizer object.
*
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
* @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
* The hypermedia link manager.
*/
public function __construct(LinkManagerInterface $link_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) {
@ -58,28 +53,28 @@ class ContentEntityNormalizer extends NormalizerBase {
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = array()) {
$context += array(
public function normalize($entity, $format = NULL, array $context = []) {
$context += [
'account' => NULL,
'included_fields' => NULL,
);
];
// Create the array of normalized fields, starting with the URI.
/** @var $entity \Drupal\Core\Entity\ContentEntityInterface */
$normalized = array(
'_links' => array(
'self' => array(
$normalized = [
'_links' => [
'self' => [
'href' => $this->getEntityUri($entity),
),
'type' => array(
],
'type' => [
'href' => $this->linkManager->getTypeUri($entity->getEntityTypeId(), $entity->bundle(), $context),
),
),
);
],
],
];
// If the fields to use were specified, only output those field values.
if (isset($context['included_fields'])) {
$fields = array();
$fields = [];
foreach ($context['included_fields'] as $field_name) {
$fields[] = $entity->get($field_name);
}
@ -120,7 +115,7 @@ class ContentEntityNormalizer extends NormalizerBase {
*
* @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function denormalize($data, $class, $format = NULL, array $context = array()) {
public function denormalize($data, $class, $format = NULL, array $context = []) {
// Get type, necessary for determining which bundle to create.
if (!isset($data['_links']['type'])) {
throw new UnexpectedValueException('The type link relation must be specified.');
@ -128,10 +123,10 @@ class ContentEntityNormalizer extends NormalizerBase {
// Create the entity.
$typed_data_ids = $this->getTypedDataIds($data['_links']['type'], $context);
$entity_type = $this->entityManager->getDefinition($typed_data_ids['entity_type']);
$entity_type = $this->getEntityTypeDefinition($typed_data_ids['entity_type']);
$default_langcode_key = $entity_type->getKey('default_langcode');
$langcode_key = $entity_type->getKey('langcode');
$values = array();
$values = [];
// Figure out the language to use.
if (isset($data[$default_langcode_key])) {
@ -159,7 +154,7 @@ class ContentEntityNormalizer extends NormalizerBase {
// Remove links from data array.
unset($data['_links']);
// Get embedded resources and remove from data array.
$embedded = array();
$embedded = [];
if (isset($data['_embedded'])) {
$embedded = $data['_embedded'];
unset($data['_embedded']);
@ -174,23 +169,11 @@ class ContentEntityNormalizer extends NormalizerBase {
}
}
// Pass the names of the fields whose values can be merged.
$entity->_restSubmittedFields = array_keys($data);
$this->denormalizeFieldData($data, $entity, $format, $context);
// Iterate through remaining items in data array. These should all
// correspond to fields.
foreach ($data as $field_name => $field_data) {
$items = $entity->get($field_name);
// Remove any values that were set as a part of entity creation (e.g
// uuid). If the incoming field data is set to an empty array, this will
// also have the effect of emptying the field in REST module.
$items->setValue(array());
if ($field_data) {
// Denormalize the field data into the FieldItemList object.
$context['target_instance'] = $items;
$this->serializer->denormalize($field_data, get_class($items), $format, $context);
}
}
// 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;
}
@ -224,12 +207,12 @@ class ContentEntityNormalizer extends NormalizerBase {
* @return array
* The typed data IDs.
*/
protected function getTypedDataIds($types, $context = array()) {
protected function getTypedDataIds($types, $context = []) {
// The 'type' can potentially contain an array of type objects. By default,
// Drupal only uses a single type in serializing, but allows for multiple
// types when deserializing.
if (isset($types['href'])) {
$types = array($types);
$types = [$types];
}
if (empty($types)) {

View file

@ -3,7 +3,7 @@
namespace Drupal\hal\Normalizer;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\rest\LinkManager\LinkManagerInterface;
use Drupal\hal\LinkManager\LinkManagerInterface;
use Drupal\serialization\EntityResolver\EntityResolverInterface;
use Drupal\serialization\EntityResolver\UuidReferenceInterface;
@ -22,7 +22,7 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
/**
* The hypermedia link manager.
*
* @var \Drupal\rest\LinkManager\LinkManagerInterface
* @var \Drupal\hal\LinkManager\LinkManagerInterface
*/
protected $linkManager;
@ -36,7 +36,7 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
/**
* Constructs an EntityReferenceItemNormalizer object.
*
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
* @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
* The hypermedia link manager.
* @param \Drupal\serialization\EntityResolver\EntityResolverInterface $entity_Resolver
* The entity resolver.
@ -49,7 +49,7 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
/**
* {@inheritdoc}
*/
public function normalize($field_item, $format = NULL, array $context = array()) {
public function normalize($field_item, $format = NULL, array $context = []) {
/** @var $field_item \Drupal\Core\Field\FieldItemInterface */
$target_entity = $field_item->get('entity')->getValue();
@ -64,7 +64,7 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
// will include the langcode.
$langcode = isset($context['langcode']) ? $context['langcode'] : NULL;
unset($context['langcode']);
$context['included_fields'] = array('uuid');
$context['included_fields'] = ['uuid'];
// Normalize the target entity.
$embedded = $this->serializer->normalize($target_entity, $format, $context);
@ -81,14 +81,14 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
$field_name = $field_item->getParent()->getName();
$entity = $field_item->getEntity();
$field_uri = $this->linkManager->getRelationUri($entity->getEntityTypeId(), $entity->bundle(), $field_name, $context);
return array(
'_links' => array(
$field_uri => array($link),
),
'_embedded' => array(
$field_uri => array($embedded),
),
);
return [
'_links' => [
$field_uri => [$link],
],
'_embedded' => [
$field_uri => [$embedded],
],
];
}
/**
@ -100,7 +100,7 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
$target_type = $field_definition->getSetting('target_type');
$id = $this->entityResolver->resolve($this, $data, $target_type);
if (isset($id)) {
return array('target_id' => $id);
return ['target_id' => $id];
}
return NULL;
}

View file

@ -20,8 +20,15 @@ class FieldItemNormalizer extends NormalizerBase {
/**
* {@inheritdoc}
*/
public function normalize($field_item, $format = NULL, array $context = array()) {
$values = $field_item->toArray();
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'];
}
@ -31,15 +38,15 @@ class FieldItemNormalizer extends NormalizerBase {
// FieldNormalizer. This is necessary for the EntityReferenceItemNormalizer
// to be able to place values in the '_links' array.
$field = $field_item->getParent();
return array(
$field->getName() => array($values),
);
return [
$field->getName() => [$values],
];
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = array()) {
public function denormalize($data, $class, $format = NULL, array $context = []) {
if (!isset($context['target_instance'])) {
throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
}

View file

@ -3,25 +3,25 @@
namespace Drupal\hal\Normalizer;
use Drupal\Component\Utility\NestedArray;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Drupal\serialization\Normalizer\FieldNormalizer as SerializationFieldNormalizer;
/**
* Converts the Drupal field structure to HAL array structure.
*/
class FieldNormalizer extends NormalizerBase {
class FieldNormalizer extends SerializationFieldNormalizer {
/**
* The interface or class that this Normalizer supports.
* The formats that the Normalizer can handle.
*
* @var string
* @var array
*/
protected $supportedInterfaceOrClass = 'Drupal\Core\Field\FieldItemListInterface';
protected $format = ['hal_json'];
/**
* {@inheritdoc}
*/
public function normalize($field, $format = NULL, array $context = array()) {
$normalized_field_items = array();
public function normalize($field, $format = NULL, array $context = []) {
$normalized_field_items = [];
// Get the field definition.
$entity = $field->getEntity();
@ -51,33 +51,6 @@ class FieldNormalizer extends NormalizerBase {
return $normalized;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = array()) {
if (!isset($context['target_instance'])) {
throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldNormalizer');
}
if ($context['target_instance']->getParent() == NULL) {
throw new InvalidArgumentException('The field passed in via $context[\'target_instance\'] must have a parent set.');
}
$items = $context['target_instance'];
$item_class = $items->getItemDefinition()->getClass();
foreach ($data as $item_data) {
// Create a new item and pass it as the target for the unserialization of
// $item_data. Note: if $item_data is about a different language than the
// default, FieldItemNormalizer::denormalize() will dismiss this item and
// create a new one for the right language.
$context['target_instance'] = $items->appendItem();
$this->serializer->denormalize($item_data, $item_class, $format, $context);
}
return $items;
}
/**
* Helper function to normalize field items.
*
@ -92,7 +65,7 @@ class FieldNormalizer extends NormalizerBase {
* The array of normalized field items.
*/
protected function normalizeFieldItems($field, $format, $context) {
$normalized_field_items = array();
$normalized_field_items = [];
if (!$field->isEmpty()) {
foreach ($field as $field_item) {
$normalized_field_items[] = $this->serializer->normalize($field_item, $format, $context);

View file

@ -4,7 +4,7 @@ namespace Drupal\hal\Normalizer;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\rest\LinkManager\LinkManagerInterface;
use Drupal\hal\LinkManager\LinkManagerInterface;
use GuzzleHttp\ClientInterface;
/**
@ -33,7 +33,7 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
* The entity manager.
* @param \GuzzleHttp\ClientInterface $http_client
* The HTTP Client.
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
* @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
* The hypermedia link manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
@ -47,7 +47,7 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = array()) {
public function normalize($entity, $format = NULL, array $context = []) {
$data = parent::normalize($entity, $format, $context);
// Replace the file url with a full url for the file.
$data['uri'][0]['value'] = $this->getEntityUri($entity);
@ -58,7 +58,7 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = array()) {
public function denormalize($data, $class, $format = NULL, array $context = []) {
$file_data = (string) $this->httpClient->get($data['uri'][0]['value'])->getBody();
$path = 'temporary://' . drupal_basename($data['uri'][0]['value']);

View file

@ -15,7 +15,7 @@ abstract class NormalizerBase extends SerializationNormalizerBase implements Den
*
* @var array
*/
protected $formats = array('hal_json');
protected $formats = ['hal_json'];
/**
* {@inheritdoc}

View file

@ -1,75 +0,0 @@
<?php
namespace Drupal\hal\Tests;
use Drupal\file\Entity\File;
use Drupal\simpletest\WebTestBase;
/**
* Tests that file entities can be denormalized in HAL.
*
* @group hal
* @see \Drupal\hal\Normalizer\FileEntityNormalizer
*/
class FileDenormalizeTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('hal', 'file', 'node');
/**
* Tests file entity denormalization.
*/
public function testFileDenormalize() {
$file_params = array(
'filename' => 'test_1.txt',
'uri' => 'public://test_1.txt',
'filemime' => 'text/plain',
'status' => FILE_STATUS_PERMANENT,
);
// Create a new file entity.
$file = File::create($file_params);
file_put_contents($file->getFileUri(), 'hello world');
$file->save();
$serializer = \Drupal::service('serializer');
$normalized_data = $serializer->normalize($file, 'hal_json');
$denormalized = $serializer->denormalize($normalized_data, 'Drupal\file\Entity\File', 'hal_json');
$this->assertTrue($denormalized instanceof File, 'A File instance was created.');
$this->assertIdentical('temporary://' . $file->getFilename(), $denormalized->getFileUri(), 'The expected file URI was found.');
$this->assertTrue(file_exists($denormalized->getFileUri()), 'The temporary file was found.');
$this->assertIdentical($file->uuid(), $denormalized->uuid(), 'The expected UUID was found');
$this->assertIdentical($file->getMimeType(), $denormalized->getMimeType(), 'The expected MIME type was found.');
$this->assertIdentical($file->getFilename(), $denormalized->getFilename(), 'The expected filename was found.');
$this->assertTrue($denormalized->isPermanent(), 'The file has a permanent status.');
// Try to denormalize with the file uri only.
$file_name = 'test_2.txt';
$file_path = 'public://' . $file_name;
file_put_contents($file_path, 'hello world');
$file_uri = file_create_url($file_path);
$data = array(
'uri' => array(
array('value' => $file_uri),
),
);
$denormalized = $serializer->denormalize($data, 'Drupal\file\Entity\File', 'hal_json');
$this->assertIdentical('temporary://' . $file_name, $denormalized->getFileUri(), 'The expected file URI was found.');
$this->assertTrue(file_exists($denormalized->getFileUri()), 'The temporary file was found.');
$this->assertIdentical('text/plain', $denormalized->getMimeType(), 'The expected MIME type was found.');
$this->assertIdentical($file_name, $denormalized->getFilename(), 'The expected filename was found.');
$this->assertFalse($denormalized->isPermanent(), 'The file has a permanent status.');
}
}