You are here

public function PhotosImage::import in Album Photos 8.5

Same name and namespace in other branches
  1. 8.4 src/Plugin/migrate/destination/PhotosImage.php \Drupal\photos\Plugin\migrate\destination\PhotosImage::import()
  2. 6.0.x src/Plugin/migrate/destination/PhotosImage.php \Drupal\photos\Plugin\migrate\destination\PhotosImage::import()

Import the row.

Derived classes must implement import(), to construct one new object (pre-populated) using ID mappings in the Migration.

Parameters

\Drupal\migrate\Row $row: The row object.

array $old_destination_id_values: (optional) The old destination IDs. Defaults to an empty array.

Return value

array|bool An indexed array of destination IDs in the same order as defined in the plugin's getIds() method if the plugin wants to save the IDs to the ID map, TRUE to indicate success without saving IDs to the ID map, or FALSE to indicate a failure.

Overrides MigrateDestinationInterface::import

File

src/Plugin/migrate/destination/PhotosImage.php, line 115

Class

PhotosImage
Photos image migration destination.

Namespace

Drupal\photos\Plugin\migrate\destination

Code

public function import(Row $row, array $old_destination_id_values = []) {
  $title = $row
    ->getDestinationProperty('title');
  $fid = $row
    ->getDestinationProperty('fid');
  try {

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

        // @note known issue: title can not be null.
        $title = $this->photosUpload
          ->cleanTitle($file
          ->getFilename());
      }
      try {

        // Create new photos_image entity.
        $photosImage = $this->entityTypeManager
          ->getStorage('photos_image')
          ->create([
          'album_id' => $row
            ->getDestinationProperty('pid'),
          'title' => $title,
          'weight' => $row
            ->getDestinationProperty('wid'),
          'description' => $row
            ->getDestinationProperty('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.
              $this->connection
                ->insert('photos_count')
                ->fields([
                'cid' => $photosImage
                  ->id(),
                'changed' => $this->time
                  ->getRequestTime(),
                'type' => 'image_views',
                'value' => $row
                  ->getDestinationProperty('count'),
              ])
                ->execute();
            } catch (\Exception $e) {
              watchdog_exception('photos', $e);
            }

            // Successfully created new photos_image entity.
            return [
              $photosImage
                ->id(),
            ];
          }
        } 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);
  }

  // Something was missing.
  return [];
}