/** * Executes field storage definition updates if needed. * * @param array $entity_types * A list of entity type definitions to be processed. */ public function updateDefinitions(array $entity_types) { // Handle field storage definition creation, if needed. // @todo Generalize this code in https://www.drupal.org/node/2346013. // @todo Handle initial values in https://www.drupal.org/node/2346019. if ($this->updateManager->needsUpdates()) { foreach ($entity_types as $entity_type_id => $entity_type) { $storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id); $installed_storage_definitions = $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type_id); foreach (array_diff_key($storage_definitions, $installed_storage_definitions) as $storage_definition) { /** @var $storage_definition \Drupal\Core\Field\FieldStorageDefinitionInterface */ if ($storage_definition->getProvider() == 'content_translation') { $this->updateManager->installFieldStorageDefinition($storage_definition->getName(), $entity_type_id, 'content_translation', $storage_definition); } } } } }
/** * Tests applying single updates. */ public function testSingleActionCalls() { $db_schema = $this->database->schema(); // Ensure that a non-existing entity type cannot be installed. $message = 'A non-existing entity type cannot be installed'; try { $this->entityDefinitionUpdateManager->installEntityType(new ContentEntityType(['id' => 'foo'])); $this->fail($message); } catch (PluginNotFoundException $e) { $this->pass($message); } // Ensure that a field cannot be installed on non-existing entity type. $message = 'A field cannot be installed on a non-existing entity type'; try { $storage_definition = BaseFieldDefinition::create('string')->setLabel(t('A new revisionable base field'))->setRevisionable(TRUE); $this->entityDefinitionUpdateManager->installFieldStorageDefinition('bar', 'foo', 'entity_test', $storage_definition); $this->fail($message); } catch (PluginNotFoundException $e) { $this->pass($message); } // Ensure that a non-existing field cannot be installed. $storage_definition = BaseFieldDefinition::create('string')->setLabel(t('A new revisionable base field'))->setRevisionable(TRUE); $this->entityDefinitionUpdateManager->installFieldStorageDefinition('bar', 'entity_test_update', 'entity_test', $storage_definition); $this->assertFalse($db_schema->fieldExists('entity_test_update', 'bar'), "A non-existing field cannot be installed."); // Ensure that installing an existing entity type is a no-op. $entity_type = $this->entityDefinitionUpdateManager->getEntityType('entity_test_update'); $this->entityDefinitionUpdateManager->installEntityType($entity_type); $this->assertTrue($db_schema->tableExists('entity_test_update'), 'Installing an existing entity type is a no-op'); // Create a new base field. $this->addRevisionableBaseField(); $storage_definition = BaseFieldDefinition::create('string')->setLabel(t('A new revisionable base field'))->setRevisionable(TRUE); $this->assertFalse($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' does not exist before applying the update."); $this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition); $this->assertTrue($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' has been created on the 'entity_test_update' table."); // Ensure that installing an existing field is a no-op. $this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition); $this->assertTrue($db_schema->fieldExists('entity_test_update', 'new_base_field'), 'Installing an existing field is a no-op'); // Update an existing field schema. $this->modifyBaseField(); $storage_definition = BaseFieldDefinition::create('text')->setName('new_base_field')->setTargetEntityTypeId('entity_test_update')->setLabel(t('A new revisionable base field'))->setRevisionable(TRUE); $this->entityDefinitionUpdateManager->updateFieldStorageDefinition($storage_definition); $this->assertFalse($db_schema->fieldExists('entity_test_update', 'new_base_field'), "Previous schema for 'new_base_field' no longer exists."); $this->assertTrue($db_schema->fieldExists('entity_test_update', 'new_base_field__value') && $db_schema->fieldExists('entity_test_update', 'new_base_field__format'), "New schema for 'new_base_field' has been created."); // Drop an existing field schema. $this->entityDefinitionUpdateManager->uninstallFieldStorageDefinition($storage_definition); $this->assertFalse($db_schema->fieldExists('entity_test_update', 'new_base_field__value') || $db_schema->fieldExists('entity_test_update', 'new_base_field__format'), "The schema for 'new_base_field' has been dropped."); // Make the entity type revisionable. $this->updateEntityTypeToRevisionable(); $this->assertFalse($db_schema->tableExists('entity_test_update_revision'), "The 'entity_test_update_revision' does not exist before applying the update."); $entity_type = $this->entityDefinitionUpdateManager->getEntityType('entity_test_update'); $keys = $entity_type->getKeys(); $keys['revision'] = 'revision_id'; $entity_type->set('entity_keys', $keys); $this->entityDefinitionUpdateManager->updateEntityType($entity_type); $this->assertTrue($db_schema->tableExists('entity_test_update_revision'), "The 'entity_test_update_revision' table has been created."); }
/** * Check that field schema is correctly handled with long-named fields. */ function testLongNameFieldIndexes() { $this->addLongNameBaseField(); $entity_type_id = 'entity_test_update'; $entity_type = $this->entityManager->getDefinition($entity_type_id); $definitions = EntityTestUpdate::baseFieldDefinitions($entity_type); $name = 'new_long_named_entity_reference_base_field'; $this->entityDefinitionUpdateManager->installFieldStorageDefinition($name, $entity_type_id, 'entity_test', $definitions[$name]); $this->assertFalse($this->entityDefinitionUpdateManager->needsUpdates(), 'Entity and field schema data are correctly detected.'); }