function viewsreference_update_viewsreference_fields in Views Reference Field 8
Same name and namespace in other branches
- 8.2 viewsreference.install \viewsreference_update_viewsreference_fields()
Update all instances using viewsreference field type with new field.
This is called from hook_update_N() when new fields are added to the schema.
Parameters
string $new_field_name: The name of the new field to be added.
3 calls to viewsreference_update_viewsreference_fields()
- viewsreference_update_8100 in ./
viewsreference.install - Update node fields already created with argument field.
- viewsreference_update_8101 in ./
viewsreference.install - Update node fields already created with title field.
- viewsreference_update_8102 in ./
viewsreference.install - Update node fields already created with data field.
File
- ./
viewsreference.install, line 39 - Viewsreference install functions.
Code
function viewsreference_update_viewsreference_fields($new_field_name) {
// Caches have to be cleared first to ensure new fields
// are detected in the code.
drupal_flush_all_caches();
// Retrieve list of all viewsreference fields mapped by entity type.
$field_map = \Drupal::service('entity_field.manager')
->getFieldMapByFieldType('viewsreference');
$manager = \Drupal::entityDefinitionUpdateManager();
$schema = \Drupal::database()
->schema();
foreach ($field_map as $entity_type_id => $fields) {
foreach (array_keys($fields) as $field_name) {
$field_storage_definition = $manager
->getFieldStorageDefinition($field_name, $entity_type_id);
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
if ($storage instanceof SqlContentEntityStorage) {
$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();
$field_exists = $schema
->fieldExists($table_name, $columns[$new_field_name]);
$table_exists = $schema
->tableExists($table_name);
if (!$field_exists && $table_exists) {
$schema
->addField($table_name, $columns[$new_field_name], $field_schema['columns'][$new_field_name]);
}
}
}
$manager
->updateFieldStorageDefinition($field_storage_definition);
}
}
}