You are here

function background_image_update_8401 in Background Image 2.x

Same name and namespace in other branches
  1. 8 background_image.install \background_image_update_8401()
  2. 2.0.x background_image.install \background_image_update_8401()

Ensure all entity targets are stored using UUIDs.

File

./background_image.install, line 13
Background Image module install/updates.

Code

function background_image_update_8401() {

  // @see https://www.drupal.org/project/background_image/issues/3032110
  $entityTypeManager = \Drupal::entityTypeManager();
  $dataTable = $tableMapping = $entityTypeManager
    ->getDefinition('background_image')
    ->getDataTable();
  $results = \Drupal::database()
    ->select($dataTable, 'data')
    ->fields('data', [
    'bid',
    'target',
  ])
    ->condition('type', BackgroundImageInterface::TYPE_ENTITY)
    ->execute()
    ->fetchAllKeyed();
  foreach ($results as $bid => $target) {
    try {
      list($entityTypeId, $entityId) = explode(':', "{$target}:");

      // Skip invalid records.
      if (!$entityTypeId || !$entityId || !is_numeric($entityId)) {
        continue;
      }
      $uuid = $entityTypeManager
        ->getStorage($entityTypeId)
        ->load($entityId)
        ->uuid();
      \Drupal::database()
        ->update($dataTable)
        ->condition('bid', $bid)
        ->fields([
        'target' => "{$entityTypeId}:{$uuid}",
      ])
        ->execute();
    } catch (\Exception $exception) {

      // Intentionally do nothing. Meant to be a passive/non-destructive update.
    }
  }
}