class EntityReferenceItemNormalizer

Converts the Drupal entity reference item object to HAL array structure.

Hierarchy

Expanded class hierarchy of EntityReferenceItemNormalizer

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

File

drupal/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php, line 15
Contains \Drupal\hal\Normalizer\EntityReferenceItemNormalizer.

Namespace

Drupal\hal\Normalizer
View source
class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidReferenceInterface {

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

  /**
   * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
   */
  public function normalize($field_item, $format = NULL, array $context = array()) {
    $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.
    $langcode = isset($context['langcode']) ? $context['langcode'] : NULL;
    unset($context['langcode']);
    $context['included_fields'] = array(
      'uuid',
    );

    // Normalize the target entity.
    $embedded = $this->serializer
      ->normalize($target_entity, $format, $context);
    $link = $embedded['_links']['self'];

    // If the field is translatable, add the langcode to the link relation
    // object. This does not indicate the language of the target entity.
    if ($langcode) {
      $embedded['lang'] = $link['lang'] = $langcode;
    }

    // The returned structure will be recursively merged into the normalized
    // entity so that the items are properly added to the _links and _embedded
    // objects.
    $field_name = $field_item
      ->getParent()
      ->getName();
    $entity = $field_item
      ->getRoot();
    $field_uri = $this->linkManager
      ->getRelationUri($entity
      ->entityType(), $entity
      ->bundle(), $field_name);
    return array(
      '_links' => array(
        $field_uri => array(
          $link,
        ),
      ),
      '_embedded' => array(
        $field_uri => array(
          $embedded,
        ),
      ),
    );
  }

  /**
   * Overrides \Drupal\hal\Normalizer\FieldItemNormalizer::constructValue().
   */
  protected function constructValue($data, $context) {
    $field_item = $context['target_instance'];
    $field_definition = $field_item
      ->getDefinition();
    $target_type = $field_definition['settings']['target_type'];
    if ($id = $this->entityResolver
      ->resolve($this, $data, $target_type)) {
      return array(
        'target_id' => $id,
      );
    }
    return NULL;
  }

  /**
   * Implements \Drupal\serialization\EntityResolver\UuidReferenceInterface::getUuid().
   */
  public function getUuid($data) {
    if (isset($data['uuid'])) {
      $uuid = $data['uuid'];
      if (is_array($uuid)) {
        $uuid = reset($uuid);
      }
      return $uuid;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceItemNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides FieldItemNormalizer::$supportedInterfaceOrClass
EntityReferenceItemNormalizer::constructValue protected function Overrides \Drupal\hal\Normalizer\FieldItemNormalizer::constructValue(). Overrides FieldItemNormalizer::constructValue
EntityReferenceItemNormalizer::getUuid public function Implements \Drupal\serialization\EntityResolver\UuidReferenceInterface::getUuid(). Overrides UuidReferenceInterface::getUuid
EntityReferenceItemNormalizer::normalize public function Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize() Overrides FieldItemNormalizer::normalize
FieldItemNormalizer::createTranslatedInstance protected function Get a translated version of the field item instance.
FieldItemNormalizer::denormalize public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize() Overrides DenormalizerInterface::denormalize
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