viewfield.install in Viewfield 8.3
Same filename and directory in other branches
Install, update and uninstall functions for the viewfield module.
File
viewfield.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the viewfield module.
*/
use Drupal\Core\Database\Database;
/**
* Changes display_id data type to varchar_ascii.
*/
function viewfield_update_8301() {
$property = 'display_id';
$db_type = 'varchar_ascii';
$db_schema = Database::getConnection()
->schema();
if (!($field_storage_configs = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->loadByProperties([
'type' => 'viewfield',
]))) {
return;
}
foreach ($field_storage_configs as $field_storage) {
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage */
$schema = $field_storage
->getSchema();
$field_spec = $schema['columns'][$property];
$field_spec['type'] = $db_type;
$entity_type_id = $field_storage
->getTargetEntityTypeId();
/** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $entity_storage */
$entity_storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = $entity_storage
->getTableMapping();
$tables = [
$table_mapping
->getDedicatedDataTableName($field_storage),
];
if ($field_storage
->isRevisionable()) {
$tables[] = $table_mapping
->getDedicatedRevisionTableName($field_storage);
}
$column = $table_mapping
->getFieldColumnName($field_storage, $property);
foreach ($tables as $table) {
if ($db_schema
->tableExists($table)) {
$db_schema
->changeField($table, $column, $column, $field_spec);
}
}
}
}
/**
* Add field for items_to_display.
*/
function viewfield_update_8302() {
$property = 'items_to_display';
$db_type = 'varchar';
$db_schema = Database::getConnection()
->schema();
if (!($field_storage_configs = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->loadByProperties([
'type' => 'viewfield',
]))) {
return;
}
foreach ($field_storage_configs as $field_storage) {
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage */
$schema = $field_storage
->getSchema();
$field_spec = $schema['columns'][$property];
$field_spec['type'] = $db_type;
$entity_type_id = $field_storage
->getTargetEntityTypeId();
/** @var \Drupal\Core\Entity\Sql\SqlEntityStorageInterface $entity_storage */
$entity_storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = $entity_storage
->getTableMapping();
$tables = [
$table_mapping
->getDedicatedDataTableName($field_storage),
];
if ($field_storage
->isRevisionable()) {
$tables[] = $table_mapping
->getDedicatedRevisionTableName($field_storage);
}
$column = $table_mapping
->getFieldColumnName($field_storage, $property);
foreach ($tables as $table) {
if ($db_schema
->tableExists($table)) {
$db_schema
->addField($table, $column, $field_spec);
}
}
}
}
/**
* Add field for items_to_display.
*/
function viewfield_update_8303() {
$property = 'items_to_display';
if (!($field_configs = \Drupal::entityTypeManager()
->getStorage('field_config')
->loadByProperties([
'field_type' => 'viewfield',
]))) {
return;
}
foreach ($field_configs as $field) {
/** @var \Drupal\Core\Field\FieldConfigInterface $field */
$default_values = $field
->getDefaultValueLiteral();
foreach ($default_values as $key => $value) {
$default_values[$key][$property] = '';
}
$field
->setDefaultValue($default_values);
$field
->save();
}
}
/**
* Update field storage definition.
*/
function viewfield_update_8304() {
if (!($field_configs = \Drupal::entityTypeManager()
->getStorage('field_config')
->loadByProperties([
'field_type' => 'viewfield',
]))) {
return;
}
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
foreach ($field_configs as $field) {
/** @var \Drupal\Core\Field\FieldConfigInterface $field */
$entity_type = $field
->getTargetEntityTypeId();
$field_name = $field
->getName();
$field_storage_definition = $entity_definition_update_manager
->getFieldStorageDefinition($field_name, $entity_type);
$entity_definition_update_manager
->updateFieldStorageDefinition($field_storage_definition);
}
}
Functions
Name![]() |
Description |
---|---|
viewfield_update_8301 | Changes display_id data type to varchar_ascii. |
viewfield_update_8302 | Add field for items_to_display. |
viewfield_update_8303 | Add field for items_to_display. |
viewfield_update_8304 | Update field storage definition. |