You are here

public function MenuLinkContentNormalizer::denormalize in Better Normalizers 8

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

Overrides ContentEntityNormalizer::denormalize

File

src/Normalizer/MenuLinkContentNormalizer.php, line 114

Class

MenuLinkContentNormalizer
A normalizer to handle menu-link content links to entities.

Namespace

Drupal\better_normalizers\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = array()) {
  if (isset($data['link']) && is_array($data['link'])) {
    foreach ($data['link'] as $key => $link) {
      try {
        $stub = EntityStub::fromEntityUri($link['uri']);
        if (isset($link['target_uuid'])) {
          if ($entity = $this->entityRepository
            ->loadEntityByUuid($stub
            ->getEntityTypeId(), $link['target_uuid'])) {
            $data['link'][$key]['uri'] = 'entity:' . $stub
              ->getEntityTypeId() . '/' . $entity
              ->id();
          }
        }
      } catch (\InvalidArgumentException $e) {
        continue;
      }
    }
  }
  $entity = parent::denormalize($data, $class, $format, $context);
  return $entity;
}