onEntityTypeDelete example

// Let the schema handler adapt to possible table layout changes.     $this->wrapSchemaException(function D) use ($entity_type$original) {
      $this->getStorageSchema()->onEntityTypeUpdate($entity_type$original);
    });
  }

  /** * {@inheritdoc} */
  public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
    $this->wrapSchemaException(function D) use ($entity_type) {
      $this->getStorageSchema()->onEntityTypeDelete($entity_type);
    });
  }

  /** * {@inheritdoc} */
  public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
    $this->wrapSchemaException(function D) use ($entity_type$field_storage_definitions) {
      $this->getStorageSchema()->onFieldableEntityTypeCreate($entity_type$field_storage_definitions);
    });
  }

  
public function onEntityTypeEvent(EntityTypeEvent $event$event_name) {
    switch ($event_name) {
      case EntityTypeEvents::CREATE:
        $this->onEntityTypeCreate($event->getEntityType());
        break;

      case EntityTypeEvents::UPDATE:
        $this->onEntityTypeUpdate($event->getEntityType()$event->getOriginal());
        break;

      case EntityTypeEvents::DELETE:
        $this->onEntityTypeDelete($event->getEntityType());
        break;
    }
  }

  /** * {@inheritdoc} */
  public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
  }

  /** * {@inheritdoc} */
public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
    $entity_type_id = $entity_type->id();

    // An entity type can be deleted even when its live (in-code) definition has     // been removed from the codebase, so we need to instantiate a custom     // storage handler that uses the passed-in entity type definition.     $storage = $this->entityTypeManager->createHandlerInstance($entity_type->getStorageClass()$entity_type);

    // @todo Forward this to all interested handlers, not only storage, once     // iterating handlers is possible: https://www.drupal.org/node/2332857.     if ($storage instanceof EntityTypeListenerInterface) {
      $storage->onEntityTypeDelete($entity_type);
    }

    $this->entityLastInstalledSchemaRepository->deleteLastInstalledDefinition($entity_type_id);

    $this->eventDispatcher->dispatch(new EntityTypeEvent($entity_type), EntityTypeEvents::DELETE);
    $this->clearCachedDefinitions();
  }

  /** * {@inheritdoc} */
  
$this->assertFalse($event_subscriber->hasEventFired(EntityTypeEvents::UPDATE), 'Entity type update was not dispatched yet.');
    \Drupal::service('entity_type.listener')->onEntityTypeUpdate($updated_entity_type$entity_type);
    $this->assertTrue($event_subscriber->hasEventFired(EntityTypeEvents::UPDATE), 'Entity type update event successfully dispatched.');
    $this->assertTrue($event_subscriber->hasDefinitionBeenUpdated(EntityTypeEvents::UPDATE), 'Last installed entity type definition was updated before the event was fired.');

    // Check that the updated definition can be retrieved from the live entity     // type definitions.     $entity_type = $this->entityTypeManager->getDefinition('entity_test_rev');
    $this->assertEquals(new TranslatableMarkup('Updated entity test rev')$entity_type->getLabel());

    $this->assertFalse($event_subscriber->hasEventFired(EntityTypeEvents::DELETE), 'Entity type delete was not dispatched yet.');
    \Drupal::service('entity_type.listener')->onEntityTypeDelete($entity_type);
    $this->assertTrue($event_subscriber->hasEventFired(EntityTypeEvents::DELETE), 'Entity type delete event successfully dispatched.');
    $this->assertTrue($event_subscriber->hasDefinitionBeenUpdated(EntityTypeEvents::DELETE), 'Last installed entity type definition was deleted before the event was fired.');

    // Check that the deleted entity type can no longer be retrieved from the     // live entity type definitions.     $this->assertNull($this->entityTypeManager->getDefinition('entity_test_rev', FALSE));
  }

  /** * Tests applying single updates. */
  
public function updateEntityType(EntityTypeInterface $entity_type) {
    $original = $this->getEntityType($entity_type->id());
    $this->clearCachedDefinitions();
    $this->entityTypeListener->onEntityTypeUpdate($entity_type$original);
  }

  /** * {@inheritdoc} */
  public function uninstallEntityType(EntityTypeInterface $entity_type) {
    $this->clearCachedDefinitions();
    $this->entityTypeListener->onEntityTypeDelete($entity_type);
  }

  /** * {@inheritdoc} */
  public function installFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions) {
    $this->clearCachedDefinitions();
    foreach ($field_storage_definitions as $name => $field_storage_definition) {
      if ($field_storage_definition instanceof BaseFieldDefinition) {
        $field_storage_definition
          ->setName($name)
          
Home | Imprint | This part of the site doesn't use cookies.