Update Composer, update everything
This commit is contained in:
parent
ea3e94409f
commit
dda5c284b6
19527 changed files with 1135420 additions and 351004 deletions
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Drupal\hal\LinkManager;
|
||||
|
||||
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
|
||||
|
||||
/**
|
||||
* Defines an abstract base-class for HAL link manager objects.
|
||||
*/
|
||||
|
@ -39,17 +41,32 @@ abstract class LinkManagerBase {
|
|||
/**
|
||||
* Gets the link domain.
|
||||
*
|
||||
* @param array $context
|
||||
* Normalization/serialization context.
|
||||
*
|
||||
* @return string
|
||||
* The link domain.
|
||||
*
|
||||
* @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
|
||||
* @see \Symfony\Component\Serializer\SerializerInterface::serialize()
|
||||
* @see \Drupal\serialization\Normalizer\CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY
|
||||
*/
|
||||
protected function getLinkDomain() {
|
||||
protected function getLinkDomain(array $context = []) {
|
||||
if (empty($this->linkDomain)) {
|
||||
if ($domain = $this->configFactory->get('hal.settings')->get('link_domain')) {
|
||||
$this->linkDomain = rtrim($domain, '/');
|
||||
// Bubble the appropriate cacheability metadata whenever possible.
|
||||
if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
|
||||
$context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($this->configFactory->get('hal.settings'));
|
||||
}
|
||||
return rtrim($domain, '/');
|
||||
}
|
||||
else {
|
||||
// Bubble the relevant cacheability metadata whenever possible.
|
||||
if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
|
||||
$context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheContexts(['url.site']);
|
||||
}
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
$this->linkDomain = $request->getSchemeAndHttpHost() . $request->getBasePath();
|
||||
return $request->getSchemeAndHttpHost() . $request->getBasePath();
|
||||
}
|
||||
}
|
||||
return $this->linkDomain;
|
||||
|
|
|
@ -13,7 +13,7 @@ use Symfony\Component\HttpFoundation\RequestStack;
|
|||
class RelationLinkManager extends LinkManagerBase implements RelationLinkManagerInterface {
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Cache\CacheBackendInterface;
|
||||
* @var \Drupal\Core\Cache\CacheBackendInterface
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
|
@ -68,11 +68,9 @@ class RelationLinkManager extends LinkManagerBase implements RelationLinkManager
|
|||
// 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";
|
||||
$uri = $this->getLinkDomain($context) . "/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);
|
||||
$this->moduleHandler->alterDeprecated('This hook is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. Implement hook_hal_relation_uri_alter() instead.', 'rest_relation_uri', $uri, $context);
|
||||
return $uri;
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ class TypeLinkManager extends LinkManagerBase implements TypeLinkManagerInterfac
|
|||
/**
|
||||
* Injected cache backend.
|
||||
*
|
||||
* @var \Drupal\Core\Cache\CacheBackendInterface;
|
||||
* @var \Drupal\Core\Cache\CacheBackendInterface
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
|
@ -69,11 +69,9 @@ class TypeLinkManager extends LinkManagerBase implements TypeLinkManagerInterfac
|
|||
// 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";
|
||||
$uri = $this->getLinkDomain($context) . "/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);
|
||||
$this->moduleHandler->alterDeprecated('This hook is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. Implement hook_hal_type_uri_alter() instead.', 'rest_type_uri', $uri, $context);
|
||||
return $uri;
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -6,6 +6,7 @@ use Drupal\Component\Utility\NestedArray;
|
|||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper;
|
||||
use Drupal\hal\LinkManager\LinkManagerInterface;
|
||||
use Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
@ -72,17 +73,12 @@ class ContentEntityNormalizer extends NormalizerBase {
|
|||
],
|
||||
];
|
||||
|
||||
$field_items = TypedDataInternalPropertiesHelper::getNonInternalProperties($entity->getTypedData());
|
||||
// If the fields to use were specified, only output those field values.
|
||||
if (isset($context['included_fields'])) {
|
||||
$fields = [];
|
||||
foreach ($context['included_fields'] as $field_name) {
|
||||
$fields[] = $entity->get($field_name);
|
||||
}
|
||||
$field_items = array_intersect_key($field_items, array_flip($context['included_fields']));
|
||||
}
|
||||
else {
|
||||
$fields = $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;
|
||||
|
@ -130,7 +126,7 @@ class ContentEntityNormalizer extends NormalizerBase {
|
|||
|
||||
// Figure out the language to use.
|
||||
if (isset($data[$default_langcode_key])) {
|
||||
// Find the field item for which the default_lancode value is set to 1 and
|
||||
// Find the field item for which the default_langcode value is set to 1 and
|
||||
// set the langcode the right default language.
|
||||
foreach ($data[$default_langcode_key] as $item) {
|
||||
if (!empty($item['value']) && isset($item['lang'])) {
|
||||
|
|
|
@ -2,16 +2,22 @@
|
|||
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
|
||||
use Drupal\hal\LinkManager\LinkManagerInterface;
|
||||
use Drupal\serialization\EntityResolver\EntityResolverInterface;
|
||||
use Drupal\serialization\EntityResolver\UuidReferenceInterface;
|
||||
use Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizerTrait;
|
||||
|
||||
/**
|
||||
* Converts the Drupal entity reference item object to HAL array structure.
|
||||
*/
|
||||
class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidReferenceInterface {
|
||||
|
||||
use EntityReferenceFieldItemNormalizerTrait;
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
|
@ -33,6 +39,13 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
|
|||
*/
|
||||
protected $entityResolver;
|
||||
|
||||
/**
|
||||
* The entity type manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||
*/
|
||||
protected $entityTypeManager;
|
||||
|
||||
/**
|
||||
* Constructs an EntityReferenceItemNormalizer object.
|
||||
*
|
||||
|
@ -40,25 +53,28 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
|
|||
* The hypermedia link manager.
|
||||
* @param \Drupal\serialization\EntityResolver\EntityResolverInterface $entity_Resolver
|
||||
* The entity resolver.
|
||||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface|null $entity_type_manager
|
||||
* The entity type manager.
|
||||
*/
|
||||
public function __construct(LinkManagerInterface $link_manager, EntityResolverInterface $entity_Resolver) {
|
||||
public function __construct(LinkManagerInterface $link_manager, EntityResolverInterface $entity_Resolver, EntityTypeManagerInterface $entity_type_manager = NULL) {
|
||||
$this->linkManager = $link_manager;
|
||||
$this->entityResolver = $entity_Resolver;
|
||||
$this->entityTypeManager = $entity_type_manager ?: \Drupal::service('entity_type.manager');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($field_item, $format = NULL, array $context = []) {
|
||||
/** @var $field_item \Drupal\Core\Field\FieldItemInterface */
|
||||
$target_entity = $field_item->get('entity')->getValue();
|
||||
|
||||
// If this is not a content entity, let the parent implementation handle it,
|
||||
// only content entities are supported as embedded resources.
|
||||
if (!($target_entity instanceof FieldableEntityInterface)) {
|
||||
// If this is not a fieldable entity, let the parent implementation handle
|
||||
// it, only fieldable entities are supported as embedded resources.
|
||||
if (!$this->targetEntityIsFieldable($field_item)) {
|
||||
return parent::normalize($field_item, $format, $context);
|
||||
}
|
||||
|
||||
/** @var $field_item \Drupal\Core\Field\FieldItemInterface */
|
||||
$target_entity = $field_item->get('entity')->getValue();
|
||||
|
||||
// If the parent entity passed in a langcode, unset it before normalizing
|
||||
// the target entity. Otherwise, untranslatable fields of the target entity
|
||||
// will include the langcode.
|
||||
|
@ -91,6 +107,39 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the referenced entity is of a fieldable entity type.
|
||||
*
|
||||
* @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item
|
||||
* The reference field item whose target entity needs to be checked.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE when the referenced entity is of a fieldable entity type.
|
||||
*/
|
||||
protected function targetEntityIsFieldable(EntityReferenceItem $item) {
|
||||
$target_entity = $item->get('entity')->getValue();
|
||||
|
||||
if ($target_entity !== NULL) {
|
||||
return $target_entity instanceof FieldableEntityInterface;
|
||||
}
|
||||
|
||||
$referencing_entity = $item->getEntity();
|
||||
$target_entity_type_id = $item->getFieldDefinition()->getSetting('target_type');
|
||||
|
||||
// If the entity type is the same as the parent, we can check that. This is
|
||||
// just a shortcut to avoid getting the entity type definition and checking
|
||||
// the class.
|
||||
if ($target_entity_type_id === $referencing_entity->getEntityTypeId()) {
|
||||
return $referencing_entity instanceof FieldableEntityInterface;
|
||||
}
|
||||
|
||||
// Otherwise, we need to get the class for the type.
|
||||
$target_entity_type = $this->entityTypeManager->getDefinition($target_entity_type_id);
|
||||
$target_entity_type_class = $target_entity_type->getClass();
|
||||
|
||||
return is_a($target_entity_type_class, FieldableEntityInterface::class, TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -100,11 +149,26 @@ 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 ['target_id' => $id];
|
||||
return ['target_id' => $id] + array_intersect_key($data, $field_item->getProperties());
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function normalizedFieldValues(FieldItemInterface $field_item, $format, array $context) {
|
||||
// Normalize root reference values here so we don't need to deal with hal's
|
||||
// nested data structure for field items. This will be called from
|
||||
// \Drupal\hal\Normalizer\FieldItemNormalizer::normalize. Which will only
|
||||
// be called from this class for entities that are not fieldable.
|
||||
$normalized = parent::normalizedFieldValues($field_item, $format, $context);
|
||||
|
||||
$this->normalizeRootReferenceValue($normalized, $field_item);
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
|
@ -21,25 +22,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 +74,38 @@ 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 */
|
||||
$field_properties = !empty($field_item->getProperties(TRUE))
|
||||
? TypedDataInternalPropertiesHelper::getNonInternalProperties($field_item)
|
||||
: $field_item->getValue();
|
||||
foreach ($field_properties 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 +114,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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,13 +2,15 @@
|
|||
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\hal\LinkManager\LinkManagerInterface;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
|
||||
/**
|
||||
* Converts the Drupal entity object structure to a HAL array structure.
|
||||
*
|
||||
* @deprecated in Drupal 8.5.0, to be removed before Drupal 9.0.0.
|
||||
*/
|
||||
class FileEntityNormalizer extends ContentEntityNormalizer {
|
||||
|
||||
|
@ -26,22 +28,29 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
|
|||
*/
|
||||
protected $httpClient;
|
||||
|
||||
/**
|
||||
* The HAL settings config.
|
||||
*
|
||||
* @var \Drupal\Core\Config\ImmutableConfig
|
||||
*/
|
||||
protected $halSettings;
|
||||
|
||||
/**
|
||||
* Constructs a FileEntityNormalizer object.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
||||
* The entity manager.
|
||||
* @param \GuzzleHttp\ClientInterface $http_client
|
||||
* The HTTP Client.
|
||||
* @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
|
||||
* The hypermedia link manager.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler.
|
||||
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
||||
* The config factory.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entity_manager, ClientInterface $http_client, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler) {
|
||||
public function __construct(EntityManagerInterface $entity_manager, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
|
||||
parent::__construct($link_manager, $entity_manager, $module_handler);
|
||||
|
||||
$this->httpClient = $http_client;
|
||||
$this->halSettings = $config_factory->get('hal.settings');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,22 +58,15 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
|
|||
*/
|
||||
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);
|
||||
|
||||
$this->addCacheableDependency($context, $this->halSettings);
|
||||
|
||||
if ($this->halSettings->get('bc_file_uri_as_url_normalizer')) {
|
||||
// Replace the file url with a full url for the file.
|
||||
$data['uri'][0]['value'] = $this->getEntityUri($entity);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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']);
|
||||
$data['uri'] = file_unmanaged_save_data($file_data, $path);
|
||||
|
||||
return $this->entityManager->getStorage('file')->create($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 a new issue