class RecurRuleUpdate in Smart Date 8.2
Same name and namespace in other branches
- 3.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate
- 3.0.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate
- 3.1.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate
- 3.2.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate
- 3.3.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate
- 3.4.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate
Updates a rule's instances.
Plugin annotation
@QueueWorker(
id = "smart_date_recur_rules",
title = @Translation("Smart Date Recur rules refresh"),
cron = {"time" = 60}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Queue\QueueWorkerBase implements QueueWorkerInterface
- class \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate
- class \Drupal\Core\Queue\QueueWorkerBase implements QueueWorkerInterface
Expanded class hierarchy of RecurRuleUpdate
File
- modules/
smart_date_recur/ src/ Plugin/ QueueWorker/ RecurRuleUpdate.php, line 17
Namespace
Drupal\smart_date_recur\Plugin\QueueWorkerView source
class RecurRuleUpdate extends QueueWorkerBase {
/**
* {@inheritdoc}
*/
public function processItem($item) {
// If we don't have rules or an entity, there's nothing to do.
if (empty($item->data) || empty($item->entity_id)) {
return;
}
$entity_manager = \Drupal::entityTypeManager($item->entity_type);
$entity_storage = $entity_manager
->getStorage($item->entity_type);
$entity = $entity_storage
->load($item->entity_id);
// If we can't find the entity, there's nothing to do.
if (empty($entity)) {
return;
}
$rules_processed = [];
foreach ($item->data as $field_name => $rules) {
$field_values = $entity
->get($field_name)
->getValue();
$processed = [];
// Go through identified rules to see if new instances are needed.
foreach ($rules as $rrid) {
$rule = SmartDateRule::load($rrid);
$new_instances = $rule
->getNewInstances()
->toArray();
if (empty($new_instances)) {
// No instances to add, so no need to process this rule.
unset($rules[$rrid]);
continue;
}
$instances = $rule
->getStoredInstances();
$template = end($instances);
foreach ($new_instances as $new_instance) {
$template['value'] = $new_instance
->getStart()
->getTimestamp();
$template['end_value'] = $new_instance
->getEnd()
->getTimestamp();
$instances[] = $template;
}
// TODO: check for expired instances. Possible to keep indexes the same?
$rule
->set('instances', [
'data' => $instances,
]);
$rule
->save();
$rules[$rrid] = $instances;
}
foreach ($field_values as $delta => $row) {
// Skip if this instance isn't in a rule or in one we've ruled out.
if (empty($row['rrule']) || !isset($rules[$row['rrule']])) {
// Add directly to our array.
$processed[] = $row;
continue;
}
if (isset($rules_processed[$row['rrule']])) {
// Already handled this rule, so skip this row.
continue;
}
$instances = $rules[$row['rrule']];
foreach ($instances as $rrule_index => $instance) {
$row['value'] = $instance['value'];
$row['end_value'] = $instance['end_value'];
$row['rrule_index'] = $rrule_index;
$processed[] = $row;
}
// Track that this rule has been processed.
$rules_processed[$row['rrule']] = $row['rrule'];
}
// Update the entity with our new values.
$entity
->set($field_name, $processed);
}
if (!empty($rules_processed)) {
$entity
->save();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
RecurRuleUpdate:: |
public | function |
Works on a single queue item. Overrides QueueWorkerInterface:: |