onEntityTypeCreate example

->getMock();
    $schema_handler
      ->expects($this->any())
      ->method('installedStorageSchema')
      ->willReturn($key_value);

    $storage
      ->expects($this->any())
      ->method('getStorageSchema')
      ->willReturn($schema_handler);

    $storage->onEntityTypeCreate($this->entityType);
  }

  /** * Tests getTableMapping() with an empty entity type. * * @covers ::__construct * @covers ::getTableMapping */
  public function testGetTableMappingEmpty() {
    $this->setUpEntityStorage();

    
/** * Listener method for any entity type definition event. * * @param \Drupal\Core\Entity\EntityTypeEvent $event * The field storage definition event object. * @param string $event_name * The event name. */
  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;
    }
  }

  
$this->setUpStorageSchema($expected);

    $table_mapping = new TestSqlContentDefaultTableMapping($this->entityType, $this->storageDefinitions);
    $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions));
    $table_mapping->setExtraColumns('entity_test', ['default_langcode']);

    $this->storageSchema->expects($this->any())
      ->method('getTableMapping')
      ->willReturn($table_mapping);

    $this->assertNull(
      $this->storageSchema->onEntityTypeCreate($this->entityType)
    );
  }

  /** * Tests the schema for revisionable, non-translatable entities. * * @covers ::__construct * @covers ::getEntitySchemaTables * @covers ::initializeBaseTable * @covers ::initializeRevisionTable * @covers ::addTableDefaults * @covers ::getEntityIndexName * @covers ::processIdentifierSchema */


  /** * Installs the storage schema for a specific entity type. * * @param string $entity_type_id * The ID of the entity type. */
  protected function installEntitySchema($entity_type_id) {
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    \Drupal::service('entity_type.listener')->onEntityTypeCreate($entity_type);

    // For test runs, the most common storage backend is a SQL database. For     // this case, ensure the tables got created.     $storage = $entity_type_manager->getStorage($entity_type_id);
    if ($storage instanceof SqlEntityStorageInterface) {
      $tables = $storage->getTableMapping()->getTableNames();
      $db_schema = $this->container->get('database')->schema();
      foreach ($tables as $table) {
        $this->assertTrue($db_schema->tableExists($table), "The entity type table '$table' for the entity type '$entity_type_id' should exist.");
      }
    }
  }
/** * {@inheritdoc} */
  public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
    $entity_type_id = $entity_type->id();

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

    $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
    if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
      $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id$this->entityFieldManager->getFieldStorageDefinitions($entity_type_id));
    }

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

  
class OembedUpdateTest extends UpdatePathTestBase {

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // Because the test manually installs media module, the entity type config     // must be manually installed similar to kernel tests.     $entity_type_manager = \Drupal::entityTypeManager();
    $media = $entity_type_manager->getDefinition('media');
    \Drupal::service('entity_type.listener')->onEntityTypeCreate($media);
    $media_type = $entity_type_manager->getDefinition('media_type');
    \Drupal::service('entity_type.listener')->onEntityTypeCreate($media_type);
  }

  /** * {@inheritdoc} */
  protected function setDatabaseDumpFiles(): void {
    $this->databaseDumpFiles = [
      __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-9.4.0.filled.standard.php.gz',
      __DIR__ . '/../../../fixtures/update/media.php',
      
// If the entity type is supported by Workspaces, add the revision metadata     // field.     if ($this->workspaceManager->isEntityTypeSupported($entity_type)) {
      $this->addRevisionMetadataField($entity_type);
    }
  }

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

  /** * {@inheritdoc} */
  public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    // If the entity type is now supported by Workspaces, add the revision     // metadata field.     if ($this->workspaceManager->isEntityTypeSupported($entity_type) && !$this->workspaceManager->isEntityTypeSupported($original)) {
      $this->addRevisionMetadataField($entity_type);
    }

    

  public function getEntityTypes() {
    return $this->entityLastInstalledSchemaRepository->getLastInstalledDefinitions();
  }

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

  /** * {@inheritdoc} */
  public function updateEntityType(EntityTypeInterface $entity_type) {
    $original = $this->getEntityType($entity_type->id());
    $this->clearCachedDefinitions();
    $this->entityTypeListener->onEntityTypeUpdate($entity_type$original);
  }

  

  public function requiresFieldDataMigration(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
    return $this->getStorageSchema()->requiresFieldDataMigration($storage_definition$original);
  }

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

  /** * {@inheritdoc} */
  public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    // Ensure we have an updated entity type definition.     $this->entityType = $entity_type;
    // The table layout may have changed depending on the new entity type     // definition.

  protected function doEntityUpdate($op$entity_type_id) {
    $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
    $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type_id);
    switch ($op) {
      case EntityDefinitionUpdateManagerInterface::DEFINITION_CREATED:
        \Drupal::service('entity_type.listener')->onEntityTypeCreate($entity_type);
        break;

      case EntityDefinitionUpdateManagerInterface::DEFINITION_UPDATED:
        $original = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledDefinition($entity_type_id);
        $original_field_storage_definitions = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions($entity_type_id);

        \Drupal::service('entity_type.listener')->onFieldableEntityTypeUpdate($entity_type$original$field_storage_definitions$original_field_storage_definitions);
        break;
    }
  }

  
$this->assertTrue($event_subscriber->hasDefinitionBeenUpdated(FieldStorageDefinitionEvents::DELETE), 'Last installed field storage definition was deleted before the event was fired.');

    // Check that the deleted field can no longer be retrieved from the live     // field storage definitions.     $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions('entity_test_rev');
    $this->assertArrayNotHasKey('field_storage_test', $field_storage_definitions);

    // Test entity type events.     $entity_type = $this->entityTypeManager->getDefinition('entity_test_rev');

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

    $updated_entity_type = clone $entity_type;
    $updated_entity_type->set('label', new TranslatableMarkup('Updated entity test rev'));
    $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
Home | Imprint | This part of the site doesn't use cookies.