View source
<?php
declare (strict_types=1);
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Utility\UpdateException;
use Drupal\date_recur\Entity\DateRecurInterpreter;
use Drupal\date_recur\Plugin\Field\FieldWidget\DateRecurBasicWidget;
use Drupal\field\Entity\FieldStorageConfig;
function date_recur_requirements($phase) : array {
$requirements = [];
if (!class_exists('\\RRule\\RRule')) {
$requirements['date_recur_dependencies'] = [
'title' => t('Date recur'),
'description' => \t('Date recur has unmet Composer dependencies. Read the <a href="@url">documentation</a> on how to install them.', [
'@url' => 'https://www.drupal.org/node/2627292',
]),
'severity' => \REQUIREMENT_ERROR,
];
}
return $requirements;
}
function date_recur_update_8201(&$sandbox) : void {
$fieldType = 'date_recur';
$schema = \Drupal::database()
->schema();
$entityTypeManager = \Drupal::entityTypeManager();
$entityFieldManager = \Drupal::service('entity_field.manager');
$entityFieldMap = $entityFieldManager
->getFieldMapByFieldType($fieldType);
$entityStorageSchemaSql = \Drupal::keyValue('entity.storage_schema.sql');
$lastInstalledSchemaRepository = \Drupal::service('entity.last_installed_schema.repository');
$specification = [
'description' => 'The repeat rule.',
'type' => 'text',
];
foreach ($entityFieldMap as $entityTypeId => $fields) {
$entityStorage = $entityTypeManager
->getStorage($entityTypeId);
if (!$entityStorage instanceof SqlEntityStorageInterface) {
continue;
}
$entityType = $entityTypeManager
->getDefinition($entityTypeId);
$entityFieldStorageDefinitions = $entityFieldManager
->getFieldStorageDefinitions($entityTypeId);
$tableMapping = $entityStorage
->getTableMapping($entityFieldStorageDefinitions);
$fieldDefinitions = array_intersect_key($entityFieldStorageDefinitions, $fields);
foreach ($fieldDefinitions as $fieldDefinition) {
$fieldName = $fieldDefinition
->getName();
$tables = [];
$tables[] = $tableMapping
->getFieldTableName($fieldName);
if ($entityType
->isRevisionable() && $fieldDefinition
->isRevisionable()) {
$tables[] = $tableMapping
->getDedicatedRevisionTableName($fieldDefinition);
}
$columns = $tableMapping
->getColumnNames($fieldName);
$rruleColumnName = $columns['rrule'];
foreach ($tables as $table) {
$schema
->changeField($table, $rruleColumnName, $rruleColumnName, $specification);
}
$schemaKey = "{$entityTypeId}.field_schema_data.{$fieldName}";
$fieldSchemaData = $entityStorageSchemaSql
->get($schemaKey);
foreach ($fieldSchemaData as $tableName => $fieldSchema) {
$fieldSchemaData[$tableName]['fields'][$rruleColumnName]['type'] = 'text';
unset($fieldSchemaData[$tableName]['fields'][$rruleColumnName]['length']);
}
$entityStorageSchemaSql
->set($schemaKey, $fieldSchemaData);
if ($tableMapping
->allowsSharedTableStorage($fieldDefinition)) {
$definitions = $lastInstalledSchemaRepository
->getLastInstalledFieldStorageDefinitions($entityTypeId);
$definitions[$fieldName] = $fieldDefinition;
$lastInstalledSchemaRepository
->setLastInstalledFieldStorageDefinitions($entityTypeId, $definitions);
}
}
}
}
function date_recur_update_8202(&$sandbox) : void {
$fieldType = 'date_recur';
$entityTypeManager = \Drupal::entityTypeManager();
$entityFieldManager = \Drupal::service('entity_field.manager');
$entityFieldMap = $entityFieldManager
->getFieldMapByFieldType($fieldType);
foreach ($entityFieldMap as $entityTypeId => $fields) {
$entityStorage = $entityTypeManager
->getStorage($entityTypeId);
if (!$entityStorage instanceof SqlEntityStorageInterface) {
continue;
}
$entityFieldStorageDefinitions = $entityFieldManager
->getFieldStorageDefinitions($entityTypeId);
$fieldDefinitions = array_intersect_key($entityFieldStorageDefinitions, $fields);
foreach ($fieldDefinitions as $fieldDefinition) {
if ($fieldDefinition instanceof FieldStorageConfig) {
$fieldDefinition
->setSetting('rrule_max_length', 256);
$fieldDefinition
->save();
}
}
}
}
function date_recur_update_8203(&$sandbox) : void {
$timeZoneList = timezone_identifiers_list();
$database = \Drupal::database();
$entityTypeManager = \Drupal::entityTypeManager();
$entityFieldManager = \Drupal::service('entity_field.manager');
$entityFieldMap = $entityFieldManager
->getFieldMapByFieldType('date_recur');
foreach ($entityFieldMap as $entityTypeId => $fields) {
$entityStorage = $entityTypeManager
->getStorage($entityTypeId);
if (!$entityStorage instanceof SqlEntityStorageInterface) {
continue;
}
$entityFieldStorageDefinitions = $entityFieldManager
->getFieldStorageDefinitions($entityTypeId);
$tableMapping = $entityStorage
->getTableMapping($entityFieldStorageDefinitions);
$fieldDefinitions = array_intersect_key($entityFieldStorageDefinitions, $fields);
foreach ($fieldDefinitions as $fieldDefinition) {
$fieldName = $fieldDefinition
->getName();
$table = $tableMapping
->getFieldTableName($fieldName);
$columns = $tableMapping
->getColumnNames($fieldName);
$timeZoneColumnName = $columns['timezone'];
$select = $database
->select($table, 'field_table');
$select
->addField('field_table', $timeZoneColumnName);
$select
->distinct();
$result = $select
->execute();
if (!$result) {
throw new \Exception('Problem executing query.');
}
$allTimeZones = $result
->fetchCol($timeZoneColumnName);
$diff = array_diff($allTimeZones, $timeZoneList);
if (count($diff)) {
throw new UpdateException(sprintf('Invalid time zones found for field `%s` in table `%s`: `%s`. Please resolve these invalid values manually before continuing.', $fieldName, $table, implode(', ', $diff)));
}
}
}
}
function date_recur_update_8204(&$sandbox) : void {
$displays = EntityFormDisplay::loadMultiple();
foreach ($displays as $display) {
$components = $display
->getComponents();
foreach ($components as $component => $options) {
$updated = FALSE;
$type = $options['type'] ?? NULL;
if ($type === 'date_recur_default_widget') {
$options['type'] = 'date_recur_basic_widget';
$updated = TRUE;
}
if ($updated) {
$display
->setComponent($component, $options);
}
}
$display
->save();
}
$displays = EntityViewDisplay::loadMultiple();
foreach ($displays as $display) {
$components = $display
->getComponents();
foreach ($components as $component => $options) {
$updated = FALSE;
$type = $options['type'] ?? NULL;
if ($type === 'date_recur_default_formatter') {
$options['type'] = 'date_recur_basic_formatter';
if (isset($options['show_rrule'])) {
unset($options['show_rrule']);
}
$updated = TRUE;
}
if ($updated) {
$display
->setComponent($component, $options);
}
}
$display
->save();
}
}
function date_recur_update_8205(&$sandbox) : void {
if (DateFormat::load('long') && !DateRecurInterpreter::load('default_interpreter')) {
$configFactory = \Drupal::configFactory();
$interpreter = $configFactory
->getEditable('date_recur.interpreter.default_interpreter');
$interpreter
->setData([
'id' => 'default_interpreter',
'label' => 'Default interpreter',
'plugin' => 'rl',
'settings' => [
'show_start_date' => TRUE,
'show_until' => TRUE,
'date_format' => 'long',
'show_infinite' => TRUE,
],
]);
$interpreter
->save(TRUE);
}
}
function date_recur_update_8206(&$sandbox) : void {
$basicWidgetClass = DateRecurBasicWidget::class;
$basicWidgetDerivativeWidgetIds = [];
$basicWidgetDerivativeWidgetIds[] = 'date_recur_basic_widget';
$fieldWidgetPluginManager = \Drupal::service('plugin.manager.field.widget');
foreach ($fieldWidgetPluginManager
->getDefinitions() as $widgetId => $definition) {
$class = $definition['class'];
if (class_exists($class) && (new \ReflectionClass($class))
->isSubclassOf($basicWidgetClass)) {
$basicWidgetDerivativeWidgetIds[] = $widgetId;
}
}
$displays = EntityFormDisplay::loadMultiple();
foreach ($displays as $display) {
$updated = 0;
$components = $display
->getComponents();
foreach ($components as $component => $options) {
$widgetId = $options['type'] ?? NULL;
if (in_array($widgetId, $basicWidgetDerivativeWidgetIds)) {
unset($options['settings']['timezone_override']);
$updated++;
$display
->setComponent($component, $options);
}
}
if ($updated > 0) {
$display
->save();
}
}
}