You are here

class ContentEntityNormalizer in Zircon Profile 8

Same name in this branch
  1. 8 core/modules/hal/src/Normalizer/ContentEntityNormalizer.php \Drupal\hal\Normalizer\ContentEntityNormalizer
  2. 8 core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php \Drupal\serialization\Normalizer\ContentEntityNormalizer
Same name and namespace in other branches
  1. 8.0 core/modules/hal/src/Normalizer/ContentEntityNormalizer.php \Drupal\hal\Normalizer\ContentEntityNormalizer

Converts the Drupal entity object structure to a HAL array structure.

Hierarchy

Expanded class hierarchy of ContentEntityNormalizer

1 file declares its use of ContentEntityNormalizer
NormalizerTestBase.php in core/modules/hal/src/Tests/NormalizerTestBase.php
Contains \Drupal\hal\Tests\NormalizerTestBase.
1 string reference to 'ContentEntityNormalizer'
hal.services.yml in core/modules/hal/hal.services.yml
core/modules/hal/hal.services.yml
1 service uses ContentEntityNormalizer
serializer.normalizer.entity.hal in core/modules/hal/hal.services.yml
Drupal\hal\Normalizer\ContentEntityNormalizer

File

core/modules/hal/src/Normalizer/ContentEntityNormalizer.php, line 20
Contains \Drupal\hal\Normalizer\ContentEntityNormalizer.

Namespace

Drupal\hal\Normalizer
View source
class ContentEntityNormalizer extends NormalizerBase {

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

