View source
<?php
declare (strict_types=1);
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\node\NodeStorage;
function smart_date_update_8201() {
$field_type = 'smartdate';
$specs = [
'rrule' => [
'type' => 'int',
'label' => t('RRule ID'),
'unsigned' => TRUE,
'not null' => FALSE,
],
'timezone' => [
'type' => 'varchar',
'label' => t('Timezone'),
'length' => 32,
],
];
_smart_date_field_columns_add($field_type, $specs, FALSE);
}
function smart_date_update_8202() {
$field_type = 'smartdate';
$specs = [
'rrule_index' => [
'type' => 'int',
'label' => t('RRule Index'),
'unsigned' => TRUE,
'not null' => FALSE,
],
];
_smart_date_field_columns_add($field_type, $specs);
if (!\Drupal::moduleHandler()
->moduleExists('smart_date_recur')) {
return;
}
$rules = \Drupal::entityTypeManager()
->getStorage('smart_date_rule')
->loadMultiple();
if (empty($rules)) {
return;
}
$manager = \Drupal::entityDefinitionUpdateManager();
$database = \Drupal::database();
foreach ($rules as $rule) {
$instances = $rule
->getRuleInstances();
if (!$instances) {
continue;
}
$rid = $rule
->id();
$entity_type_id = $rule->entity_type
->getString();
$field_name = $rule->field_name
->getString();
$field_storage_definition = $manager
->getFieldStorageDefinition($field_name, $entity_type_id);
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
$table_mapping = $storage
->getTableMapping([
$field_name => $field_storage_definition,
]);
$table_name = $table_mapping
->getDedicatedDataTableName($field_storage_definition);
foreach ($instances as $index => $instance) {
$query = $database
->update($table_name)
->fields([
$field_name . '_rrule_index' => $index,
])
->condition($field_name . '_value', $instance['value'])
->condition($field_name . '_end_value', $instance['end_value'])
->condition($field_name . '_rrule', $rid)
->condition($field_name . '_rrule_index', NULL, 'IS NULL')
->execute();
}
}
}
function smart_date_update_8301() {
$format_storage = \Drupal::entityTypeManager()
->getStorage('smart_date_format');
$formats = $format_storage
->loadMultiple(NULL);
foreach ($formats as $format) {
$format
->set('site_time_toggle', 1);
$format
->save();
}
}
function _smart_date_field_columns_add($field_type, array $specs, $update_definitions = TRUE) {
if (!($field_storage_configs = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->loadByProperties([
'type' => $field_type,
]))) {
return;
}
$manager = \Drupal::entityDefinitionUpdateManager();
foreach ($field_storage_configs as $field_storage) {
$field_name = $field_storage
->getName();
$entity_type_id = $field_storage
->getTargetEntityTypeId();
$field_storage_definition = $manager
->getFieldStorageDefinition($field_name, $entity_type_id);
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
if (!$storage instanceof NodeStorage && !$storage instanceof SqlContentEntityStorage) {
return;
}
$table_mapping = $storage
->getTableMapping([
$field_name => $field_storage_definition,
]);
$table_names = $table_mapping
->getDedicatedTableNames();
$columns = $table_mapping
->getColumnNames($field_name);
foreach ($table_names as $table_name) {
$field_schema = $field_storage_definition
->getSchema();
$schema = \Drupal::database()
->schema();
$table_exists = $schema
->tableExists($table_name);
if (!$table_exists) {
continue;
}
foreach ($specs as $name => $spec) {
$new_column = $field_name . '_' . $name;
$field_exists = $schema
->fieldExists($table_name, $new_column);
if (!$field_exists) {
$schema
->addField($table_name, $new_column, $spec, [
'fields' => [
$new_column => $spec,
],
'indexes' => [
$new_column => [
$new_column,
],
],
]);
}
}
}
if ($update_definitions) {
$manager
->updateFieldStorageDefinition($field_storage_definition);
}
}
}