viewsreference.install in Views Reference Field 8
Same filename and directory in other branches
Viewsreference install functions.
File
viewsreference.installView source
<?php
/**
* @file
* Viewsreference install functions.
*/
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
/**
* Update node fields already created with argument field.
*/
function viewsreference_update_8100() {
viewsreference_update_viewsreference_fields('argument');
}
/**
* Update node fields already created with title field.
*/
function viewsreference_update_8101() {
viewsreference_update_viewsreference_fields('title');
}
/**
* Update node fields already created with data field.
*/
function viewsreference_update_8102() {
viewsreference_update_viewsreference_fields('data');
}
/**
* 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.
*
* @param string $new_field_name
* The name of the new field to be added.
*/
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);
}
}
}
Functions
Name | Description |
---|---|
viewsreference_update_8100 | Update node fields already created with argument field. |
viewsreference_update_8101 | Update node fields already created with title field. |
viewsreference_update_8102 | Update node fields already created with data field. |
viewsreference_update_viewsreference_fields | Update all instances using viewsreference field type with new field. |