"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/serialization/src/Normalizer/EntityNormalizer.php" (26 Nov 2020, 3571 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "EntityNormalizer.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\serialization\Normalizer;
    4 
    5 use Drupal\Core\Entity\EntityFieldManagerInterface;
    6 use Drupal\Core\Entity\EntityInterface;
    7 use Drupal\Core\Entity\EntityTypeManagerInterface;
    8 use Drupal\Core\Entity\EntityTypeRepositoryInterface;
    9 use Drupal\Core\Entity\FieldableEntityInterface;
   10 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
   11 
   12 /**
   13  * Normalizes/denormalizes Drupal entity objects into an array structure.
   14  */
   15 class EntityNormalizer extends ComplexDataNormalizer implements DenormalizerInterface {
   16 
   17   use FieldableEntityNormalizerTrait;
   18 
   19   /**
   20    * {@inheritdoc}
   21    */
   22   protected $supportedInterfaceOrClass = EntityInterface::class;
   23 
   24   /**
   25    * Constructs an EntityNormalizer object.
   26    *
   27    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   28    *   The entity type manager.
   29    * @param \Drupal\Core\Entity\EntityTypeRepositoryInterface $entity_type_repository
   30    *   The entity type repository.
   31    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   32    *   The entity field manager.
   33    */
   34   public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository = NULL, EntityFieldManagerInterface $entity_field_manager = NULL) {
   35     $this->entityTypeManager = $entity_type_manager;
   36     if (!$entity_type_repository) {
   37       @trigger_error('The entity_type.repository service must be passed to EntityNormalizer::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
   38       $entity_type_repository = \Drupal::service('entity_type.repository');
   39     }
   40     $this->entityTypeRepository = $entity_type_repository;
   41     if (!$entity_field_manager) {
   42       @trigger_error('The entity_field.manager service must be passed to EntityNormalizer::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
   43       $entity_field_manager = \Drupal::service('entity_field.manager');
   44     }
   45     $this->entityFieldManager = $entity_field_manager;
   46   }
   47 
   48   /**
   49    * {@inheritdoc}
   50    */
   51   public function denormalize($data, $class, $format = NULL, array $context = []) {
   52     $entity_type_id = $this->determineEntityTypeId($class, $context);
   53     $entity_type_definition = $this->getEntityTypeDefinition($entity_type_id);
   54 
   55     // The bundle property will be required to denormalize a bundleable
   56     // fieldable entity.
   57     if ($entity_type_definition->entityClassImplements(FieldableEntityInterface::class)) {
   58       // Extract bundle data to pass into entity creation if the entity type uses
   59       // bundles.
   60       if ($entity_type_definition->hasKey('bundle')) {
   61         // Get an array containing the bundle only. This also remove the bundle
   62         // key from the $data array.
   63         $create_params = $this->extractBundleData($data, $entity_type_definition);
   64       }
   65       else {
   66         $create_params = [];
   67       }
   68 
   69       // Create the entity from bundle data only, then apply field values after.
   70       $entity = $this->entityTypeManager->getStorage($entity_type_id)->create($create_params);
   71 
   72       $this->denormalizeFieldData($data, $entity, $format, $context);
   73     }
   74     else {
   75       // Create the entity from all data.
   76       $entity = $this->entityTypeManager->getStorage($entity_type_id)->create($data);
   77     }
   78 
   79     // Pass the names of the fields whose values can be merged.
   80     // @todo https://www.drupal.org/node/2456257 remove this.
   81     $entity->_restSubmittedFields = array_keys($data);
   82 
   83     return $entity;
   84   }
   85 
   86 }