optional_end_date.install in Optional End Date 8
The optional_end_date install file.
File
optional_end_date.installView source
<?php
/**
* @file
* The optional_end_date install file.
*/
/**
* Implements hook_install().
*/
function optional_end_date_install() {
// Update existing daterange fields, to allow NULL in the end_value.
$database_schema = \Drupal::database()
->schema();
$storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
$configs = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->loadByProperties([
'type' => 'daterange',
]);
foreach ($configs as $config) {
$schema_key = $config
->getTargetEntityTypeId() . '.field_schema_data.' . $config
->getName();
$tables = $storage_schema
->get($schema_key);
$field_name = $config
->getName() . '_end_value';
foreach ($tables as $table_name => &$table) {
$table['fields'][$field_name]['not null'] = FALSE;
$database_schema
->changeField($table_name, $field_name, $field_name, $table['fields'][$field_name]);
}
unset($table);
$storage_schema
->set($schema_key, $tables);
}
}
/**
* Update existing field storages to match schema.
*/
function optional_end_date_update_8001() {
$field_configs = Drupal::entityTypeManager()
->getStorage('field_storage_config')
->loadByProperties([
'type' => 'daterange',
]);
foreach ($field_configs as $field_config) {
// Saving the config is only needed if the setting is not already a boolean.
$setting = $field_config
->getSetting('optional_end_date');
if (!is_bool($setting)) {
$field_config
->save();
}
}
}
Functions
Name | Description |
---|---|
optional_end_date_install | Implements hook_install(). |
optional_end_date_update_8001 | Update existing field storages to match schema. |