function smart_date_update_8202 in Smart Date 3.3.x
Same name and namespace in other branches
- 8.2 smart_date.install \smart_date_update_8202()
- 3.x smart_date.install \smart_date_update_8202()
- 3.0.x smart_date.install \smart_date_update_8202()
- 3.1.x smart_date.install \smart_date_update_8202()
- 3.2.x smart_date.install \smart_date_update_8202()
- 3.4.x smart_date.install \smart_date_update_8202()
Add rule index column for recurring dates.
File
- ./
smart_date.install, line 39
Code
function smart_date_update_8202() {
$field_type = 'smartdate';
// Define the columns we want to add.
$specs = [
'rrule_index' => [
'type' => 'int',
'label' => t('RRule Index'),
'unsigned' => TRUE,
'not null' => FALSE,
],
];
_smart_date_field_columns_add($field_type, $specs);
// If Smart Date Recur isn't enabled, nothing more to do.
if (!\Drupal::moduleHandler()
->moduleExists('smart_date_recur')) {
return;
}
// Now, populate the new column.
$rules = \Drupal::entityTypeManager()
->getStorage('smart_date_rule')
->loadMultiple();
if (empty($rules)) {
return;
}
$manager = \Drupal::entityDefinitionUpdateManager();
$database = \Drupal::database();
foreach ($rules as $rule) {
// TODO: Need to populate $before here?
$instances = $rule
->getRuleInstances();
if (!$instances) {
continue;
}
$rid = $rule
->id();
$entity_type_id = $rule->entity_type
->getString();
$field_name = $rule->field_name
->getString();
// Get db table.
$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) {
// Update the row.
$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();
}
}
}