ContentEntityUpdater
A utility class to make updating content entities simple.
Use this in a post update function like so:
- Full name:
\Drupal\helper\Entity\ContentEntityUpdater - This class implements:
ContainerInjectionInterface
See Also:
- \Drupal\Core\Config\Entity\ConfigEntityUpdater
Constants
| Constant | Visibility | Type | Value |
|---|---|---|---|
SANDBOX_KEY |
protected | 'content_entity_updater' |
Properties
entityTypeManager
protected \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
entityFieldManager
protected \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
entityTypeHelper
protected \Drupal\helper\EntityType $entityTypeHelper
batchSize
protected int $batchSize
Methods
__construct
ContentEntityUpdater constructor.
public __construct(\Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager, \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager, \Drupal\helper\EntityType $entityTypeHelper, \Psr\Log\LoggerInterface $logger, int $batchSize): mixed
Parameters:
| Parameter | Type | Description |
|---|---|---|
$entityTypeManager |
\Drupal\Core\Entity\EntityTypeManagerInterface | |
| The entity type manager. | ||
$entityFieldManager |
\Drupal\Core\Entity\EntityFieldManagerInterface | |
| The entity field manager. | ||
$entityTypeHelper |
\Drupal\helper\EntityType | |
| The entity type helper. | ||
$logger |
\Psr\Log\LoggerInterface | |
| The logger. | ||
$batchSize |
int | |
| The number of entities to process in each batch. |
create
{@inheritdoc}
public static create(\Symfony\Component\DependencyInjection\ContainerInterface $container): mixed
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$container |
\Symfony\Component\DependencyInjection\ContainerInterface |
update
Updates content entities as part of a Drupal update.
public update(array& $sandbox, string|\Drupal\Core\Entity\Query\QueryInterface $entityTypeIdOrQuery, callable $callback, bool $continue_on_error = false): \Drupal\Core\StringTranslation\TranslatableMarkup|null
This is best used as with hook_post_update_NAME() or hook_deploy_NAME().
Parameters:
| Parameter | Type | Description |
|---|---|---|
$sandbox |
array | |
| Stores information for batch updates. | ||
$entityTypeIdOrQuery |
string|\Drupal\Core\Entity\Query\QueryInterface | |
| The content entity type ID or entity query object for | ||
| \Drupal\helper\EntityType::getQuery(). | ||
$callback |
callable | |
| A callback to determine if a content 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. | ||
$continue_on_error |
bool | |
| Set to TRUE to continue updating if an error has occurred. |
Return Value:
An error message if $continue_on_error is set to TRUE and an error has occurred.
Throws:
Thrown when used twice in the same update function for different entity
types. This method should only be called once per update function.
- RuntimeException
See Also:
- \Drupal\helper\Entity\hook_post_update_NAME()
- \Drupal\helper\Entity\hook_deploy_NAME()
updateFieldPropertyValue
Updates an entity field property to a new value.
public updateFieldPropertyValue(array& $sandbox, string|\Drupal\Core\Entity\Query\QueryInterface $entityTypeIdOrQuery, string $fieldName, string $propertyName, mixed $propertyValue): ?\Drupal\Core\StringTranslation\TranslatableMarkup
Parameters:
| Parameter | Type | Description |
|---|---|---|
$sandbox |
array | |
| The sandbox parameter from the update/deploy hook calling this method. | ||
$entityTypeIdOrQuery |
string|\Drupal\Core\Entity\Query\QueryInterface | |
| The content entity type ID or entity query object for | ||
| \Drupal\helper\EntityType::getQuery(). | ||
$fieldName |
string | |
| The field name. | ||
$propertyName |
string | |
| The field property name. | ||
$propertyValue |
mixed | |
| The field property value to update to. This can also be an array mapping | ||
| from old values to their new values. |
doOne
Apply the callback an entity and save it if the callback makes changes.
protected doOne(\Drupal\Core\Entity\ContentEntityInterface $entity, callable $callback): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$entity |
\Drupal\Core\Entity\ContentEntityInterface | |
| The entity to potentially update. | ||
$callback |
callable | |
| The callback to apply. |
Inherited methods
includeTrashedEntities
Sets a flag to include trashed entities.
public includeTrashedEntities(bool $include = true): $this
Parameters:
| Parameter | Type | Description |
|---|---|---|
$include |
bool | |
| TRUE to include trashed entities, or FALSE to skip them. |
skipTrashedEntities
Skip trashed entities.
public skipTrashedEntities(): $this
executeIgnoringTrash
Executes a callback in the trash 'ignore' context.
protected executeIgnoringTrash(callable $callback): mixed
Entity queries and loads inside the callback will include entities that are in the trash. The previous trash context is restored afterwards. If the Trash module is not installed, or trashed entities are skipped with ::skipTrashedEntities(), the callback is called directly.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$callback |
callable | |
| The callback to execute. |
Return Value:
The return value of the callback.
See Also:
- \Drupal\trash\TrashManagerInterface::executeInTrashContext()