class EntityReferenceItemNormalizer in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php \Drupal\hal\Normalizer\EntityReferenceItemNormalizer
Converts the Drupal entity reference item object to HAL array structure.
Hierarchy
- class \Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer implements SerializerAwareInterface
- class \Drupal\serialization\Normalizer\NormalizerBase implements NormalizerInterface
- class \Drupal\hal\Normalizer\NormalizerBase implements DenormalizerInterface
- class \Drupal\hal\Normalizer\FieldItemNormalizer
- class \Drupal\hal\Normalizer\EntityReferenceItemNormalizer implements UuidReferenceInterface
- class \Drupal\hal\Normalizer\FieldItemNormalizer
- class \Drupal\hal\Normalizer\NormalizerBase implements DenormalizerInterface
- class \Drupal\serialization\Normalizer\NormalizerBase implements NormalizerInterface
Expanded class hierarchy of EntityReferenceItemNormalizer
1 file declares its use of EntityReferenceItemNormalizer
- NormalizerTestBase.php in core/
modules/ hal/ src/ Tests/ NormalizerTestBase.php - Contains \Drupal\hal\Tests\NormalizerTestBase.
1 string reference to 'EntityReferenceItemNormalizer'
- hal.services.yml in core/
modules/ hal/ hal.services.yml - core/modules/hal/hal.services.yml
1 service uses EntityReferenceItemNormalizer
File
- core/
modules/ hal/ src/ Normalizer/ EntityReferenceItemNormalizer.php, line 18 - Contains \Drupal\hal\Normalizer\EntityReferenceItemNormalizer.
Namespace
Drupal\hal\NormalizerView source
class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidReferenceInterface {
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = 'Drupal\\Core\\Field\\Plugin\\Field\\FieldType\\EntityReferenceItem';
/**
* 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;
}
/**
* {@inheritdoc}
*/
public function normalize($field_item, $format = NULL, array $context = array()) {
/** @var $field_item \Drupal\Core\Field\FieldItemInterface */
$target_entity = $field_item
->get('entity')
->getValue();
// If this is not a content entity, let the parent implementation handle it,
// only content entities are supported as embedded resources.
if (!$target_entity instanceof FieldableEntityInterface) {
return parent::normalize($field_item, $format, $context);
}
// 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
->getEntity();
$field_uri = $this->linkManager
->getRelationUri($entity
->getEntityTypeId(), $entity
->bundle(), $field_name, $context);
return array(
'_links' => array(
$field_uri => array(
$link,
),
),
'_embedded' => array(
$field_uri => array(
$embedded,
),
),
);
}
/**
* {@inheritdoc}
*/
protected function constructValue($data, $context) {
$field_item = $context['target_instance'];
$field_definition = $field_item
->getFieldDefinition();
$target_type = $field_definition
->getSetting('target_type');
$id = $this->entityResolver
->resolve($this, $data, $target_type);
if (isset($id)) {
return array(
'target_id' => $id,
);
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function getUuid($data) {
if (isset($data['uuid'])) {
$uuid = $data['uuid'];
// The value may be a nested array like $uuid[0]['value'].
if (is_array($uuid) && isset($uuid[0]['value'])) {
$uuid = $uuid[0]['value'];
}
return $uuid;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityReferenceItemNormalizer:: |
protected | property | The entity resolver. | |
EntityReferenceItemNormalizer:: |
protected | property | The hypermedia link manager. | |
EntityReferenceItemNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides FieldItemNormalizer:: |
|
EntityReferenceItemNormalizer:: |
protected | function |
Build the field item value using the incoming data. Overrides FieldItemNormalizer:: |
|
EntityReferenceItemNormalizer:: |
public | function |
Get the uuid from the data array. Overrides UuidReferenceInterface:: |
|
EntityReferenceItemNormalizer:: |
public | function |
Normalizes an object into a set of arrays/scalars. Overrides FieldItemNormalizer:: |
|
EntityReferenceItemNormalizer:: |
public | function | Constructs an EntityReferenceItemNormalizer object. | |
FieldItemNormalizer:: |
protected | function | Get a translated version of the field item instance. | |
FieldItemNormalizer:: |
public | function |
Denormalizes data back into an object of the given class. Overrides DenormalizerInterface:: |
|
NormalizerBase:: |
protected | property | The formats that the Normalizer can handle. | |
NormalizerBase:: |
protected | function | Checks if the provided format is supported by this normalizer. | |
NormalizerBase:: |
public | function |
Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase:: |
|
NormalizerBase:: |
public | function |
Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase:: |
|
SerializerAwareNormalizer:: |
protected | property | ||
SerializerAwareNormalizer:: |
public | function |
Sets the owning Serializer object. Overrides SerializerAwareInterface:: |