EntityReferenceItemNormalizer.php 4.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\hal\Normalizer\EntityReferenceItemNormalizer.
 */

namespace Drupal\hal\Normalizer;

10
use Drupal\Core\Entity\FieldableEntityInterface;
11
12
use Drupal\rest\LinkManager\LinkManagerInterface;
use Drupal\serialization\EntityResolver\EntityResolverInterface;
13
14
use Drupal\serialization\EntityResolver\UuidReferenceInterface;

15
16
17
/**
 * Converts the Drupal entity reference item object to HAL array structure.
 */
18
class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidReferenceInterface {
19
20
21
22
23
24

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

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  /**
   * The hypermedia link manager.
   *
   * @var \Drupal\rest\LinkManager\LinkManagerInterface
   */
  protected $linkManager;

  /**
   * The entity resolver.
   *
   * @var \Drupal\serialization\EntityResolver\EntityResolverInterface
   */
  protected $entityResolver;

  /**
   * Constructs an EntityReferenceItemNormalizer object.
   *
   * @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
   *   The hypermedia link manager.
   * @param \Drupal\serialization\EntityResolver\EntityResolverInterface $entity_Resolver
   *   The entity resolver.
   */
  public function __construct(LinkManagerInterface $link_manager, EntityResolverInterface $entity_Resolver) {
    $this->linkManager = $link_manager;
    $this->entityResolver = $entity_Resolver;
  }

54
55
56
57
  /**
   * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
   */
  public function normalize($field_item, $format = NULL, array $context = array()) {
58
    /** @var $field_item \Drupal\Core\Field\FieldItemInterface */
59
60
    $target_entity = $field_item->get('entity')->getValue();

61
62
    // If this is not a content entity, let the parent implementation handle it,
    // only content entities are supported as embedded resources.
63
    if (!($target_entity instanceof FieldableEntityInterface)) {
64
65
66
      return parent::normalize($field_item, $format, $context);
    }

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    // 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();
87
    $entity = $field_item->getEntity();
88
    $field_uri = $this->linkManager->getRelationUri($entity->getEntityTypeId(), $entity->bundle(), $field_name);
89
90
91
92
93
94
95
96
97
98
    return array(
      '_links' => array(
        $field_uri => array($link),
      ),
      '_embedded' => array(
        $field_uri => array($embedded),
      ),
    );
  }

99
  /**
100
   * Overrides \Drupal\hal\Normalizer\FieldItemNormalizer::constructValue().
101
   */
102
103
  protected function constructValue($data, $context) {
    $field_item = $context['target_instance'];
104
    $field_definition = $field_item->getFieldDefinition();
105
    $target_type = $field_definition->getSetting('target_type');
106
107
    $id = $this->entityResolver->resolve($this, $data, $target_type);
    if (isset($id)) {
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
      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;
    }
124
125
  }

126
}