function _smart_date_field_columns_add in Smart Date 3.1.x
Same name and namespace in other branches
- 8.2 smart_date.install \_smart_date_field_columns_add()
- 3.x smart_date.install \_smart_date_field_columns_add()
- 3.0.x smart_date.install \_smart_date_field_columns_add()
- 3.2.x smart_date.install \_smart_date_field_columns_add()
- 3.3.x smart_date.install \_smart_date_field_columns_add()
- 3.4.x smart_date.install \_smart_date_field_columns_add()
Helper function to find all instances of our field type and add a column.
Parameters
string $field_type: The type of field to look for.
array $specs: An array of columns to add.
2 calls to _smart_date_field_columns_add()
- smart_date_update_8201 in ./
smart_date.install - Implements hook_install().
- smart_date_update_8202 in ./
smart_date.install - Add rule index column for recurring dates.
File
- ./
smart_date.install, line 118
Code
function _smart_date_field_columns_add($field_type, array $specs, $update_definitions = TRUE) {
// If there are no instances of the field, nothing to do.
if (!($field_storage_configs = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->loadByProperties([
'type' => $field_type,
]))) {
return;
}
$manager = \Drupal::entityDefinitionUpdateManager();
// Iterate through each instance of FieldStorageConfig returned.
foreach ($field_storage_configs as $field_storage) {
// Extract the proeprties needed for other calls.
$field_name = $field_storage
->getName();
$entity_type_id = $field_storage
->getTargetEntityTypeId();
// Now collect the objects we need.
$field_storage_definition = $manager
->getFieldStorageDefinition($field_name, $entity_type_id);
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
if (!$storage instanceof NodeStorage && !$storage instanceof SqlContentEntityStorage) {
return;
}
$table_mapping = $storage
->getTableMapping([
$field_name => $field_storage_definition,
]);
$table_names = $table_mapping
->getDedicatedTableNames();
$columns = $table_mapping
->getColumnNames($field_name);
foreach ($table_names as $table_name) {
$field_schema = $field_storage_definition
->getSchema();
$schema = \Drupal::database()
->schema();
// If the table doesn't exist, no need to go any further.
$table_exists = $schema
->tableExists($table_name);
if (!$table_exists) {
continue;
}
// Step through the columns to add.
foreach ($specs as $name => $spec) {
// Only add the column if it isn't there already.
$new_column = $field_name . '_' . $name;
$field_exists = $schema
->fieldExists($table_name, $new_column);
if (!$field_exists) {
$schema
->addField($table_name, $new_column, $spec, [
'fields' => [
$new_column => $spec,
],
'indexes' => [
$new_column => [
$new_column,
],
],
]);
}
}
}
if ($update_definitions) {
$manager
->updateFieldStorageDefinition($field_storage_definition);
}
}
}