You are here

background_image.install in Background Image 2.0.x

Same filename and directory in other branches
  1. 8 background_image.install
  2. 2.x background_image.install

Background Image module install/updates.

File

background_image.install
View source
<?php

/**
 * @file
 * Background Image module install/updates.
 */
use Drupal\background_image\BackgroundImageInterface;

/**
 * Ensure all entity targets are stored using UUIDs.
 */
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.
    }
  }
}

Functions

Namesort descending Description
background_image_update_8401 Ensure all entity targets are stored using UUIDs.