/** * Generates an index name for a field data table. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The field storage definition. * @param string $index * The name of the index. * * @return string * A string containing a generated index name for a field data table that is * unique among all other fields. */ protected function getFieldIndexName(FieldStorageDefinitionInterface $storage_definition, $index) { return $storage_definition->getName() . '_' . $index; }
/** * Deletes the field storage definition from the application state. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The field storage definition. */ protected function deleteLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) { $entity_type_id = $storage_definition->getTargetEntityTypeId(); $definitions = $this->getLastInstalledFieldStorageDefinitions($entity_type_id); unset($definitions[$storage_definition->getName()]); $this->setLastInstalledFieldStorageDefinitions($entity_type_id, $definitions); }
/** * 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)); }
/** * Generates a safe and unambiguous field table name. * * The method accounts for a maximum table name length of 64 characters, and * takes care of disambiguation. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The field storage definition. * @param bool $revision * TRUE for revision table, FALSE otherwise. * * @return string * The final table name. */ protected function generateFieldTableName(FieldStorageDefinitionInterface $storage_definition, $revision) { $separator = $revision ? '_revision__' : '__'; $table_name = $storage_definition->getTargetEntityTypeId() . $separator . $storage_definition->getName(); // Limit the string to 48 characters, keeping a 16 characters margin for db // prefixes. if (strlen($table_name) > 48) { // Use a shorter separator, a truncated entity_type, and a hash of the // field UUID. $separator = $revision ? '_r__' : '__'; // Truncate to the same length for the current and revision tables. $entity_type = substr($storage_definition->getTargetEntityTypeId(), 0, 34); $field_hash = substr(hash('sha256', $storage_definition->getUniqueStorageIdentifier()), 0, 10); $table_name = $entity_type . $separator . $field_hash; } return $table_name; }
/** * Generates a column name for a field data table. * * @private Calling this function circumvents the entity system and is * strongly discouraged. This function is not considered part of the public * API and modules relying on it might break even in minor releases. Only * call this function to write a query that \Drupal::entityQuery() does not * support. Always call entity_load() before using the data found in the * table. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The field storage definition. * @param string $column * The name of the column. * * @return string * A string containing a generated column name for a field data table that is * unique among all other fields. */ public static function _fieldColumnName(FieldStorageDefinitionInterface $storage_definition, $column) { return in_array($column, FieldStorageConfig::getReservedColumns()) ? $column : $storage_definition->getName() . '_' . $column; }
/** * Creates a new field definition based upon a field storage definition. * * In cases where one needs a field storage definitions to act like full * field definitions, this creates a new field definition based upon the * (limited) information available. That way it is possible to use the field * definition in places where a full field definition is required; e.g., with * widgets or formatters. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition * The field storage definition to base the new field definition upon. * * @return $this */ public static function createFromFieldStorageDefinition(FieldStorageDefinitionInterface $definition) { return static::create($definition->getType())->setCardinality($definition->getCardinality())->setConstraints($definition->getConstraints())->setCustomStorage($definition->hasCustomStorage())->setDescription($definition->getDescription())->setLabel($definition->getLabel())->setName($definition->getName())->setProvider($definition->getProvider())->setQueryable($definition->isQueryable())->setRequired($definition->isRequired())->setRevisionable($definition->isRevisionable())->setSettings($definition->getSettings())->setTargetEntityTypeId($definition->getTargetEntityTypeId())->setTranslatable($definition->isTranslatable()); }
/** * {@inheritdoc} */ public function updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) { $original = $this->getFieldStorageDefinition($storage_definition->getName(), $storage_definition->getTargetEntityTypeId()); $this->entityManager->clearCachedDefinitions(); $this->entityManager->onFieldStorageDefinitionUpdate($storage_definition, $original); }
/** * 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) { // Configurable fields are marked for deletion. if ($storage_definition instanceof FieldStorageConfigInterface) { return $storage_definition->isDeleted(); } // For non configurable fields check whether they are still in the last // installed schema repository. return !array_key_exists($storage_definition->getName(), $this->entityManager->getLastInstalledFieldStorageDefinitions($this->entityTypeId)); }
/** * {@inheritdoc} */ public function getFieldColumnName(FieldStorageDefinitionInterface $storage_definition, $column) { return in_array($column, $this->getReservedColumns()) ? $column : $storage_definition->getName() . '_' . $column; }
/** * {@inheritdoc} */ public function getAllFieldConfigsForField(FieldStorageDefinitionInterface $definition, $entity_type_id) { $map = $this->entityFieldManager->getFieldMap()[$entity_type_id]; $definitions = []; $field_name = $definition->getName(); if (isset($map[$field_name])) { $bundles = $map[$field_name]['bundles']; foreach ($bundles as $bundle) { $bundle_definitions = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle); $definitions[$bundle] = $bundle_definitions[$field_name]; } } return $definitions; }
/** * Create a form display for a newly clone field. * * This function attempts to use same setting settings as the source field. * * @param \Drupal\scheduled_updates\ScheduledUpdateTypeInterface $scheduled_update_type * @param $field_name * @param $field_config_id * @param $entity_type * @param $definition * Source field definition * @param $new_field_name */ protected function createFormDisplay(ScheduledUpdateTypeInterface $scheduled_update_type, $field_config_id, FieldStorageDefinitionInterface $definition, $new_field_name) { $destination_bundle = $scheduled_update_type->id(); $field_name = $definition->getName(); $entity_type = $scheduled_update_type->getUpdateEntityType(); /** @var EntityFormDisplay $destination_form_display */ $destination_form_display = EntityFormDisplay::load("scheduled_update.$destination_bundle.default"); if (empty($destination_form_display)) { $destination_form_display = EntityFormDisplay::create([ 'targetEntityType' => 'scheduled_update', 'bundle' => $destination_bundle, 'mode' => 'default', 'status' => TRUE, ]); } $display_options = []; if ($field_config_id) { $parts = explode('.', $field_config_id); $source_bundle = $parts[1]; /** @var EntityFormDisplay $source_form_display */ $source_form_display = EntityFormDisplay::load("$entity_type.$source_bundle.default"); $display_options = $source_form_display->getComponent($field_name); } else { if ($definition instanceof BaseFieldDefinition) { $display_options = $definition->getDisplayOptions('form'); if (empty($display_options)) { if ($definition->getType()) { // Provide default display for base boolean fields that don't have their own form display $display_options = [ 'type' => 'boolean_checkbox', 'settings' => ['display_label' => TRUE], ]; } } } } if (empty($display_options)) { $display_options = []; } if ($destination_form_display) { $destination_form_display->setComponent($new_field_name, $display_options); $destination_form_display->save(); } else { // Alert user if display options could not be created. // @todo Create default display options even none on source. drupal_set_message( $this->t( 'Form display options could not be created for @field they will have to be created manually.', ['@field' => $field_name] ), 'warning'); } }