uninstallFieldStorageDefinition example

// Test that composite indexes are handled correctly when dropping and     // re-creating one of their columns.     $this->addEntityIndex();
    $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);

    $storage_definition = $this->entityDefinitionUpdateManager->getFieldStorageDefinition('name', 'entity_test_update');
    $this->entityDefinitionUpdateManager->updateFieldStorageDefinition($storage_definition);
    $this->assertTrue($this->database->schema()->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index created.');
    $this->entityDefinitionUpdateManager->uninstallFieldStorageDefinition($storage_definition);
    $this->assertFalse($this->database->schema()->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index deleted.');
    $this->entityDefinitionUpdateManager->installFieldStorageDefinition('name', 'entity_test_update', 'entity_test', $storage_definition);
    $this->assertTrue($this->database->schema()->indexExists('entity_test_update', 'entity_test_update__new_index'), 'Index created again.');
  }

  /** * Tests creating a multi-field index when there are existing entities. */
  public function testEntityIndexCreateWithData() {
    // Save an entity.     $name = $this->randomString();
    
$entity_field_manager = \Drupal::service('entity_field.manager');
      foreach ($entity_type_manager->getDefinitions() as $entity_type) {
        if ($entity_type->getProvider() == $module) {
          $update_manager->uninstallEntityType($entity_type);
        }
        elseif ($entity_type->entityClassImplements(FieldableEntityInterface::CLASS)) {
          // The module being uninstalled might have added new fields to           // existing entity types. This will add them to the deleted fields           // repository so their data will be purged on cron.           foreach ($entity_field_manager->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) {
            if ($storage_definition->getProvider() == $module) {
              $update_manager->uninstallFieldStorageDefinition($storage_definition);
            }
          }
        }
      }

      // Remove the schema.       $this->uninstallSchema($module);

      // Remove the module's entry from the config. Don't check schema when       // uninstalling a module since we are only clearing a key.       \Drupal::configFactory()->getEditable('core.extension')->clear("module.$module")->save(TRUE);

      
$this->state->set('entity_test_update.additional_base_field_definitions', $definitions);

    $this->entityDefinitionUpdateManager->installFieldStorageDefinition('data_map', 'entity_test_update', 'entity_test', $definitions['data_map']);

    $entity = EntityTestUpdate::create([
      'data_map' => [
        'key' => 'value',
      ],
    ]);
    $entity->save();

    $this->entityDefinitionUpdateManager->uninstallFieldStorageDefinition($definitions['data_map']);
  }

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

    // If the entity type is no longer supported by Workspaces, remove the     // revision metadata field.     if ($this->workspaceManager->isEntityTypeSupported($original) && !$this->workspaceManager->isEntityTypeSupported($entity_type)) {
      $revision_metadata_keys = $original->get('revision_metadata_keys');
      $field_storage_definition = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type->id())[$revision_metadata_keys['workspace']];
      $this->entityDefinitionUpdateManager->uninstallFieldStorageDefinition($field_storage_definition);

      // 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} */
  


    // First, test explicitly deleting and re-installing a field. Make sure that     // all primary keys are there to start with.     $this->assertSame($expected$this->findPrimaryKeys($entity_type));

    // Then uninstall the field and make sure all primary keys that the field     // was part of have been updated. Since this is not a valid state of the     // entity type (for example a revisionable entity type without a revision ID     // field or a translatable entity type without a language code field) the     // actual primary keys at this point are irrelevant.     $update_manager->uninstallFieldStorageDefinition($field);
    $this->assertNotEquals($expected$this->findPrimaryKeys($entity_type));

    // Finally, reinstall the field and make sure the primary keys have been     // recreated.     $update_manager->installFieldStorageDefinition($field->getName()$entity_type_id$field->getProvider()$field);
    $this->assertSame($expected$this->findPrimaryKeys($entity_type));

    // Now test updating a field without data. This will end up deleting     // and re-creating the field, similar to the code above.     $update_manager->updateFieldStorageDefinition($field);
    $this->assertSame($expected$this->findPrimaryKeys($entity_type));

    
Home | Imprint | This part of the site doesn't use cookies.