onEntityTypeUpdate example

/** * {@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.     $this->initTableLayout();
    // 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);
    });
  }

  
$this->assertTrue($this->entityDefinitionUpdateManager->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
    $expected = [
      'entity_test_update' => [
        t('The %entity_type entity type needs to be updated.', ['%entity_type' => $this->entityTypeManager->getDefinition('entity_test_update')->getLabel()]),
      ],
    ];
    $this->assertEquals($expected$this->entityDefinitionUpdateManager->getChangeSummary(), 'EntityDefinitionUpdateManager reports the expected change summary.');

    // Run the update and ensure the new index is created.     $entity_type = \Drupal::entityTypeManager()->getDefinition('entity_test_update');
    $original = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledDefinition('entity_test_update');
    \Drupal::service('entity_type.listener')->onEntityTypeUpdate($entity_type$original);
    $this->assertTrue($this->database->schema()->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index created.');

    // Remove the index and ensure the update manager reports that as an     // update to the entity type.     $this->removeEntityIndex();
    $this->assertTrue($this->entityDefinitionUpdateManager->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
    $expected = [
      'entity_test_update' => [
        t('The %entity_type entity type needs to be updated.', ['%entity_type' => $this->entityTypeManager->getDefinition('entity_test_update')->getLabel()]),
      ],
    ];
    

  public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    // An entity type can be updated 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->onEntityTypeUpdate($entity_type$original);
    }

    $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);

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

  /** * {@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);
  }

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

  /** * {@inheritdoc} */
// We are only removing a revision metadata key so we don't need to go       // through the entity update process.       $entity_type->setRevisionMetadataKey('workspace', NULL);
      $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
    }
  }

  /** * {@inheritdoc} */
  public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
    $this->onEntityTypeUpdate($entity_type$original);
  }

  /** * {@inheritdoc} */
  public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
    // Nothing to do here.   }

  /** * Adds the 'workspace' revision metadata field to an entity type. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type that has been installed or updated. */

  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} */
  
$this->dbSchemaHandler->expects($this->atLeastOnce())
      ->method('addIndex')
      ->with('entity_test', 'entity_test__b588603cb9', [['long_index_name', 10]]$this->callback(function D$actual_value) use ($expected) {
        $this->assertEquals($expected['entity_test']['indexes']$actual_value['indexes']);
        $this->assertEquals($expected['entity_test']['fields']$actual_value['fields']);
        // If the parameters don't match, the assertions above will throw an         // exception.         return TRUE;
      }));

    $this->assertNull(
      $this->storageSchema->onEntityTypeUpdate($this->entityType, $original_entity_type)
    );
  }

  /** * Tests various value casts depending on column schema. * * @param mixed $expected * The expected value. * @param mixed $value * The tested value. * @param array $schema * The schema for the table column. * * @dataProvider providerSchemaCastValue * @covers ::castValue */
Home | Imprint | This part of the site doesn't use cookies.