/** * Deletes the schema for a field stored in a shared table. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The storage definition of the field being deleted. */ protected function deleteSharedTableSchema(FieldStorageDefinitionInterface $storage_definition) { $deleted_field_name = $storage_definition->getName(); $table_mapping = $this->storage->getTableMapping($this->entityManager->getLastInstalledFieldStorageDefinitions($this->entityType->id())); $column_names = $table_mapping->getColumnNames($deleted_field_name); $schema_handler = $this->database->schema(); $shared_table_names = array_diff($table_mapping->getTableNames(), $table_mapping->getDedicatedTableNames()); // Iterate over the mapped table to find the ones that host the deleted // field schema. foreach ($shared_table_names as $table_name) { foreach ($table_mapping->getFieldNames($table_name) as $field_name) { if ($field_name == $deleted_field_name) { $schema = $this->getSharedTableFieldSchema($storage_definition, $table_name, $column_names); // Drop indexes and unique keys first. if (!empty($schema['indexes'])) { foreach ($schema['indexes'] as $name => $specifier) { $schema_handler->dropIndex($table_name, $name); } } if (!empty($schema['unique keys'])) { foreach ($schema['unique keys'] as $name => $specifier) { $schema_handler->dropUniqueKey($table_name, $name); } } // Drop columns. foreach ($column_names as $column_name) { $schema_handler->dropField($table_name, $column_name); } // After deleting the field schema skip to the next table. break; } } } $this->deleteFieldSchemaData($storage_definition); }
/** * 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); } } } } }
/** * Initializes an instance of the content translation controller. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The info array of the given entity type. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. * @param \Drupal\content_translation\ContentTranslationManagerInterface $manager * The content translation manager service. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. */ public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager, ContentTranslationManagerInterface $manager, EntityManagerInterface $entity_manager, AccountInterface $current_user) { $this->entityTypeId = $entity_type->id(); $this->entityType = $entity_type; $this->languageManager = $language_manager; $this->manager = $manager; $this->currentUser = $current_user; $this->fieldStorageDefinitions = $entity_manager->getLastInstalledFieldStorageDefinitions($this->entityTypeId); }
/** * Gets a list of changes to entity type and field storage definitions. * * @return array * An associative array keyed by entity type id of change descriptors. Every * entry is an associative array with the following optional keys: * - entity_type: a scalar having only the DEFINITION_UPDATED value. * - field_storage_definitions: an associative array keyed by field name of * scalars having one value among: * - DEFINITION_CREATED * - DEFINITION_UPDATED * - DEFINITION_DELETED */ protected function getChangeList() { $this->entityManager->useCaches(FALSE); $change_list = array(); foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) { $original = $this->entityManager->getLastInstalledDefinition($entity_type_id); // @todo Support non-storage-schema-changing definition updates too: // https://www.drupal.org/node/2336895. if (!$original) { $change_list[$entity_type_id]['entity_type'] = static::DEFINITION_CREATED; } else { if ($this->requiresEntityStorageSchemaChanges($entity_type, $original)) { $change_list[$entity_type_id]['entity_type'] = static::DEFINITION_UPDATED; } if ($this->entityManager->getStorage($entity_type_id) instanceof DynamicallyFieldableEntityStorageInterface) { $field_changes = array(); $storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id); $original_storage_definitions = $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type_id); // Detect created field storage definitions. foreach (array_diff_key($storage_definitions, $original_storage_definitions) as $field_name => $storage_definition) { $field_changes[$field_name] = static::DEFINITION_CREATED; } // Detect deleted field storage definitions. foreach (array_diff_key($original_storage_definitions, $storage_definitions) as $field_name => $original_storage_definition) { $field_changes[$field_name] = static::DEFINITION_DELETED; } // Detect updated field storage definitions. foreach (array_intersect_key($storage_definitions, $original_storage_definitions) as $field_name => $storage_definition) { // @todo Support non-storage-schema-changing definition updates too: // https://www.drupal.org/node/2336895. So long as we're checking // based on schema change requirements rather than definition // equality, skip the check if the entity type itself needs to be // updated, since that can affect the schema of all fields, so we // want to process that update first without reporting false // positives here. if (!isset($change_list[$entity_type_id]['entity_type']) && $this->requiresFieldStorageSchemaChanges($storage_definition, $original_storage_definitions[$field_name])) { $field_changes[$field_name] = static::DEFINITION_UPDATED; } } if ($field_changes) { $change_list[$entity_type_id]['field_storage_definitions'] = $field_changes; } } } } // @todo Support deleting entity definitions when we support base field // purging. See https://www.drupal.org/node/2282119. $this->entityManager->useCaches(TRUE); return array_filter($change_list); }
/** * Determines whether the passed field has been already deleted. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The field storage definition. * * @return bool * Whether the field has been already deleted. */ protected function storageDefinitionIsDeleted(FieldStorageDefinitionInterface $storage_definition) { return !array_key_exists($storage_definition->getName(), $this->entityManager->getLastInstalledFieldStorageDefinitions($this->entityTypeId)); }
/** * {@inheritdoc} */ public function getLastInstalledFieldStorageDefinitions($entity_type_id) { return $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type_id); }