View source
<?php
use Drupal\event\Entity\Event;
function event_update_8101() {
$config_factory = \Drupal::configFactory();
$configs = $config_factory
->listAll('event.event_type');
foreach ($configs as $config) {
$event_config = $config_factory
->getEditable($config);
$event_config
->setName(preg_replace('/event.event_type/', 'event.type', $config));
$event_config
->save();
}
}
function event_update_8102() {
$entity_type_manager = \Drupal::entityTypeManager();
$bundle_of = 'event';
$storage = $entity_type_manager
->getStorage($bundle_of);
$bundle_definition = $entity_type_manager
->getDefinition($bundle_of);
$id_key = $bundle_definition
->getKey('id');
$base_table_name = $storage
->getDataTable() ?: $storage
->getBaseTable();
$revision_table_name = $storage
->getRevisionDataTable() ?: $storage
->getRevisionTable();
$database = \Drupal::database();
$definition_manager = \Drupal::entityDefinitionUpdateManager();
$event_start_field_storage_definition = $definition_manager
->getFieldStorageDefinition('event_start', $bundle_of);
$event_end_field_storage_definition = $definition_manager
->getFieldStorageDefinition('event_end', $bundle_of);
if (isset($event_end_field_storage_definition) && isset($event_start_field_storage_definition)) {
$event_date_values = $database
->select($base_table_name)
->fields($base_table_name, [
$id_key,
'event_start__value',
'event_start__timezone',
'event_end__value',
])
->execute()
->fetchAllAssoc($id_key);
$event_date_revision_values = $database
->select($revision_table_name)
->fields($revision_table_name, [
$id_key,
'event_start__value',
'event_start__timezone',
'event_end__value',
])
->execute()
->fetchAllAssoc($id_key);
$database
->update($base_table_name)
->fields([
'event_start__value' => NULL,
'event_start__timezone' => NULL,
'event_end__value' => NULL,
'event_end__timezone' => NULL,
])
->execute();
$definition_manager
->uninstallFieldStorageDefinition($event_start_field_storage_definition);
$definition_manager
->uninstallFieldStorageDefinition($event_end_field_storage_definition);
}
$current_fields = Event::baseFieldDefinitions($bundle_definition);
if (isset($current_fields['event_date']) && !$definition_manager
->getFieldStorageDefinition('event_date', 'event')) {
$definition_manager
->installFieldStorageDefinition('event_date', $bundle_of, $bundle_of, $current_fields['event_date']);
}
if (isset($current_fields['machine_name']) && !$definition_manager
->getFieldStorageDefinition('machine_name', 'event')) {
$definition_manager
->installFieldStorageDefinition('machine_name', $bundle_of, $bundle_of, $current_fields['machine_name']);
}
$description_field = \Drupal::configFactory()
->get('field.storage.event.description');
if ($description_field
->isNew()) {
\Drupal::service('config.installer')
->installDefaultConfig('module', 'event');
$bundles = \Drupal::service('entity_type.bundle.info')
->getBundleInfo('event');
foreach (array_keys($bundles) as $bundle) {
$event_type = \Drupal::entityTypeManager()
->getStorage('event_type')
->load($bundle);
event_add_description_field($event_type);
}
}
if (!empty($event_date_values) && !empty($event_date_revision_values)) {
foreach ($event_date_values as $id => $row) {
$database
->update($base_table_name)
->fields([
'event_date__value' => $row->event_start__value,
'event_date__end_value' => $row->event_end__value,
'event_date__timezone' => $row->event_start__timezone,
])
->condition($id_key, $id)
->execute();
}
foreach ($event_date_revision_values as $id => $row) {
$database
->update($revision_table_name)
->fields([
'event_date__value' => $row->event_start__value,
'event_date__end_value' => $row->event_end__value,
'event_date__timezone' => $row->event_start__timezone,
])
->condition($id_key, $id)
->execute();
}
}
}