3 namespace Drupal\Core\Config\Entity;
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
10 * A utility class to make updating configuration entities simple.
12 * Use this in a post update function like so:
14 * // Update the dependencies of all Vocabulary configuration entities.
15 * \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'taxonomy_vocabulary');
18 * The number of entities processed in each batch is determined by the
19 * 'entity_update_batch_size' setting.
21 * @see default.settings.php
23 class ConfigEntityUpdater implements ContainerInjectionInterface {
26 * The entity type manager.
28 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
30 protected $entityTypeManager;
33 * The number of entities to process in each batch.
39 * ConfigEntityUpdater constructor.
41 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
42 * The entity type manager.
43 * @param int $batch_size
44 * The number of entities to process in each batch.
46 public function __construct(EntityTypeManagerInterface $entity_type_manager, $batch_size) {
47 $this->entityTypeManager = $entity_type_manager;
48 $this->batchSize = $batch_size;
54 public static function create(ContainerInterface $container) {
56 $container->get('entity_type.manager'),
57 $container->get('settings')->get('entity_update_batch_size', 50)
62 * Updates configuration entities as part of a Drupal update.
64 * @param array $sandbox
65 * Stores information for batch updates.
66 * @param string $entity_type_id
67 * The configuration entity type ID. For example, 'view' or 'vocabulary'.
68 * @param callable $callback
69 * (optional) A callback to determine if a configuration entity should be
70 * saved. The callback will be passed each entity of the provided type that
71 * exists. The callback should not save an entity itself. Return TRUE to
72 * save an entity. The callback can make changes to an entity. Note that all
73 * changes should comply with schema as an entity's data will not be
74 * validated against schema on save to avoid unexpected errors. If a
75 * callback is not provided, the default behaviour is to update the
76 * dependencies if required.
78 * @see hook_post_update_NAME()
82 * @throws \InvalidArgumentException
83 * Thrown when the provided entity type ID is not a configuration entity
86 public function update(array &$sandbox, $entity_type_id, callable $callback = NULL) {
87 $storage = $this->entityTypeManager->getStorage($entity_type_id);
88 $sandbox_key = 'config_entity_updater:' . $entity_type_id;
89 if (!isset($sandbox[$sandbox_key])) {
90 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
91 if (!($entity_type instanceof ConfigEntityTypeInterface)) {
92 throw new \InvalidArgumentException("The provided entity type ID '$entity_type_id' is not a configuration entity type");
94 $sandbox[$sandbox_key]['entities'] = $storage->getQuery()->accessCheck(FALSE)->execute();
95 $sandbox[$sandbox_key]['count'] = count($sandbox[$sandbox_key]['entities']);
98 // The default behaviour is to fix dependencies.
99 if ($callback === NULL) {
100 $callback = function ($entity) {
101 /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
102 $original_dependencies = $entity->getDependencies();
103 return $original_dependencies !== $entity->calculateDependencies()->getDependencies();
107 /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
108 $entities = $storage->loadMultiple(array_splice($sandbox[$sandbox_key]['entities'], 0, $this->batchSize));
109 foreach ($entities as $entity) {
110 if (call_user_func($callback, $entity)) {
111 $entity->trustData();
116 $sandbox['#finished'] = empty($sandbox[$sandbox_key]['entities']) ? 1 : ($sandbox[$sandbox_key]['count'] - count($sandbox[$sandbox_key]['entities'])) / $sandbox[$sandbox_key]['count'];