You are here

public function ContentEntityNormalizer::denormalize in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/hal/src/Normalizer/ContentEntityNormalizer.php \Drupal\hal\Normalizer\ContentEntityNormalizer::denormalize()

Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize().

Parameters

array $data: Entity data to restore.

string $class: Unused parameter.

string $format: Format the given data was extracted from.

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.

Return value

\Drupal\Core\Entity\EntityInterface An unserialized entity object containing the data in $data.

Throws

\Symfony\Component\Serializer\Exception\UnexpectedValueException

File

core/modules/hal/src/Normalizer/ContentEntityNormalizer.php, line 141

Class

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

Namespace

Drupal\hal\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {

  // 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
    ->getEntityTypeDefinition($typed_data_ids['entity_type']);
  $default_langcode_key = $entity_type
    ->getKey('default_langcode');
  $langcode_key = $entity_type
    ->getKey('langcode');
  $values = [];

  // Figure out the language to use.
  if (isset($data[$default_langcode_key])) {

    // Find the field item for which the default_langcode value is set to 1 and
    // set the langcode the right default language.
    foreach ($data[$default_langcode_key] as $item) {
      if (!empty($item['value']) && isset($item['lang'])) {
        $values[$langcode_key] = $item['lang'];
        break;
      }
    }

    // Remove the default langcode so it does not get iterated over below.
    unset($data[$default_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->entityTypeManager
    ->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 = [];
  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;
    }
  }
  $this
    ->denormalizeFieldData($data, $entity, $format, $context);

  // Pass the names of the fields whose values can be merged.
  // @todo https://www.drupal.org/node/2456257 remove this.
  $entity->_restSubmittedFields = array_keys($data);
  return $entity;
}