  /**
   * The hypermedia link manager.
   *
   * @var \Drupal\rest\LinkManager\LinkManagerInterface
   */
  protected $linkManager;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs an ContentEntityNormalizer object.
   *
   * @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
   *   The hypermedia link manager.
   */
  public function __construct(LinkManagerInterface $link_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) {
    $this->linkManager = $link_manager;
    $this->entityManager = $entity_manager;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($entity, $format = NULL, array $context = array()) {
    $context += array(
      'account' => NULL,
      'included_fields' => NULL,
    );

    // Create the array of normalized fields, starting with the URI.

    /** @var $entity \Drupal\Core\Entity\ContentEntityInterface */
    $normalized = array(
      '_links' => array(
        'self' => array(
          'href' => $this
            ->getEntityUri($entity),
        ),
        'type' => array(
          'href' => $this->linkManager
            ->getTypeUri($entity
            ->getEntityTypeId(), $entity
            ->bundle(), $context),
        ),
      ),
    );

    // If the fields to use were specified, only output those field values.
    // Otherwise, output all field values except internal ID.
    if (isset($context['included_fields'])) {
      $fields = array();
      foreach ($context['included_fields'] as $field_name) {
        $fields[] = $entity
          ->get($field_name);
      }
    }
    else {
      $fields = $entity
        ->getFields();
    }

    // Ignore the entity ID and revision ID.
    $exclude = array(
      $entity
        ->getEntityType()
        ->getKey('id'),
      $entity
        ->getEntityType()
        ->getKey('revision'),
    );
    foreach ($fields as $field) {

      // Continue if this is an excluded field or the current user does not have
      // access to view it.
      if (in_array($field
        ->getFieldDefinition()
        ->getName(), $exclude) || !$field
        ->access('view', $context['account'])) {
        continue;
      }
      $normalized_property = $this->serializer
        ->normalize($field, $format, $context);
      $normalized = NestedArray::mergeDeep($normalized, $normalized_property);
    }
    return $normalized;
  }

  /**
   * Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize().
   *
   * @param array $data
   *   Entity data to restore.
   * @param string $class
   *   Unused, entity_create() is used to instantiate entity objects.
   * @param string $format
   *   Format the given data was extracted from.
   * @param array $context
   *   Options available to the denormalizer. Keys that can be used:
   *   - request_method: if set to "patch" the denormalization will clear out
   *     all default values for entity fields before applying $data to the
   *     entity.
   *
   * @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException
   */
  public function denormalize($data, $class, $format = NULL, array $context = array()) {

    // Get type, necessary for determining which bundle to create.
    if (!isset($data['_links']['type'])) {
      throw new UnexpectedValueException('The type link relation must be specified.');
    }

    // Create the entity.
    $typed_data_ids = $this
      ->getTypedDataIds($data['_links']['type'], $context);
    $entity_type = $this->entityManager
      ->getDefinition($typed_data_ids['entity_type']);
    $langcode_key = $entity_type
      ->getKey('langcode');
    $values = array();

    // Figure out the language to use.
    if (isset($data[$langcode_key])) {
      $values[$langcode_key] = $data[$langcode_key][0]['value'];

      // Remove the langcode so it does not get iterated over below.
      unset($data[$langcode_key]);
    }
    if ($entity_type
      ->hasKey('bundle')) {
      $bundle_key = $entity_type
        ->getKey('bundle');
      $values[$bundle_key] = $typed_data_ids['bundle'];

      // Unset the bundle key from data, if it's there.
      unset($data[$bundle_key]);
    }
    $entity = $this->entityManager
      ->getStorage($typed_data_ids['entity_type'])
      ->create($values);

    // Remove links from data array.
    unset($data['_links']);

    // Get embedded resources and remove from data array.
    $embedded = array();
    if (isset($data['_embedded'])) {
      $embedded = $data['_embedded'];
      unset($data['_embedded']);
    }

    // Flatten the embedded values.
    foreach ($embedded as $relation => $field) {
      $field_ids = $this->linkManager
        ->getRelationInternalIds($relation);
      if (!empty($field_ids)) {
        $field_name = $field_ids['field_name'];
        $data[$field_name] = $field;
      }
    }

    // Pass the names of the fields whose values can be merged.
    $entity->_restSubmittedFields = array_keys($data);

    // Iterate through remaining items in data array. These should all
    // correspond to fields.
    foreach ($data as $field_name => $field_data) {
      $items = $entity
        ->get($field_name);

      // Remove any values that were set as a part of entity creation (e.g
      // uuid). If the incoming field data is set to an empty array, this will
      // also have the effect of emptying the field in REST module.
      $items
        ->setValue(array());
      if ($field_data) {

        // Denormalize the field data into the FieldItemList object.
        $context['target_instance'] = $items;
        $this->serializer
          ->denormalize($field_data, get_class($items), $format, $context);
      }
    }
    return $entity;
  }

  /**
   * Constructs the entity URI.
   *
   * @param \Drupal\Core\Entity\EntityInterface
   *   The entity.
   * @return string
   *   The entity URI.
   */
  protected function getEntityUri(EntityInterface $entity) {

    // Some entity types don't provide a canonical link template, at least call
    // out to ->url().
    if ($entity
      ->isNew() || !$entity
      ->hasLinkTemplate('canonical')) {
      return $entity
        ->url('canonical', []);
    }
    $url = $entity
      ->urlInfo('canonical', [
      'absolute' => TRUE,
    ]);
    return $url
      ->setRouteParameter('_format', 'hal_json')
      ->toString();
  }

  /**
   * 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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntityNormalizer::$entityManager protected property The entity manager.
ContentEntityNormalizer::$linkManager protected property The hypermedia link manager.
ContentEntityNormalizer::$moduleHandler protected property The module handler.
ContentEntityNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass 1
ContentEntityNormalizer::denormalize public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize(). Overrides DenormalizerInterface::denormalize 1
ContentEntityNormalizer::getEntityUri protected function Constructs the entity URI.
ContentEntityNormalizer::getTypedDataIds protected function Gets the typed data IDs for a type URI.
ContentEntityNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides NormalizerInterface::normalize 1
ContentEntityNormalizer::__construct public function Constructs an ContentEntityNormalizer object. 1
NormalizerBase::$formats protected property The formats that the Normalizer can handle.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase::supportsNormalization
SerializerAwareNormalizer::$serializer protected property
SerializerAwareNormalizer::setSerializer public function Sets the owning Serializer object. Overrides SerializerAwareInterface::setSerializer