You are here

function optional_end_date_install in Optional End Date 8

Implements hook_install().

File

./optional_end_date.install, line 11
The optional_end_date install file.

Code

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);
  }
}