class ConfigEntityUpdater implements ContainerInjectionInterface (View source)

A utility class to make updating configuration entities simple.

Use this in a post update function like so:

Constants

SANDBOX_KEY

The key used to store information in the update sandbox.

Properties

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

protected int $batchSize

The number of entities to process in each batch.

Methods

__construct(EntityTypeManagerInterface $entity_type_manager, int $batch_size)

ConfigEntityUpdater constructor.

static 
create(ContainerInterface $container)

Instantiates a new instance of this class.

update(array $sandbox, string $entity_type_id, callable $callback = NULL)

Updates configuration entities as part of a Drupal update.

Details

__construct(EntityTypeManagerInterface $entity_type_manager, int $batch_size)

ConfigEntityUpdater constructor.

Parameters

EntityTypeManagerInterface $entity_type_manager

The entity type manager.

int $batch_size

The number of entities to process in each batch.

static create(ContainerInterface $container)

Instantiates a new instance of this class.

This is a factory method that returns a new instance of this class. The factory should pass any needed dependencies into the constructor of this class, but not the container itself. Every call to this method must return a new instance of this class; that is, it may not implement a singleton.

Parameters

ContainerInterface $container

The service container this instance should use.

update(array $sandbox, string $entity_type_id, callable $callback = NULL)

Updates configuration entities as part of a Drupal update.

Parameters

array $sandbox

Stores information for batch updates.

string $entity_type_id

The configuration entity type ID. For example, 'view' or 'vocabulary'. The calling code should ensure that the entity type exists beforehand (i.e., by checking that the entity type is defined or that the module that provides it is installed).

callable $callback

(optional) A callback to determine if a configuration entity should be saved. The callback will be passed each entity of the provided type that exists. The callback should not save an entity itself. Return TRUE to save an entity. The callback can make changes to an entity. Note that all changes should comply with schema as an entity's data will not be validated against schema on save to avoid unexpected errors. If a callback is not provided, the default behavior is to update the dependencies if required.

Exceptions

InvalidArgumentException
RuntimeException

See also

hook_post_update_NAME()