/** * {@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))); }
/** * Gets the typed data IDs for a type URI. * * @param array $types * The type array(s) (value of the 'type' attribute of the incoming data). * @param array $context * Context from the normalizer/serializer operation. * * @return array * The typed data IDs. * */ protected function getTypedDataIds($types, $context = array()) { // The 'type' can potentially contain an array of type objects. By default, // Drupal only uses a single type in serializing, but allows for multiple // types when deserializing. if (isset($types['href'])) { $types = array($types); } foreach ($types as $type) { if (!isset($type['href'])) { throw new UnexpectedValueException('Type must contain an \'href\' attribute.'); } $type_uri = $type['href']; // Check whether the URI corresponds to a known type on this site. Break // once one does. if ($typed_data_ids = $this->linkManager->getTypeInternalIds($type['href'], $context)) { break; } } // If none of the URIs correspond to an entity type on this site, no entity // can be created. Throw an exception. if (empty($typed_data_ids)) { throw new UnexpectedValueException(sprintf('Type %s does not correspond to an entity on this site.', $type_uri)); } return $typed_data_ids; }
/** * {@inheritdoc} */ public function exportContentWithReferences($entity_type_id, $entity_id) { $storage = $this->entityManager->getStorage($entity_type_id); $entity = $storage->load($entity_id); if (!$entity) { throw new \InvalidArgumentException(SafeMarkup::format('Entity @type with ID @id does not exist', ['@type' => $entity_type_id, '@id' => $entity_id])); } /** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */ $entities = [$entity]; $entities = array_merge($entities, $this->getEntityReferencesRecursive($entity)); $serialized_entities_per_type = []; $this->linkManager->setLinkDomain(static::LINK_DOMAIN); // Serialize all entities and key them by entity TYPE and uuid. foreach ($entities as $entity) { $serialized_entities_per_type[$entity->getEntityTypeId()][$entity->uuid()] = $this->serializer->serialize($entity, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]); } $this->linkManager->setLinkDomain(FALSE); return $serialized_entities_per_type; }
/** * Gets the relation URI of the field containing an item. * * The relation URI is used as a property key when building the HAL structure. * * @param FieldItemInterface $field_item * The field item that is being normalized. * * @return string * The relation URI of the field. */ protected function getFieldRelationUri(FieldItemInterface $field_item) { $field_name = $field_item->getParent()->getName(); $entity = $field_item->getEntity(); return $this->linkManager->getRelationUri($entity->getEntityTypeId(), $entity->bundle(), $field_name); }