You are here

class CropNormalizer in Replication 8

Hierarchy

Expanded class hierarchy of CropNormalizer

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

File

src/Normalizer/CropNormalizer.php, line 7

Namespace

Drupal\replication\Normalizer
View source
class CropNormalizer extends ContentEntityNormalizer {

  /**
   * @var string[]
   */
  protected $supportedInterfaceOrClass = [
    '\\Drupal\\crop\\Entity\\Crop',
  ];

  /**
   * {@inheritdoc}
   */
  public function normalize($entity, $format = NULL, array $context = []) {
    $data = parent::normalize($entity, $format, $context);
    $cropped_entity_type_id = $entity->entity_type->value;
    $cropped_entity_storage = $this->entityManager
      ->getStorage($cropped_entity_type_id);
    $cropped_entity = $cropped_entity_storage
      ->load($entity->entity_id->value);
    $entity_languages = $entity
      ->getTranslationLanguages();
    foreach ($entity_languages as $entity_language) {

      // Store all needed information about entity referenced by 'entity_id' field.
      $data[$entity_language
        ->getId()]['entity_id'][0]['crop_target_uuid'] = $cropped_entity
        ->uuid();
      $data[$entity_language
        ->getId()]['entity_id'][0]['crop_target_entity_type_id'] = $cropped_entity
        ->getEntityTypeId();
      $data[$entity_language
        ->getId()]['entity_id'][0]['crop_target_bundle'] = $cropped_entity
        ->bundle();
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    foreach ($data as $key => &$translation) {
      if (in_array($key[0], [
        '_',
        '@',
      ])) {
        continue;
      }
      if (!empty($translation['entity_id'][0]['crop_target_uuid'])) {
        $uuid_index = isset($context['workspace']) && $context['workspace'] instanceof WorkspaceInterface ? $this->indexFactory
          ->get('multiversion.entity_index.uuid', $context['workspace']) : $this->indexFactory
          ->get('multiversion.entity_index.uuid');
        if ($target_entity_info = $uuid_index
          ->get($translation['entity_id'][0]['crop_target_uuid'])) {
          $translation['entity_id'][0]['value'] = $target_entity_info['entity_id'];
        }
        else {

          // Create a stub for entity referenced by 'entity_id' field.
          $options['target_type'] = $translation['entity_id'][0]['crop_target_entity_type_id'];
          $selection_instance = $this->selectionManager
            ->getInstance($options);
          $target_entity = $selection_instance
            ->createNewEntity($options['target_type'], $translation['entity_id'][0]['crop_target_bundle'], rand(), 1);
          $target_entity->uri->value = "uri:stub";

          // Set the target workspace if we have it in context.
          if (isset($context['workspace']) && $context['workspace'] instanceof WorkspaceInterface && $target_entity
            ->getEntityType()
            ->get('workspace') !== FALSE) {
            $target_entity->workspace->target_id = $context['workspace']
              ->id();
          }
          $target_entity->uuid->value = $translation['entity_id'][0]['crop_target_uuid'];
          $target_entity->_rev->is_stub = TRUE;
          $target_entity
            ->save();
          $translation['entity_id'][0]['value'] = $target_entity
            ->id();
        }
      }
    }
    return parent::denormalize($data, $class, $format);
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDenormalization($data, $type, $format = NULL) {
    if (in_array($type, [
      'Drupal\\Core\\Entity\\ContentEntityInterface',
      'Drupal\\crop\\Entity\\Crop',
    ], TRUE)) {
      if (!empty($data['@type']) && $data['@type'] == 'crop') {
        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::$format protected property Overrides NormalizerBase::$format 3
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
CropNormalizer::$supportedInterfaceOrClass protected property Overrides ContentEntityNormalizer::$supportedInterfaceOrClass
CropNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides ContentEntityNormalizer::denormalize
CropNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides ContentEntityNormalizer::normalize
CropNormalizer::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
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.
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