Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Config / Entity / ConfigEntityUpdater.php
1 <?php
2
3 namespace Drupal\Core\Config\Entity;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8
9 /**
10  * A utility class to make updating configuration entities simple.
11  *
12  * Use this in a post update function like so:
13  * @code
14  * // Update the dependencies of all Vocabulary configuration entities.
15  * \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'taxonomy_vocabulary');
16  * @endcode
17  *
18  * The number of entities processed in each batch is determined by the
19  * 'entity_update_batch_size' setting.
20  *
21  * @see default.settings.php
22  */
23 class ConfigEntityUpdater implements ContainerInjectionInterface {
24
25   /**
26    * The entity type manager.
27    *
28    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
29    */
30   protected $entityTypeManager;
31
32   /**
33    * The number of entities to process in each batch.
34    * @var int
35    */
36   protected $batchSize;
37
38   /**
39    * ConfigEntityUpdater constructor.
40    *
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.
45    */
46   public function __construct(EntityTypeManagerInterface $entity_type_manager, $batch_size) {
47     $this->entityTypeManager = $entity_type_manager;
48     $this->batchSize = $batch_size;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function create(ContainerInterface $container) {
55     return new static(
56       $container->get('entity_type.manager'),
57       $container->get('settings')->get('entity_update_batch_size', 50)
58     );
59   }
60
61   /**
62    * Updates configuration entities as part of a Drupal update.
63    *
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.
77    *
78    * @see hook_post_update_NAME()
79    *
80    * @api
81    *
82    * @throws \InvalidArgumentException
83    *   Thrown when the provided entity type ID is not a configuration entity
84    *   type.
85    */
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");
93       }
94       $sandbox[$sandbox_key]['entities'] = $storage->getQuery()->accessCheck(FALSE)->execute();
95       $sandbox[$sandbox_key]['count'] = count($sandbox[$sandbox_key]['entities']);
96     }
97
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();
104       };
105     }
106
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();
112         $entity->save();
113       }
114     }
115
116     $sandbox['#finished'] = empty($sandbox[$sandbox_key]['entities']) ? 1 : ($sandbox[$sandbox_key]['count'] - count($sandbox[$sandbox_key]['entities'])) / $sandbox[$sandbox_key]['count'];
117   }
118
119 }