class FieldNormalizer

Converts the Drupal field structure to HAL array structure.

Hierarchy

Expanded class hierarchy of FieldNormalizer

1 file declares its use of FieldNormalizer
NormalizerTestBase.php in drupal/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php
Contains \Drupal\hal\Tests\NormalizerTestBase.
1 string reference to 'FieldNormalizer'
hal.services.yml in drupal/core/modules/hal/hal.services.yml
drupal/core/modules/hal/hal.services.yml
1 service uses FieldNormalizer

File

drupal/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php, line 18
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\\Entity\\Field\\FieldInterface';

  /**
   * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
   */
  public function normalize($field, $format = NULL, array $context = array()) {
    $normalized_field_items = array();

    // Get the field definition.
    $entity = $field
      ->getParent();
    $field_name = $field
      ->getName();
    $field_definition = $entity
      ->getPropertyDefinition($field_name);

    // If this field is not translatable, it can simply be normalized without
    // separating it into different translations.
    if (empty($field_definition['translatable'])) {
      $normalized_field_items = $this
        ->normalizeFieldItems($field, $format, $context);
    }
    else {
      foreach ($entity
        ->getTranslationLanguages() as $lang) {
        $context['langcode'] = $lang->langcode == 'und' ? Language::LANGCODE_DEFAULT : $lang->langcode;
        $translation = $entity
          ->getTranslation($lang->langcode);
        $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;
  }

  /**
   * Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize()
   */
  public function denormalize($data, $class, $format = NULL, array $context = array()) {
    if (!isset($context['target_instance'])) {
      throw new LogicException('$context[\'target_instance\'] must be set to denormalize with the FieldNormalizer');
    }
    if ($context['target_instance']
      ->getParent() == NULL) {
      throw new LogicException('The field passed in via $context[\'target_instance\'] must have a parent set.');
    }
    $field = $context['target_instance'];
    foreach ($data as $field_item_data) {
      $count = $field
        ->count();

      // Get the next field item instance. The offset will serve as the field
      // item name.
      $field_item = $field
        ->offsetGet($count);
      $field_item_class = get_class($field_item);

      // Pass in the empty field item object as the target instance.
      $context['target_instance'] = $field_item;
      $this->serializer
        ->denormalize($field_item_data, $field_item_class, $format, $context);
    }
    return $field;
  }

  /**
   * Helper function to normalize field items.
   *
   * @param \Drupal\Core\Entity\Field\FieldInterface $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 Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize() Overrides DenormalizerInterface::denormalize
FieldNormalizer::normalize public function Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize() Overrides NormalizerInterface::normalize
FieldNormalizer::normalizeFieldItems protected function Helper function to normalize field items.
NormalizerBase::$entityResolver protected property The entity resolver.
NormalizerBase::$formats protected property The formats that the Normalizer can handle.
NormalizerBase::$linkManager protected property The hypermedia link manager.
NormalizerBase::setEntityResolver public function Sets the entity resolver.
NormalizerBase::setLinkManager public function Sets the link manager.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides DenormalizerInterface::supportsDenormalization
NormalizerBase::supportsNormalization public function Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::supportsNormalization(). Overrides NormalizerBase::supportsNormalization
SerializerAwareNormalizer::$serializer protected property
SerializerAwareNormalizer::setSerializer public function Sets the owning Serializer object Overrides SerializerAwareInterface::setSerializer