Skip to content

Drupal\helper\Entity\ContentEntityUpdater

A utility class to make updating content entities simple.

Use this in a post update function like so:

Implements:

Drupal\Core\DependencyInjection\ContainerInjectionInterface

Methods

Name Description
__construct ContentEntityUpdater constructor.
create {@inheritdoc}
setLogger Sets a logger.
update Updates content entities as part of a Drupal update.
updateFieldPropertyValue Updates an entity field property to a new value.

ContentEntityUpdater::__construct

Description

public __construct (\Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager, \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager, \Psr\Log\LoggerInterface $logger, int $batchSize)

ContentEntityUpdater constructor.

Parameters

  • (\Drupal\Core\Entity\EntityTypeManagerInterface) $entityTypeManager :
    The entity type manager.
  • (\Drupal\Core\Entity\EntityFieldManagerInterface) $entityFieldManager :
    The entity field manager.
  • (\Psr\Log\LoggerInterface) $logger :
    The logger.
  • (int) $batchSize :
    The number of entities to process in each batch.

Return Values

void


ContentEntityUpdater::create

Description

public static create (void)

{@inheritdoc}

Parameters

This function has no parameters.

Return Values

void


ContentEntityUpdater::setLogger

Description

public setLogger (void)

Sets a logger.

Parameters

This function has no parameters.

Return Values

void


ContentEntityUpdater::update

Description

public update (array $sandbox, string|\Drupal\Core\Entity\Query\QueryInterface $entityTypeIdOrQuery, callable $callback, bool $continue_on_error)

Updates content entities as part of a Drupal update.

This is best used as with hook_post_update_NAME() or hook_deploy_NAME().

Parameters

  • (array) $sandbox :
    Stores information for batch updates.
  • (string|\Drupal\Core\Entity\Query\QueryInterface) $entityTypeIdOrQuery :
    The content entity type ID or entity query object for ::getEntityQuery().
  • (callable) $callback :
    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.
  • (bool) $continue_on_error :
    Set to TRUE to continue updating if an error has occurred.

Return Values

\Drupal\Core\StringTranslation\TranslatableMarkup|null

An error message if $continue_on_error is set to TRUE and an error has
occurred.

Throws Exceptions

\RuntimeException

Thrown when used twice in the same update function for different entity
types. This method should only be called once per update function.


ContentEntityUpdater::updateFieldPropertyValue

Description

public updateFieldPropertyValue (array $sandbox, string|\Drupal\Core\Entity\Query\QueryInterface $entityTypeIdOrQuery, string $fieldName, string $propertyName, mixed $propertyValue)

Updates an entity field property to a new value.

Parameters

  • (array) $sandbox :
    The sandbox parameter from the update/deploy hook calling this method.
  • (string|\Drupal\Core\Entity\Query\QueryInterface) $entityTypeIdOrQuery :
    The entity type ID or entity query object for ::getEntityQuery().
  • (string) $fieldName :
    The field name.
  • (string) $propertyName :
    The field property name.
  • (mixed) $propertyValue :
    The field property value to update to. This can also be an array mapping
    from old values to their new values.

Return Values

void