"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/hal/src/Normalizer/FieldItemNormalizer.php" (26 Nov 2020, 4710 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "FieldItemNormalizer.php" see the Fossies "Dox" file reference documentation.

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