public function PhotosUpload::saveImage in Album Photos 8.5
Same name and namespace in other branches
- 8.4 src/PhotosUpload.php \Drupal\photos\PhotosUpload::saveImage()
- 6.0.x src/PhotosUpload.php \Drupal\photos\PhotosUpload::saveImage()
Attach image file to PhotosImage entity.
Parameters
\Drupal\file\Entity\File $file: The file entity.
Return value
bool TRUE if image saved successfully.
Overrides PhotosUploadInterface::saveImage
1 call to PhotosUpload::saveImage()
- PhotosUpload::unzip in src/
PhotosUpload.php - Unzip archive of image files.
File
- src/
PhotosUpload.php, line 159
Class
- PhotosUpload
- Functions to help with uploading images to albums.
Namespace
Drupal\photosCode
public function saveImage(File $file) {
// @todo maybe pass file object and array of other vars.
if ($file
->id() && isset($file->album_id)) {
$fid = $file
->id();
$album_id = $file->album_id;
// Prep image title.
if (isset($file->title) && !empty($file->title)) {
$title = $file->title;
}
else {
// Cleanup filename and use as title.
$title = $this
->cleanTitle($file
->getFilename());
}
// Create photos_image entity.
/* @var \Drupal\Core\Image\Image $image */
$image = \Drupal::service('image.factory')
->get($file
->getFileUri());
if ($image
->isValid()) {
$newPhotosImageEntity = [
'album_id' => $file->album_id,
'title' => $title,
'weight' => isset($file->weight) ? $file->weight : 0,
'description' => isset($file->des) ? $file->des : '',
];
// Check if photos_image has default field_image.
$uploadField = \Drupal::config('photos.settings')
->get('multi_upload_default_field');
$uploadFieldParts = explode(':', $uploadField);
$field = isset($uploadFieldParts[0]) ? $uploadFieldParts[0] : 'field_image';
$allBundleFields = \Drupal::service('entity_field.manager')
->getFieldDefinitions('photos_image', 'photos_image');
if (isset($allBundleFields[$field])) {
$fieldType = $allBundleFields[$field]
->getType();
if ($fieldType == 'image') {
$newPhotosImageEntity[$field] = [
'target_id' => $fid,
'alt' => $title,
'title' => $title,
'width' => $image
->getWidth(),
'height' => $image
->getHeight(),
];
}
else {
// Check media fields.
if ($fieldType == 'entity_reference') {
$mediaField = isset($uploadFieldParts[1]) ? $uploadFieldParts[1] : '';
$mediaBundle = isset($uploadFieldParts[2]) ? $uploadFieldParts[2] : '';
if ($mediaField && $mediaBundle) {
// Create new media entity.
$values = [
'bundle' => $mediaBundle,
'uid' => \Drupal::currentUser()
->id(),
];
$values[$mediaField] = [
'target_id' => $file
->id(),
];
$media = Media::create($values);
// @todo media name?
$media
->setName('Photo ' . $file
->id())
->setPublished(TRUE)
->save();
// Set photos_image media reference field.
$newPhotosImageEntity[$field] = [
'target_id' => $media
->id(),
];
}
}
}
}
$photosImage = PhotosImage::create($newPhotosImageEntity);
try {
$photosImage
->save();
if ($photosImage && $photosImage
->id()) {
if (\Drupal::config('photos.settings')
->get('photos_user_count_cron')) {
$user = \Drupal::currentUser();
PhotosAlbum::setCount('user_image', $photosImage
->getOwnerId() ? $photosImage
->getOwnerId() : $user
->id());
PhotosAlbum::setCount('node_album', $file->album_id);
}
// Save file and add file usage.
$file_usage = \Drupal::service('file.usage');
$file_usage
->add($file, 'photos', 'node', $album_id);
// Check admin setting for maximum image resolution.
if ($photos_size_max = \Drupal::config('photos.settings')
->get('photos_size_max')) {
// Will scale image if needed.
file_validate_image_resolution($file, $photos_size_max);
}
return TRUE;
}
} catch (EntityStorageException $e) {
watchdog_exception('photos', $e);
}
}
}
return FALSE;
}