entityDefinitionUpdateManager example

foreach ($names as $name) {
        if (in_array($name$exclude, TRUE)) {
          // Skip checking schema if the config is listed in the           // $configSchemaCheckerExclusions property.           continue;
        }
        $config = $this->config($name);
        $this->assertConfigSchema($typed_config$name$config->get());
      }

      // Ensure that the update hooks updated all entity schema.       $needs_updates = \Drupal::entityDefinitionUpdateManager()->needsUpdates();
      if ($needs_updates) {
        foreach (\Drupal::entityDefinitionUpdateManager()->getChangeSummary() as $entity_type_id => $summary) {
          $entity_type_label = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getLabel();
          foreach ($summary as $message) {
            $this->fail("$entity_type_label: $message");
          }
        }
        // The above calls to `fail()` should prevent this from ever being         // called, but it is here in case something goes really wrong.         $this->assertFalse($needs_updates, 'After all updates ran, entity schema is up to date.');
      }
    }
/** * {@inheritdoc} */
  protected function createEntity() {
    // Set flag so that internal field 'internal_string_field' is created.     // @see entity_test_entity_base_field_info()     $this->container->get('state')->set('entity_test.internal_field', TRUE);
    $field_storage_definition = BaseFieldDefinition::create('string')
      ->setLabel('Internal field')
      ->setInternal(TRUE);
    \Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('internal_string_field', 'entity_test', 'entity_test', $field_storage_definition);

    $entity_test = EntityTest::create([
      'name' => 'Llama',
      'type' => 'entity_test',
      // Set a value for the internal field to confirm that it will not be       // returned in normalization.       // @see entity_test_entity_base_field_info().       'internal_string_field' => [
        'value' => 'This value shall not be internal!',
      ],
    ]);
    
$this->submitForm([], 'Delete all workspaces');
    $this->drupalGet('admin/modules/uninstall');
    $this->submitForm(['uninstall[workspaces]' => TRUE], 'Uninstall');
    $this->submitForm([], 'Uninstall');
    $session->pageTextContains('The selected modules have been uninstalled.');
    $session->pageTextNotContains('Workspaces');

    $this->assertFalse(\Drupal::database()->schema()->fieldExists('node_revision', 'workspace'));

    // Verify that the revision metadata key has been removed.     $this->rebuildContainer();
    $entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('node');
    $revision_metadata_keys = $entity_type->get('revision_metadata_keys');
    $this->assertArrayNotHasKey('workspace', $revision_metadata_keys);
  }

}

  protected $entityDefinitionUpdateManager;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $this->entityDefinitionUpdateManager = \Drupal::entityDefinitionUpdateManager();
  }

  /** * {@inheritdoc} */
  protected function setDatabaseDumpFiles() {
    $this->databaseDumpFiles = [
      __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-9.4.0.filled.standard.php.gz',
    ];
  }

  
// Add the bundle field and run the update.     $this->addBundleField();
    $this->applyEntityUpdates();

    // Update the entity type schema to revisionable but don't run the updates     // yet.     $this->updateEntityTypeToRevisionable();

    // Perform a no-op update on the bundle field, which should work because     // both the storage and the storage schema are using the last installed     // entity type definition.     $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    $entity_definition_update_manager->updateFieldStorageDefinition($entity_definition_update_manager->getFieldStorageDefinition('new_bundle_field', 'entity_test_update'));
  }

  /** * Tests creating and deleting a multi-field index when there are no existing entities. */
  public function testEntityIndexCreateDeleteWithoutData() {
    // Add an entity index and ensure the update manager reports that as an     // update to the entity type.     $this->addEntityIndex();
    $this->assertTrue($this->entityDefinitionUpdateManager->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
    

  public function testNonLangcodeEntityTypeModeration() {
    // Unset the langcode entity key for 'entity_test_rev'.     $entity_type = clone $this->entityTypeManager->getDefinition($this->revEntityTypeId);
    $keys = $entity_type->getKeys();
    unset($keys['langcode']);
    $entity_type->set('entity_keys', $keys);
    \Drupal::state()->set($this->revEntityTypeId . '.entity_type', $entity_type);

    // Update the entity type in order to remove the 'langcode' field.     \Drupal::entityDefinitionUpdateManager()->updateFieldableEntityType($entity_type, \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type->id()));

    $workflow = $this->createEditorialWorkflow();
    $this->addEntityTypeAndBundleToWorkflow($workflow$this->revEntityTypeId, $this->revEntityTypeId);

    // Check that the tested entity type is not translatable and does not have a     // 'langcode' entity key.     $entity_type = $this->entityTypeManager->getDefinition($this->revEntityTypeId);
    $this->assertFalse($entity_type->isTranslatable(), 'The test entity type is not translatable.');
    $this->assertFalse($entity_type->getKey('langcode'), "The test entity type does not have a 'langcode' entity key.");

    // Create a test entity.

  public function testColumnUpdate() {
    // Change the field type in the stored schema.     $schema = \Drupal::keyValue('entity.storage_schema.sql')->get('entity_test_rev.field_schema_data.test');
    $schema['entity_test_rev__test']['fields']['test_value']['type'] = 'varchar_ascii';
    \Drupal::keyValue('entity.storage_schema.sql')->set('entity_test_rev.field_schema_data.test', $schema);

    // Now attempt to run automatic updates. An exception should be thrown     // since there is data in the table.     $this->expectException(FieldStorageDefinitionUpdateForbiddenException::class);
    $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    $field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition('test', 'entity_test_rev');
    $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
  }

}
$versions = $this->updateRegistry->getAvailableUpdates($module);
        if ($versions) {
          $version = max(max($versions)$version);
        }

        // Notify interested components that this module's entity types and         // field storage definitions are new. For example, a SQL-based storage         // handler can use this as an opportunity to create the necessary         // database tables.         // @todo Clean this up in https://www.drupal.org/node/2350111.         $entity_type_manager = \Drupal::entityTypeManager();
        $update_manager = \Drupal::entityDefinitionUpdateManager();
        /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
        $entity_field_manager = \Drupal::service('entity_field.manager');
        foreach ($entity_type_manager->getDefinitions() as $entity_type) {
          $is_fieldable_entity_type = $entity_type->entityClassImplements(FieldableEntityInterface::class);

          if ($entity_type->getProvider() == $module) {
            if ($is_fieldable_entity_type) {
              $update_manager->installFieldableEntityType($entity_type$entity_field_manager->getFieldStorageDefinitions($entity_type->id()));
            }
            else {
              $update_manager->installEntityType($entity_type);
            }
/** * Applies all the detected valid changes. * * Use this with care, as it will apply updates for any module, which will * lead to unpredictable results. * * @param string $entity_type_id * (optional) Applies changes only for the specified entity type ID. * Defaults to NULL. */
  protected function applyEntityUpdates($entity_type_id = NULL) {
    $complete_change_list = \Drupal::entityDefinitionUpdateManager()->getChangeList();
    if ($complete_change_list) {
      // In case there are changes, explicitly invalidate caches.       \Drupal::entityTypeManager()->clearCachedDefinitions();
      \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    }

    if ($entity_type_id) {
      $complete_change_list = array_intersect_key($complete_change_list[$entity_type_id => TRUE]);
    }

    foreach ($complete_change_list as $entity_type_id => $change_list) {
      
Home | Imprint | This part of the site doesn't use cookies.