You are here

class FieldNormalizer in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/hal/src/Normalizer/FieldNormalizer.php \Drupal\hal\Normalizer\FieldNormalizer

Converts the Drupal field structure to HAL array structure.

Hierarchy

Expanded class hierarchy of FieldNormalizer

2 files declare their use of FieldNormalizer
FieldNormalizerDenormalizeExceptionsUnitTest.php in core/modules/hal/tests/src/Unit/FieldNormalizerDenormalizeExceptionsUnitTest.php
Contains \Drupal\Tests\hal\Unit\FieldNormalizerDenormalizeExceptionsUnitTest.
NormalizerTestBase.php in core/modules/hal/src/Tests/NormalizerTestBase.php
Contains \Drupal\hal\Tests\NormalizerTestBase.
1 string reference to 'FieldNormalizer'
hal.services.yml in core/modules/hal/hal.services.yml
core/modules/hal/hal.services.yml
1 service uses FieldNormalizer
serializer.normalizer.field.hal in core/modules/hal/hal.services.yml
Drupal\hal\Normalizer\FieldNormalizer

File

core/modules/hal/src/Normalizer/FieldNormalizer.php, line 16
Contains \Drupal\hal\Normalizer\FieldNormalizer.

Namespace

Drupal\hal\Normalizer
View source
class FieldNormalizer extends NormalizerBase {

  /**
   * The interface or class that this Normalizer supports.
   *
   * @var string
   */
  protected $supportedInterfaceOrClass = 'Drupal\\Core\\Field\\FieldItemListInterface';

  /**
   * {@inheritdoc}
   */
  public function normalize($field, $format = NULL, array $context = array()) {
    $normalized_field_items = array();

    // Get the field definition.
    $entity = $field
      ->getEntity();
    $field_name = $field
      ->getName();
    $field_definition = $field
      ->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);
    }
    else {
      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));
      }
    }

    // Merge deep so that links set in entity reference normalizers are merged
    // into the links property.
    $normalized = NestedArray::mergeDeepArray($normalized_field_items);
    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.
   *
   * @param \Drupal\Core\Field\FieldItemListInterface $field
   *   The field object.
   * @param string $format
   *   The format.
   * @param array $context
   *   The context array.
   *
   * @return array
   *   The array of normalized field items.
   */
  protected function normalizeFieldItems($field, $format, $context) {
    $normalized_field_items = array();
    if (!$field
      ->isEmpty()) {
      foreach ($field as $field_item) {
        $normalized_field_items[] = $this->serializer
          ->normalize($field_item, $format, $context);
      }
    }
    return $normalized_field_items;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass
FieldNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides DenormalizerInterface::denormalize
FieldNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides NormalizerInterface::normalize
FieldNormalizer::normalizeFieldItems protected function Helper function to normalize field items.
NormalizerBase::$formats protected property The formats that the Normalizer can handle.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase::supportsNormalization
SerializerAwareNormalizer::$serializer protected property
SerializerAwareNormalizer::setSerializer public function Sets the owning Serializer object. Overrides SerializerAwareInterface::setSerializer