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 7b28d85
Show file tree
Hide file tree
Showing 3 changed files with 241 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
239 changes: 239 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,241 @@ 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';
if (!isset($sandbox['data_rows'])) {
// The table data to restore after the update is completed.
$sandbox['data_rows'] = $database->select("node__$field", 'n')
->fields('n')
->execute()
->fetchAll();
$sandbox['revision_rows'] = $database->select("node_revision__$field", 'n')
->fields('n')
->execute()
->fetchAll();

$sandbox['data_total'] = count($sandbox['data_rows']);
$sandbox['revision_total'] = count($sandbox['revision_rows']);
$sandbox['current'] = 0;

// 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();

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

// Restore existing data in the same table by 50 records per batch.
$data_rows = array_slice($sandbox['data_rows'], $sandbox['current'], 50);
foreach ($data_rows as $row) {
$database->insert("node__$field")
->fields((array) $row)
->execute();
}

$revision_rows = array_slice($sandbox['revision_rows'], $sandbox['current'], 50);
foreach ($revision_rows as $row) {
$database->insert("node_revision__$field")
->fields((array) $row)
->execute();
}

$sandbox['current'] += 50;

$sandbox['#finished'] = empty($sandbox['data_rows']) || $sandbox['current'] >= $sandbox['data_total'] && $sandbox['current'] >= $sandbox['revision_total'];

if ($sandbox['#finished'] === TRUE) {
return t('Finished converting the %field to daterange_timezone type.', ['%field' => $field]);
}
}

/**
* 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';
if (!isset($sandbox['data_rows'])) {
// The table data to restore after the update is completed.
$sandbox['data_rows'] = $database->select("node__$field", 'n')
->fields('n')
->execute()
->fetchAll();
$sandbox['revision_rows'] = $database->select("node_revision__$field", 'n')
->fields('n')
->execute()
->fetchAll();

$sandbox['data_total'] = count($sandbox['data_rows']);
$sandbox['revision_total'] = count($sandbox['revision_rows']);
$sandbox['current'] = 0;

// 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();

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

// Restore existing data in the same table by 50 records per batch.
$data_rows = array_slice($sandbox['data_rows'], $sandbox['current'], 50);
foreach ($data_rows as $row) {
$database->insert("node__$field")
->fields((array) $row)
->execute();
}

$revision_rows = array_slice($sandbox['revision_rows'], $sandbox['current'], 50);
foreach ($revision_rows as $row) {
$database->insert("node_revision__$field")
->fields((array) $row)
->execute();
}

$sandbox['current'] += 50;

$sandbox['#finished'] = empty($sandbox['data_rows']) || $sandbox['current'] >= $sandbox['data_total'] && $sandbox['current'] >= $sandbox['revision_total'];

if ($sandbox['#finished'] === TRUE) {
return t('Finished converting the %field to daterange_timezone type.', ['%field' => $field]);
}
}

/**
* 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';
if (!isset($sandbox['data_rows'])) {
// The table data to restore after the update is completed.
$sandbox['data_rows'] = $database->select("node__$field", 'n')
->fields('n')
->execute()
->fetchAll();
$sandbox['revision_rows'] = $database->select("node_revision__$field", 'n')
->fields('n')
->execute()
->fetchAll();

$sandbox['data_total'] = count($sandbox['data_rows']);
$sandbox['revision_total'] = count($sandbox['revision_rows']);
$sandbox['current'] = 0;

// 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();

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

// Restore existing data in the same table by 50 records per batch.
$data_rows = array_slice($sandbox['data_rows'], $sandbox['current'], 50);
foreach ($data_rows as $row) {
$database->insert("node__$field")
->fields((array) $row)
->execute();
}

$revision_rows = array_slice($sandbox['revision_rows'], $sandbox['current'], 50);
foreach ($revision_rows as $row) {
$database->insert("node_revision__$field")
->fields((array) $row)
->execute();
}

$sandbox['current'] += 50;

$sandbox['#finished'] = empty($sandbox['data_rows']) || $sandbox['current'] >= $sandbox['data_total'] && $sandbox['current'] >= $sandbox['revision_total'];

if ($sandbox['#finished'] === TRUE) {
return t('Finished converting the %field to daterange_timezone type.', ['%field' => $field]);
}
}

0 comments on commit 7b28d85

Please sign in to comment.