You are here

class MenuLinkContentNormalizer in Replication 8

Hierarchy

Expanded class hierarchy of MenuLinkContentNormalizer

1 string reference to 'MenuLinkContentNormalizer'
replication.services.yml in ./replication.services.yml
replication.services.yml
1 service uses MenuLinkContentNormalizer
replication.normalizer.menu_link_content in ./replication.services.yml
Drupal\replication\Normalizer\MenuLinkContentNormalizer

File

src/Normalizer/MenuLinkContentNormalizer.php, line 8

Namespace

Drupal\replication\Normalizer
View source
class MenuLinkContentNormalizer extends ContentEntityNormalizer implements DenormalizerInterface {

  /**
   * @var string[]
   */
  protected $supportedInterfaceOrClass = [
    'Drupal\\menu_link_content\\MenuLinkContentInterface',
  ];

  /**
   * @var string[]
   */
  protected $format = [
    'json',
  ];

  /**
   * {@inheritdoc}
   */
  public function normalize($data, $format = NULL, array $context = []) {
    $normalized = parent::normalize($data, $format, $context);
    return $normalized;
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    $denormalized = parent::denormalize($data, $class, $format, $context);
    $entity_type_id = 'menu_link_content';
    $parent = $denormalized instanceof MenuLinkContentInterface ? $denormalized
      ->getParentId() : NULL;
    if (!empty($parent) && strpos($parent, $entity_type_id) === 0) {
      list($type, $uuid, ) = explode(':', $parent);
      if ($type === 'menu_link_content' && $uuid) {
        $storage = $this->entityManager
          ->getStorage($entity_type_id);
        if (!empty($context['workspace'])) {
          $storage
            ->useWorkspace($context['workspace']
            ->id());
        }
        $parent = $storage
          ->loadByProperties([
          'uuid' => $uuid,
        ]);
        $parent = reset($parent);
        if ($parent instanceof MenuLinkContentInterface && $parent
          ->id()) {
          $denormalized->parent->value = $type . ':' . $uuid . ':' . $parent
            ->id();
        }
        elseif (!$parent) {

          // Create a new menu link as stub.
          $selection_instance = $this->selectionManager
            ->getInstance([
            'target_type' => $entity_type_id,
          ]);
          $parent = $selection_instance
            ->createNewEntity($entity_type_id, $entity_type_id, rand(), 1);

          // Indicate that this revision is a stub.
          $parent->_rev->is_stub = TRUE;
          $parent->uuid->value = $uuid;
          $parent->langcode->value = $data['@context']['@language'];
          $parent->link->uri = 'internal:/';
          if (!empty($context['workspace'])) {
            $parent->workspace->entity = $context['workspace'];
          }
          $parent->menu_name->value = $denormalized
            ->getMenuName();
          $parent
            ->save();
          $denormalized->parent->value = $type . ':' . $uuid . ':' . $parent
            ->id();
        }
        $storage
          ->useWorkspace(NULL);
      }
    }
    return $denormalized;
  }
  public function supportsDenormalization($data, $type, $format = NULL) {
    if (in_array($type, [
      'Drupal\\menu_link_content\\MenuLinkContentInterface',
      'Drupal\\Core\\Entity\\ContentEntityInterface',
    ], true)) {
      if (isset($data['@type']) && $data['@type'] == 'menu_link_content') {
        return TRUE;
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
ContentEntityNormalizer::$dispatcher protected property
ContentEntityNormalizer::$indexFactory protected property
ContentEntityNormalizer::$languageManager protected property
ContentEntityNormalizer::$moduleHandler private property
ContentEntityNormalizer::$selectionManager protected property
ContentEntityNormalizer::$usersMapping protected property
ContentEntityNormalizer::createEntityInstance private function Handles entity creation for fieldable and non-fieldable entities.
ContentEntityNormalizer::denormalizeMenuLinkParent protected function
ContentEntityNormalizer::denormalizeTranslation private function
ContentEntityNormalizer::__construct public function 1
FieldableEntityNormalizerTrait::$entityFieldManager protected property The entity field manager.
FieldableEntityNormalizerTrait::$entityTypeManager protected property The entity type manager. 1
FieldableEntityNormalizerTrait::$entityTypeRepository protected property The entity type repository.
FieldableEntityNormalizerTrait::constructValue protected function Build the field item value using the incoming data. 7
FieldableEntityNormalizerTrait::denormalizeFieldData protected function Denormalizes entity data by denormalizing each field individually.
FieldableEntityNormalizerTrait::determineEntityTypeId protected function Determines the entity type ID to denormalize as.
FieldableEntityNormalizerTrait::extractBundleData protected function Denormalizes the bundle property so entity creation can use it.
FieldableEntityNormalizerTrait::getEntityFieldManager protected function Returns the entity field manager.
FieldableEntityNormalizerTrait::getEntityTypeDefinition protected function Gets the entity type definition.
FieldableEntityNormalizerTrait::getEntityTypeManager protected function Returns the entity type manager.
FieldableEntityNormalizerTrait::getEntityTypeRepository protected function Returns the entity type repository.
MenuLinkContentNormalizer::$format protected property Overrides ContentEntityNormalizer::$format
MenuLinkContentNormalizer::$supportedInterfaceOrClass protected property Overrides ContentEntityNormalizer::$supportedInterfaceOrClass
MenuLinkContentNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides ContentEntityNormalizer::denormalize
MenuLinkContentNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides ContentEntityNormalizer::normalize
MenuLinkContentNormalizer::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1