refactor
This commit is contained in:
parent
f6792e5053
commit
d483b3f47e
@ -1,7 +1,6 @@
|
|||||||
<?php declare(strict_types = 1);
|
<?php declare(strict_types = 1);
|
||||||
|
|
||||||
namespace Drupal\opencase;
|
namespace Drupal\opencase;
|
||||||
use Drupal;
|
|
||||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
use Drupal\opencase\Utils;
|
use Drupal\opencase\Utils;
|
||||||
|
|
||||||
@ -9,9 +8,10 @@ final class TimeBasedFieldUpdater {
|
|||||||
|
|
||||||
private EntityTypeManagerInterface $entityTypeManager;
|
private EntityTypeManagerInterface $entityTypeManager;
|
||||||
private string $date_field;
|
private string $date_field;
|
||||||
|
private Utils $utils;
|
||||||
private string $entity_type;
|
private string $entity_type;
|
||||||
private array $conditions;
|
|
||||||
private string $date_format;
|
private string $date_format;
|
||||||
|
private string $bundle;
|
||||||
|
|
||||||
final public function __construct(
|
final public function __construct(
|
||||||
EntityTypeManagerInterface $entityTypeManager,
|
EntityTypeManagerInterface $entityTypeManager,
|
||||||
@ -33,11 +33,14 @@ final class TimeBasedFieldUpdater {
|
|||||||
$conditions[] = ['type', $this->bundle, '='];
|
$conditions[] = ['type', $this->bundle, '='];
|
||||||
$this->utils->addConditionsToQuery($query, $conditions);
|
$this->utils->addConditionsToQuery($query, $conditions);
|
||||||
foreach($query->execute() as $id) {
|
foreach($query->execute() as $id) {
|
||||||
$entity = $this->entityTypeManager->getStorage($this->entity_type)->load($id);
|
$this->updateEntity($id, $new_values);
|
||||||
foreach($new_values as $new_field=>$new_value) {
|
|
||||||
$entity->$new_field = $new_value;
|
|
||||||
}
|
|
||||||
$entity->save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
@ -50,4 +50,4 @@ class Utils {
|
|||||||
$term = reset($terms);
|
$term = reset($terms);
|
||||||
return (int)(!empty($term) ? $term->id() : 0);
|
return (int)(!empty($term) ? $term->id() : 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user