This commit is contained in:
naomi 2022-05-16 17:25:58 +01:00
parent f6792e5053
commit d483b3f47e
2 changed files with 11 additions and 8 deletions

View File

@ -1,7 +1,6 @@
<?php declare(strict_types = 1);
namespace Drupal\opencase;
use Drupal;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\opencase\Utils;
@ -9,9 +8,10 @@ final class TimeBasedFieldUpdater {
private EntityTypeManagerInterface $entityTypeManager;
private string $date_field;
private Utils $utils;
private string $entity_type;
private array $conditions;
private string $date_format;
private string $bundle;
final public function __construct(
EntityTypeManagerInterface $entityTypeManager,
@ -33,11 +33,14 @@ final class TimeBasedFieldUpdater {
$conditions[] = ['type', $this->bundle, '='];
$this->utils->addConditionsToQuery($query, $conditions);
foreach($query->execute() as $id) {
$entity = $this->entityTypeManager->getStorage($this->entity_type)->load($id);
$this->updateEntity($id, $new_values);
}
}
private function updateEntity(int $entity_id, array $new_values): void {
$entity = $this->entityTypeManager->getStorage($this->entity_type)->load($entity_id);
foreach($new_values as $new_field=>$new_value) {
$entity->$new_field = $new_value;
}
$entity->save();
}
}
}