You are here

public function FieldItemNormalizer::denormalize in Zircon Profile 8

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

Denormalizes data back into an object of the given class.

Parameters

mixed $data data to restore:

string $class the expected class to instantiate:

string $format format the given data was extracted from:

array $context options available to the denormalizer:

Return value

object

Overrides DenormalizerInterface::denormalize

File

core/modules/hal/src/Normalizer/FieldItemNormalizer.php, line 47
Contains \Drupal\hal\Normalizer\FieldItemNormalizer.

Class

FieldItemNormalizer
Converts the Drupal field item object structure to HAL array structure.

Namespace

Drupal\hal\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = array()) {
  if (!isset($context['target_instance'])) {
    throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
  }
  if ($context['target_instance']
    ->getParent() == NULL) {
    throw new InvalidArgumentException('The field item passed in via $context[\'target_instance\'] must have a parent set.');
  }
  $field_item = $context['target_instance'];

  // If this field is translatable, we need to create a translated instance.
  if (isset($data['lang'])) {
    $langcode = $data['lang'];
    unset($data['lang']);
    $field_definition = $field_item
      ->getFieldDefinition();
    if ($field_definition
      ->isTranslatable()) {
      $field_item = $this
        ->createTranslatedInstance($field_item, $langcode);
    }
  }
  $field_item
    ->setValue($this
    ->constructValue($data, $context));
  return $field_item;
}