Skip to content

Commit

Permalink
EWPP-1764: Replace event date fields with timezone ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
nagyad committed Feb 10, 2022
1 parent 73bed9a commit 07c57a4
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"require": {
"php": ">=7.3",
"drupal/core": "^8.9.14 || ^9.1",
"drupal/datetime_range_timezone": "1.x-dev",
"drupal/linkit": "^5.0-beta11",
"drupal/maxlength": "^1.0@beta",
"easyrdf/easyrdf": "1.0.0 as 0.9.2",
Expand Down
1 change: 1 addition & 0 deletions modules/oe_content_event/oe_content_event.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
- oe_media_avportal:oe_media_avportal
- oe_corporate_countries:oe_corporate_countries_address
- drupal:datetime_range
- datetime_range_timezone:datetime_range_timezone
- typed_link:typed_link
- field_group:field_group
- inline_entity_form:inline_entity_form
Expand Down
278 changes: 278 additions & 0 deletions modules/oe_content_event/oe_content_event.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Drupal\Core\Config\FileStorage;
use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldStorageConfig;

/**
* Make the Event venue and contact fields composite.
Expand Down Expand Up @@ -129,3 +130,280 @@ function oe_content_event_post_update_20003(): void {
$field_config->setSetting('title', 2);
$field_config->save();
}

/**
* Enable datetime_range_timezone module.
*/
function oe_content_event_post_update_20004(array &$sandbox) {
\Drupal::service('module_installer')->install(['datetime_range_timezone']);
}

/**
* Convert oe_event_dates field to daterange_timezone.
*/
function oe_content_event_post_update_20005(array &$sandbox) {
$database = \Drupal::database();
$field = 'oe_event_dates';

// Check if we have any records in the field data table.
$count = $database->select("node__$field", 'n')
->countQuery()
->execute()
->fetchField();
if ($count > 0) {
// Create a table based on the one we want to back up.
$data_table = "node__$field";
$backup_data_table = "_node__$field";
$query_string = 'CREATE TABLE ' . $backup_data_table . ' LIKE ' . $data_table;
$database->query($query_string);

// Copy every record from the field table to the backup table.
$query_string = 'INSERT ' . $backup_data_table . ' SELECT * FROM ' . $data_table;
$database->query($query_string);

// Now do the same for the field revisions table.
$revision_table = "node_revision__$field";
$backup_revision_table = "_node_revision__$field";
$query_string = 'CREATE TABLE ' . $backup_revision_table . ' LIKE ' . $revision_table;
$database->query($query_string);
$query_string = 'INSERT ' . $backup_revision_table . ' SELECT * FROM ' . $revision_table;
$database->query($query_string);
}

// Load up the form display in its original state.
$form_display = \Drupal::service('entity_display.repository')
->getFormDisplay('node', 'oe_event', 'default');

/** @var \Drupal\field\Entity\FieldStorageConfig $field_config */
$field_storage = FieldStorageConfig::load("node.$field");
$new_field_storage = $field_storage;
$new_field_storage->set('type', 'daterange_timezone');
$new_field_storage = $new_field_storage->toArray();
$new_field_storage = FieldStorageConfig::create($new_field_storage);

/** @var \Drupal\field\Entity\FieldConfig $field_config */
$field_config = FieldConfig::load("node.oe_event.$field");
$new_field_config = $field_config;
$new_field_config->set('field_type', 'daterange_timezone');
$new_field_config = $new_field_config->toArray();
$new_field_config = FieldConfig::create($new_field_config);
$field_config->delete();

// Purge field data.
field_purge_batch(50);

// Save the new field.
$new_field_storage->save();
$new_field_config->save();

// Update form display.
$component = $form_display->getComponent($field);
$component['type'] = 'daterange_timezone';
$form_display->setComponent($field, $component);
$form_display->save();

if ($count > 0) {
// Now we need to account for an additional column for timezone, so we
// change the schema of the backup table accordingly.
$query_string = 'ALTER TABLE ' . $backup_data_table . ' ADD COLUMN ' . $field . '_timezone VARCHAR(255)';
$database->query($query_string);
$query_string = 'ALTER TABLE ' . $backup_revision_table . ' ADD COLUMN ' . $field . '_timezone VARCHAR(255)';
$database->query($query_string);

// Now make sure we add 'Europe/Brussels' timezone.
$query_string = 'UPDATE ' . $backup_data_table . ' SET ' . $field . '_timezone = \'Europe/Brussels\'';
$database->query($query_string);
$query_string = 'UPDATE ' . $backup_revision_table . ' SET ' . $field . '_timezone = \'Europe/Brussels\'';
$database->query($query_string);

// Restore existing data in the same field table.
$query_string = 'INSERT ' . $data_table . ' SELECT * FROM ' . $backup_data_table;
$database->query($query_string);
$query_string = 'INSERT ' . $revision_table . ' SELECT * FROM ' . $backup_revision_table;
$database->query($query_string);

// Delete the backup tables from the database.
$database->query('DROP TABLE ' . $backup_data_table);
$database->query('DROP TABLE ' . $backup_revision_table);
}
}

/**
* Convert oe_event_online_dates field to daterange_timezone.
*/
function oe_content_event_post_update_20006(array &$sandbox) {
$database = \Drupal::database();
$field = 'oe_event_online_dates';

// Check if we have any records in the field data table.
$count = $database->select("node__$field", 'n')
->countQuery()
->execute()
->fetchField();
if ($count > 0) {
// Create a table based on the one we want to back up.
$data_table = "node__$field";
$backup_data_table = "_node__$field";
$query_string = 'CREATE TABLE ' . $backup_data_table . ' LIKE ' . $data_table;
$database->query($query_string);

// Copy every record from the field table to the backup table.
$query_string = 'INSERT ' . $backup_data_table . ' SELECT * FROM ' . $data_table;
$database->query($query_string);

// Now do the same for the field revisions table.
$revision_table = "node_revision__$field";
$backup_revision_table = "_node_revision__$field";
$query_string = 'CREATE TABLE ' . $backup_revision_table . ' LIKE ' . $revision_table;
$database->query($query_string);
$query_string = 'INSERT ' . $backup_revision_table . ' SELECT * FROM ' . $revision_table;
$database->query($query_string);
}

// Load up the form display in its original state.
$form_display = \Drupal::service('entity_display.repository')
->getFormDisplay('node', 'oe_event', 'default');

/** @var \Drupal\field\Entity\FieldStorageConfig $field_config */
$field_storage = FieldStorageConfig::load("node.$field");
$new_field_storage = $field_storage;
$new_field_storage->set('type', 'daterange_timezone');
$new_field_storage = $new_field_storage->toArray();
$new_field_storage = FieldStorageConfig::create($new_field_storage);

/** @var \Drupal\field\Entity\FieldConfig $field_config */
$field_config = FieldConfig::load("node.oe_event.$field");
$new_field_config = $field_config;
$new_field_config->set('field_type', 'daterange_timezone');
$new_field_config = $new_field_config->toArray();
$new_field_config = FieldConfig::create($new_field_config);
$field_config->delete();

// Purge field data.
field_purge_batch(50);

// Save the new field.
$new_field_storage->save();
$new_field_config->save();

// Update form display.
$component = $form_display->getComponent($field);
$component['type'] = 'daterange_timezone';
$form_display->setComponent($field, $component);
$form_display->save();

if ($count > 0) {
// Now we need to account for an additional column for timezone, so we
// change the schema of the backup table accordingly.
$query_string = 'ALTER TABLE ' . $backup_data_table . ' ADD COLUMN ' . $field . '_timezone VARCHAR(255)';
$database->query($query_string);
$query_string = 'ALTER TABLE ' . $backup_revision_table . ' ADD COLUMN ' . $field . '_timezone VARCHAR(255)';
$database->query($query_string);

// Now make sure we add 'Europe/Brussels' timezone.
$query_string = 'UPDATE ' . $backup_data_table . ' SET ' . $field . '_timezone = \'Europe/Brussels\'';
$database->query($query_string);
$query_string = 'UPDATE ' . $backup_revision_table . ' SET ' . $field . '_timezone = \'Europe/Brussels\'';
$database->query($query_string);

// Restore existing data in the same field table.
$query_string = 'INSERT ' . $data_table . ' SELECT * FROM ' . $backup_data_table;
$database->query($query_string);
$query_string = 'INSERT ' . $revision_table . ' SELECT * FROM ' . $backup_revision_table;
$database->query($query_string);

// Delete the backup tables from the database.
$database->query('DROP TABLE ' . $backup_data_table);
$database->query('DROP TABLE ' . $backup_revision_table);
}
}

/**
* Convert oe_event_registration_dates field to daterange_timezone.
*/
function oe_content_event_post_update_20007(array &$sandbox) {
$database = \Drupal::database();
$field = 'oe_event_registration_dates';

// Check if we have any records in the field data table.
$count = $database->select("node__$field", 'n')
->countQuery()
->execute()
->fetchField();
if ($count > 0) {
// Create a table based on the one we want to back up.
$data_table = "node__$field";
$backup_data_table = "_node__$field";
$query_string = 'CREATE TABLE ' . $backup_data_table . ' LIKE ' . $data_table;
$database->query($query_string);

// Copy every record from the field table to the backup table.
$query_string = 'INSERT ' . $backup_data_table . ' SELECT * FROM ' . $data_table;
$database->query($query_string);

// Now do the same for the field revisions table.
$revision_table = "node_revision__$field";
$backup_revision_table = "_node_revision__$field";
$query_string = 'CREATE TABLE ' . $backup_revision_table . ' LIKE ' . $revision_table;
$database->query($query_string);
$query_string = 'INSERT ' . $backup_revision_table . ' SELECT * FROM ' . $revision_table;
$database->query($query_string);
}

// Load up the form display in its original state.
$form_display = \Drupal::service('entity_display.repository')
->getFormDisplay('node', 'oe_event', 'default');

/** @var \Drupal\field\Entity\FieldStorageConfig $field_config */
$field_storage = FieldStorageConfig::load("node.$field");
$new_field_storage = $field_storage;
$new_field_storage->set('type', 'daterange_timezone');
$new_field_storage = $new_field_storage->toArray();
$new_field_storage = FieldStorageConfig::create($new_field_storage);

/** @var \Drupal\field\Entity\FieldConfig $field_config */
$field_config = FieldConfig::load("node.oe_event.$field");
$new_field_config = $field_config;
$new_field_config->set('field_type', 'daterange_timezone');
$new_field_config = $new_field_config->toArray();
$new_field_config = FieldConfig::create($new_field_config);
$field_config->delete();

// Purge field data.
field_purge_batch(50);

// Save the new field.
$new_field_storage->save();
$new_field_config->save();

// Update form display.
$component = $form_display->getComponent($field);
$component['type'] = 'daterange_timezone';
$form_display->setComponent($field, $component);
$form_display->save();

if ($count > 0) {
// Now we need to account for an additional column for timezone, so we
// change the schema of the backup table accordingly.
$query_string = 'ALTER TABLE ' . $backup_data_table . ' ADD COLUMN ' . $field . '_timezone VARCHAR(255)';
$database->query($query_string);
$query_string = 'ALTER TABLE ' . $backup_revision_table . ' ADD COLUMN ' . $field . '_timezone VARCHAR(255)';
$database->query($query_string);

// Now make sure we add 'Europe/Brussels' timezone.
$query_string = 'UPDATE ' . $backup_data_table . ' SET ' . $field . '_timezone = \'Europe/Brussels\'';
$database->query($query_string);
$query_string = 'UPDATE ' . $backup_revision_table . ' SET ' . $field . '_timezone = \'Europe/Brussels\'';
$database->query($query_string);

// Restore existing data in the same field table.
$query_string = 'INSERT ' . $data_table . ' SELECT * FROM ' . $backup_data_table;
$database->query($query_string);
$query_string = 'INSERT ' . $revision_table . ' SELECT * FROM ' . $backup_revision_table;
$database->query($query_string);

// Delete the backup tables from the database.
$database->query('DROP TABLE ' . $backup_data_table);
$database->query('DROP TABLE ' . $backup_revision_table);
}
}

0 comments on commit 07c57a4

Please sign in to comment.