37 lines
1.3 KiB
PHP
37 lines
1.3 KiB
PHP
|
<?php declare(strict_types = 1);
|
||
|
|
||
|
namespace Drupal\opencase;
|
||
|
use Drupal;
|
||
|
|
||
|
final class TimeBasedFieldUpdater {
|
||
|
private string $date_field;
|
||
|
private string $entity_type;
|
||
|
private array $conditions;
|
||
|
private string $date_format;
|
||
|
|
||
|
final public function __construct($entity_type, $date_field, $conditions = [], $date_format = 'Y-m-d')
|
||
|
{
|
||
|
$this->date_field = $date_field;
|
||
|
$this->conditions = $conditions;
|
||
|
$this->date_format = $date_format;
|
||
|
$this->entity_type = $entity_type;
|
||
|
}
|
||
|
|
||
|
final public function update($time_elapsed, $old_values, $new_values): void {
|
||
|
$query = Drupal::entityQuery($this->entity_type);
|
||
|
foreach($this->conditions as $cond_field=>$cond_value) {
|
||
|
$query->condition($cond_field, $cond_value);
|
||
|
}
|
||
|
foreach($old_values as $old_field=>$old_value) {
|
||
|
$query->condition($old_field, $old_value);
|
||
|
}
|
||
|
$query->condition($this->date_field, date($this->date_format, strtotime('-'.$time_elapsed)), "<");
|
||
|
foreach($query->execute() as $id) {
|
||
|
$entity = Drupal::entityTypeManager()->getStorage($this->entity_type)->load($id);
|
||
|
foreach($new_values as $new_field=>$new_value) {
|
||
|
$entity->set($new_field, $new_value);
|
||
|
}
|
||
|
$entity->save();
|
||
|
}
|
||
|
}
|
||
|
}
|