Advertisement
Guest User

core/modules/hal/src/Normalizer/FieldItemNormalizer.php

a guest
Feb 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.75 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\hal\Normalizer;
  4.  
  5. use Drupal\Core\Field\FieldItemInterface;
  6. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  7.  
  8. /**
  9.  * Converts the Drupal field item object structure to HAL array structure.
  10.  */
  11. class FieldItemNormalizer extends NormalizerBase {
  12.  
  13.   /**
  14.    * The interface or class that this Normalizer supports.
  15.    *
  16.    * @var string
  17.    */
  18.   protected $supportedInterfaceOrClass = 'Drupal\Core\Field\FieldItemInterface';
  19.  
  20.   /**
  21.    * {@inheritdoc}
  22.    */
  23.   public function normalize($field_item, $format = NULL, array $context = []) {
  24.     $values = [];
  25.     // We normalize each individual property, so each can do their own casting,
  26.     // if needed.
  27.     /** @var \Drupal\Core\TypedData\TypedDataInterface $property */
  28.     foreach ($field_item as $property_name => $property) {
  29.       $values[$property_name] = $this->serializer->normalize($property, $format, $context);
  30.     }
  31.  
  32.     if (isset($context['langcode'])) {
  33.       $values['lang'] = $context['langcode'];
  34.     }
  35.  
  36.     // The values are wrapped in an array, and then wrapped in another array
  37.     // keyed by field name so that field items can be merged by the
  38.     // FieldNormalizer. This is necessary for the EntityReferenceItemNormalizer
  39.     // to be able to place values in the '_links' array.
  40.     $field = $field_item->getParent();
  41.     return [
  42.       $field->getName() => [$values],
  43.     ];
  44.   }
  45.  
  46.   /**
  47.    * {@inheritdoc}
  48.    */
  49.   public function denormalize($data, $class, $format = NULL, array $context = []) {
  50.     if (!isset($context['target_instance'])) {
  51.       throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
  52.     }
  53.     if ($context['target_instance']->getParent() == NULL) {
  54.       throw new InvalidArgumentException('The field item passed in via $context[\'target_instance\'] must have a parent set.');
  55.     }
  56.  
  57.     $field_item = $context['target_instance'];
  58.     $this->pmCheckForSerializedStrings($data, $class, $field_item);
  59.  
  60.     // If this field is translatable, we need to create a translated instance.
  61.     if (isset($data['lang'])) {
  62.       $langcode = $data['lang'];
  63.       unset($data['lang']);
  64.       $field_definition = $field_item->getFieldDefinition();
  65.       if ($field_definition->isTranslatable()) {
  66.         $field_item = $this->createTranslatedInstance($field_item, $langcode);
  67.       }
  68.     }
  69.  
  70.     $field_item->setValue($this->constructValue($data, $context));
  71.     return $field_item;
  72.   }
  73.  
  74.   /**
  75.    * Build the field item value using the incoming data.
  76.    *
  77.    * @param $data
  78.    *   The incoming data for this field item.
  79.    * @param $context
  80.    *   The context passed into the Normalizer.
  81.    *
  82.    * @return mixed
  83.    *   The value to use in Entity::setValue().
  84.    */
  85.   protected function constructValue($data, $context) {
  86.     /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
  87.     $field_item = $context['target_instance'];
  88.     $serialized_property_names = $this->pmGetCustomSerializedPropertyNames($field_item);
  89.  
  90.     // Explicitly serialize the input, unlike properties that rely on
  91.     // being automatically serialized, manually managed serialized properties
  92.     // expect to receive serialized input.
  93.     foreach ($serialized_property_names as $serialized_property_name) {
  94.       if (!empty($data[$serialized_property_name])) {
  95.         $data[$serialized_property_name] = serialize($data[$serialized_property_name]);
  96.       }
  97.     }
  98.  
  99.     return $data;
  100.   }
  101.  
  102.   /**
  103.    * Get a translated version of the field item instance.
  104.    *
  105.    * To indicate that a field item applies to one translation of an entity and
  106.    * not another, the property path must originate with a translation of the
  107.    * entity. This is the reason for using target_instances, from which the
  108.    * property path can be traversed up to the root.
  109.    *
  110.    * @param \Drupal\Core\Field\FieldItemInterface $field_item
  111.    *   The untranslated field item instance.
  112.    * @param $langcode
  113.    *   The langcode.
  114.    *
  115.    * @return \Drupal\Core\Field\FieldItemInterface
  116.    *   The translated field item instance.
  117.    */
  118.   protected function createTranslatedInstance(FieldItemInterface $item, $langcode) {
  119.     // Remove the untranslated item that was created for the default language
  120.     // by FieldNormalizer::denormalize().
  121.     $items = $item->getParent();
  122.     $delta = $item->getName();
  123.     unset($items[$delta]);
  124.  
  125.     // Instead, create a new item for the entity in the requested language.
  126.     $entity = $item->getEntity();
  127.     $entity_translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
  128.     $field_name = $item->getFieldDefinition()->getName();
  129.     return $entity_translation->get($field_name)->appendItem();
  130.   }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement