You are here

public function PhotosUpload::saveExistingMedia in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 src/PhotosUpload.php \Drupal\photos\PhotosUpload::saveExistingMedia()

Attach media to PhotosImage entity.

Parameters

int $mediaId: The media entity_id.

int $albumId: The album entity_id.

Return value

bool TRUE if media saved successfully.

Overrides PhotosUploadInterface::saveExistingMedia

File

src/PhotosUpload.php, line 394

Class

PhotosUpload
Functions to help with uploading images to albums.

Namespace

Drupal\photos

Code

public function saveExistingMedia($mediaId, $albumId) {
  $config = $this->configFactory
    ->get('photos.settings');

  /** @var \Drupal\media\MediaInterface $mediaItem */
  $mediaItem = NULL;
  try {
    $mediaItem = $this->entityTypeManager
      ->getStorage('media')
      ->load($mediaId);
  } catch (InvalidPluginDefinitionException $e) {
  } catch (PluginNotFoundException $e) {
  }
  if ($mediaItem) {
    $defaultWeight = $this->connection
      ->select('photos_image_field_data', 'i')
      ->fields('i', [
      'weight',
    ])
      ->condition('i.album_id', $albumId)
      ->orderBy('i.weight', 'DESC')
      ->execute()
      ->fetchField();
    $newPhotosImageEntity = [
      'album_id' => $albumId,
      'title' => $mediaItem
        ->getName(),
      'weight' => $defaultWeight + 1,
    ];

    // Check default media field.
    $uploadField = $config
      ->get('multi_upload_default_field');
    $uploadFieldParts = explode(':', $uploadField);
    $field = isset($uploadFieldParts[0]) ? $uploadFieldParts[0] : 'field_image';
    $allBundleFields = $this->entityFieldManager
      ->getFieldDefinitions('photos_image', 'photos_image');
    if (isset($allBundleFields[$field])) {
      $fieldType = $allBundleFields[$field]
        ->getType();
      if ($fieldType == 'entity_reference') {
        $mediaField = isset($uploadFieldParts[1]) ? $uploadFieldParts[1] : '';
        $mediaBundle = isset($uploadFieldParts[2]) ? $uploadFieldParts[2] : '';
        if ($mediaField && $mediaBundle) {

          // Set photos_image media reference field.
          $newPhotosImageEntity[$field] = [
            'target_id' => $mediaId,
          ];
        }

        // Save PhotosImageFile entity.
        $photosImage = PhotosImage::create($newPhotosImageEntity);
        try {
          $photosImage
            ->save();
          if ($photosImage && $photosImage
            ->id()) {
            if ($config
              ->get('photos_user_count_cron')) {
              $user = $this->currentUser;
              PhotosAlbum::setCount('user_image', $photosImage
                ->getOwnerId() ? $photosImage
                ->getOwnerId() : $user
                ->id());
              PhotosAlbum::setCount('node_album', $albumId);
            }
            return TRUE;
          }
        } catch (EntityStorageException $e) {
          watchdog_exception('photos', $e);
        }
      }
    }
  }
  return FALSE;
}