"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Entity/EntityTypeListener.php" (26 Nov 2020, 7631 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 "EntityTypeListener.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.0.8_vs_9.1.0-rc1.

    1 <?php
    2 
    3 namespace Drupal\Core\Entity;
    4 
    5 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
    6 
    7 /**
    8  * Reacts to entity type CRUD on behalf of the Entity system.
    9  *
   10  * @see \Drupal\Core\Entity\EntityTypeEvents
   11  */
   12 class EntityTypeListener implements EntityTypeListenerInterface {
   13 
   14   /**
   15    * The entity type manager.
   16    *
   17    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   18    */
   19   protected $entityTypeManager;
   20 
   21   /**
   22    * The entity field manager.
   23    *
   24    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   25    */
   26   protected $entityFieldManager;
   27 
   28   /**
   29    * The event dispatcher.
   30    *
   31    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   32    */
   33   protected $eventDispatcher;
   34 
   35   /**
   36    * The entity last installed schema repository.
   37    *
   38    * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
   39    */
   40   protected $entityLastInstalledSchemaRepository;
   41 
   42   /**
   43    * Constructs a new EntityTypeListener.
   44    *
   45    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   46    *   The entity type manager.
   47    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   48    *   The entity field manager.
   49    * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   50    *   The event dispatcher.
   51    * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository
   52    *   The entity last installed schema repository.
   53    */
   54   public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository) {
   55     $this->entityTypeManager = $entity_type_manager;
   56     $this->entityFieldManager = $entity_field_manager;
   57     $this->eventDispatcher = $event_dispatcher;
   58     $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
   59   }
   60 
   61   /**
   62    * {@inheritdoc}
   63    */
   64   public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
   65     $entity_type_id = $entity_type->id();
   66 
   67     // @todo Forward this to all interested handlers, not only storage, once
   68     //   iterating handlers is possible: https://www.drupal.org/node/2332857.
   69     $storage = $this->entityTypeManager->getStorage($entity_type_id);
   70     if ($storage instanceof EntityTypeListenerInterface) {
   71       $storage->onEntityTypeCreate($entity_type);
   72     }
   73 
   74     $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
   75     if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
   76       $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id));
   77     }
   78 
   79     $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type));
   80     $this->clearCachedDefinitions();
   81   }
   82 
   83   /**
   84    * {@inheritdoc}
   85    */
   86   public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
   87     $entity_type_id = $entity_type->id();
   88 
   89     // @todo Forward this to all interested handlers, not only storage, once
   90     //   iterating handlers is possible: https://www.drupal.org/node/2332857.
   91     $storage = $this->entityTypeManager->createHandlerInstance($entity_type->getStorageClass(), $entity_type);
   92     if ($storage instanceof EntityTypeListenerInterface) {
   93       $storage->onFieldableEntityTypeCreate($entity_type, $field_storage_definitions);
   94     }
   95 
   96     $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
   97     if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
   98       $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions);
   99     }
  100 
  101     $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type));
  102     $this->clearCachedDefinitions();
  103   }
  104 
  105   /**
  106    * {@inheritdoc}
  107    */
  108   public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
  109     // An entity type can be updated even when its live (in-code) definition has
  110     // been removed from the codebase, so we need to instantiate a custom
  111     // storage handler that uses the passed-in entity type definition.
  112     $storage = $this->entityTypeManager->createHandlerInstance($entity_type->getStorageClass(), $entity_type);
  113 
  114     // @todo Forward this to all interested handlers, not only storage, once
  115     //   iterating handlers is possible: https://www.drupal.org/node/2332857.
  116     if ($storage instanceof EntityTypeListenerInterface) {
  117       $storage->onEntityTypeUpdate($entity_type, $original);
  118     }
  119 
  120     $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
  121 
  122     $this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original));
  123     $this->clearCachedDefinitions();
  124   }
  125 
  126   /**
  127    * {@inheritdoc}
  128    */
  129   public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
  130     $entity_type_id = $entity_type->id();
  131 
  132     // An entity type can be deleted even when its live (in-code) definition has
  133     // been removed from the codebase, so we need to instantiate a custom
  134     // storage handler that uses the passed-in entity type definition.
  135     $storage = $this->entityTypeManager->createHandlerInstance($entity_type->getStorageClass(), $entity_type);
  136 
  137     // @todo Forward this to all interested handlers, not only storage, once
  138     //   iterating handlers is possible: https://www.drupal.org/node/2332857.
  139     if ($storage instanceof EntityTypeListenerInterface) {
  140       $storage->onEntityTypeDelete($entity_type);
  141     }
  142 
  143     $this->entityLastInstalledSchemaRepository->deleteLastInstalledDefinition($entity_type_id);
  144 
  145     $this->eventDispatcher->dispatch(EntityTypeEvents::DELETE, new EntityTypeEvent($entity_type));
  146     $this->clearCachedDefinitions();
  147   }
  148 
  149   /**
  150    * {@inheritdoc}
  151    */
  152   public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
  153     $entity_type_id = $entity_type->id();
  154 
  155     // @todo Forward this to all interested handlers, not only storage, once
  156     //   iterating handlers is possible: https://www.drupal.org/node/2332857.
  157     $storage = $this->entityTypeManager->createHandlerInstance($entity_type->getStorageClass(), $entity_type);
  158     if ($storage instanceof EntityTypeListenerInterface) {
  159       $storage->onFieldableEntityTypeUpdate($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox);
  160     }
  161 
  162     if ($sandbox === NULL || (isset($sandbox['#finished']) && $sandbox['#finished'] == 1)) {
  163       $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
  164       if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
  165         $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions);
  166       }
  167 
  168       $this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original));
  169       $this->clearCachedDefinitions();
  170     }
  171   }
  172 
  173   /**
  174    * Clears necessary caches to apply entity/field definition updates.
  175    */
  176   protected function clearCachedDefinitions() {
  177     $this->entityTypeManager->clearCachedDefinitions();
  178     $this->entityFieldManager->clearCachedFieldDefinitions();
  179   }
  180 
  181 }