You are here

photos.post_update.php in Album Photos 8.5

Same filename and directory in other branches
  1. 6.0.x photos.post_update.php

Post update functions for Photos module.

File

photos.post_update.php
View source
<?php

/**
 * @file
 * Post update functions for Photos module.
 */
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityStorageException;

/**
 * Migrate images to new photos_image entity.
 */
function photos_post_update_migrate_photos_image_entity_1(&$sandbox = NULL) {

  // Check if temporary table was created.
  if (\Drupal::database()
    ->schema()
    ->tableExists('photos_image_tmp')) {
    if (!isset($sandbox['steps'])) {
      $sandbox['limit'] = 25;
      $sandbox['current_step'] = 0;
      $sandbox['current_fid'] = 0;

      // Count query.
      $query = \Drupal::database()
        ->select('photos_image_tmp', 'i')
        ->fields('i', [
        'fid',
      ]);
      $num_rows = $query
        ->countQuery()
        ->execute()
        ->fetchField();
      $sandbox['steps'] = $num_rows;
    }
    $results = \Drupal::database()
      ->select('photos_image_tmp', 'i')
      ->fields('i')
      ->condition('i.fid', $sandbox['current_fid'], '>')
      ->range(0, $sandbox['limit'])
      ->orderBy('i.fid', 'ASC')
      ->execute();
    foreach ($results as $result) {
      $fid = $result->fid;
      $sandbox['current_step']++;
      $sandbox['current_fid'] = $fid;
      try {
        $title = $result->title;

        /* @var \Drupal\file\Entity\File $file */
        $file = \Drupal::entityTypeManager()
          ->getStorage('file')
          ->load($fid);
        $image = \Drupal::service('image.factory')
          ->get($file
          ->getFileUri());
        if ($image) {
          if (empty($title)) {

            // @note known issue: title can not be null.
            $title = \Drupal::service('photos.upload')
              ->cleanTitle($file
              ->getFilename());
          }
          try {

            // Create new photos_image entity.
            $photosImage = \Drupal::entityTypeManager()
              ->getStorage('photos_image')
              ->create([
              'album_id' => $result->pid,
              'title' => $title,
              'weight' => $result->wid,
              'description' => $result->des,
              'field_image' => [
                'target_id' => $fid,
                'alt' => $title,
                'title' => $title,
                'width' => $image
                  ->getWidth(),
                'height' => $image
                  ->getHeight(),
              ],
            ]);
            try {
              $photosImage
                ->save();
              if ($photosImage) {
                try {

                  // Move image views to the {photos_count} table.
                  \Drupal::database()
                    ->insert('photos_count')
                    ->fields([
                    'cid' => $photosImage
                      ->id(),
                    'changed' => \Drupal::time()
                      ->getRequestTime(),
                    'type' => 'image_views',
                    'value' => $result->count,
                  ])
                    ->execute();
                } catch (\Exception $e) {
                  watchdog_exception('photos', $e);
                }
              }
            } catch (EntityStorageException $e) {
              watchdog_exception('photos', $e);
            }
          } catch (InvalidPluginDefinitionException $e) {
            watchdog_exception('photos', $e);
          } catch (PluginNotFoundException $e) {
            watchdog_exception('photos', $e);
          }
        }
      } catch (InvalidPluginDefinitionException $e) {
        watchdog_exception('photos', $e);
      } catch (PluginNotFoundException $e) {
        watchdog_exception('photos', $e);
      }
    }
    $sandbox['#finished'] = $sandbox['current_step'] / $sandbox['steps'];
    if ($sandbox['#finished'] >= 1) {
      return t('Updated to use the new photos_image entity.');
    }
  }
  return NULL;
}

/**
 * Update {photos_album}.cover_id to use new photos_image entity id.
 */
function photos_post_update_migrate_photos_image_entity_2(&$sandbox = NULL) {

  // Check if temporary table was created.
  if (\Drupal::database()
    ->schema()
    ->tableExists('photos_image_tmp')) {
    if (!isset($sandbox['steps'])) {
      $sandbox['limit'] = 50;
      $sandbox['current_step'] = 0;
      $sandbox['current_id'] = 0;

      // Count query.
      $query = \Drupal::database()
        ->select('photos_album', 'a')
        ->fields('a', [
        'cover_id',
      ])
        ->condition('a.cover_id', 0, '!=');
      $num_rows = $query
        ->countQuery()
        ->execute()
        ->fetchField();
      $sandbox['steps'] = $num_rows;
      if ($sandbox['steps'] <= 1) {

        // Drop the temporary table.
        \Drupal::database()
          ->schema()
          ->dropTable('photos_image_tmp');
        $sandbox['#finished'] = 1;
        return t('No albums need to be updated.');
      }
    }
    $results = \Drupal::database()
      ->select('photos_album', 'a')
      ->fields('a')
      ->condition('a.album_id', $sandbox['current_id'], '>')
      ->condition('a.cover_id', 0, '!=')
      ->orderBy('a.album_id', 'ASC')
      ->range(0, $sandbox['limit'])
      ->execute();
    foreach ($results as $result) {
      $sandbox['current_step']++;
      $sandbox['current_id'] = $result->album_id;
      $photosImageIds = \Drupal::entityQuery('photos_image')
        ->condition('field_image.target_id', $result->cover_id)
        ->execute();
      $cover_id = 0;
      if (!empty($photosImageIds)) {
        $cover_id = reset($photosImageIds);
      }
      \Drupal::database()
        ->update('photos_album')
        ->fields([
        'cover_id' => $cover_id,
      ])
        ->condition('album_id', $result->album_id)
        ->execute();
    }
    $sandbox['#finished'] = $sandbox['current_step'] / $sandbox['steps'];
    if ($sandbox['#finished'] >= 1) {

      // Drop the temporary table.
      \Drupal::database()
        ->schema()
        ->dropTable('photos_image_tmp');
      return t('Updated album cover id to use photos_image entity id.');
    }
  }
  return NULL;
}

Functions

Namesort descending Description
photos_post_update_migrate_photos_image_entity_1 Migrate images to new photos_image entity.
photos_post_update_migrate_photos_image_entity_2 Update {photos_album}.cover_id to use new photos_image entity id.