updateFieldStorageDefinition example

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

    // Now test updating a field with data.     /** @var \Drupal\Core\Entity\FieldableEntityStorageInterface $storage */
    $storage = $this->entityTypeManager->getStorage($entity_type_id);
    $storage->create()->save();
    $this->assertTrue($storage->countFieldData($field, TRUE));
    $update_manager->updateFieldStorageDefinition($field);
    $this->assertSame($expected$this->findPrimaryKeys($entity_type));
    $this->assertTrue($storage->countFieldData($field, TRUE));
  }

  
    $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.');
    $expected = [
      
/** * Tests the upgrade path for moderation state reindexing. */
  public function testRunUpdates() {
    $constraint = 'UniqueField';
    $constraints = $this->getFieldInfoConstraints();
    if (!isset($constraints[$constraint])) {
      $constraints[$constraint] = [];
      $field_storage_definition = $this->entityDefinitionUpdateManager->getFieldStorageDefinition('info', 'block_content');
      $field_storage_definition->setConstraints($constraints);
      $this->entityDefinitionUpdateManager->updateFieldStorageDefinition($field_storage_definition);
    }

    $this->assertCount(2, $this->getFieldInfoConstraints());

    $this->runUpdates();

    $this->assertCount(1, $this->getFieldInfoConstraints());
  }

  /** * Get constraints for info field. * * @return array[] * List of constraints. */

    $actual = $this->installedStorageSchema->get('entity_test.field_schema_data.shape');
    $this->assertSame($expected$actual);

    // Make the field an entity key, so that it will get marked as NOT NULL.     $entity_type = $this->entityDefinitionUpdateManager->getEntityType('entity_test');
    $original_keys = $entity_type->getKeys();
    $entity_type->set('entity_keys', $original_keys + ['shape' => 'shape']);
    $this->entityDefinitionUpdateManager->updateEntityType($entity_type);

    // Update the field and make sure the schema got updated.     $this->entityDefinitionUpdateManager->updateFieldStorageDefinition($field);
    $expected['entity_test']['fields']['shape__shape']['not null'] = TRUE;
    $expected['entity_test']['fields']['shape__color']['not null'] = TRUE;
    $actual = $this->installedStorageSchema->get('entity_test.field_schema_data.shape');
    $this->assertSame($expected$actual);

    // Remove the entity key again and check that the schema got reverted.     $entity_type->set('entity_keys', $original_keys);
    $this->entityDefinitionUpdateManager->updateEntityType($entity_type);

    $this->entityDefinitionUpdateManager->updateFieldStorageDefinition($field);
    $expected['entity_test']['fields']['shape__shape']['not null'] = FALSE;
    
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);
  }

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