Programmatic Photo Creation

Created on 30 January 2023, over 1 year ago
Updated 21 April 2023, about 1 year ago

I have a fairly large number of photos, in a bunch of albums, that I'd like to load into this system. Rather than do this manually, and lose a lot of time and data, I'd like to do it with a program.

I'm quite comfortable with the programming, with several custom modules done, and it would save me a TON of time if you can point me to the function I need to invoke to create the PhotosImage with the image file.

I tried Drupal\photos\Entity\PhotosImage::create();, but that did not immediately show me how to associate a file (or file id) with the entity.

Thank you.

πŸ’¬ Support request
Status

Fixed

Version

6.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States Cayenne

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Cayenne
  • πŸ‡ΊπŸ‡ΈUnited States Nathaniel

    Here's a snippet from the migration code. I think you are looking for 'field_image' 'target_id' - this is the file ID (fid).

    The 'pid' or 'album_id' is the album node ID (nid).

    <?php
              // Create new photos_image entity.
              $photosImage = $this->entityTypeManager->getStorage('photos_image')->create([
                'uid' => $row->getDestinationProperty('uid'),
                '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(),
                ],
              ]);
    ?>
  • πŸ‡ΊπŸ‡ΈUnited States Nathaniel

    Marking as fixed. Comment here or open a new issue if needed.

  • Status changed to Fixed about 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024