Remove an attachment as preview image of an element, if it is not an image anymore through a change

This commit is contained in:
Jan Böhmer 2023-11-27 22:59:02 +01:00
parent 08bd4d54e3
commit dbff543fa8
2 changed files with 18 additions and 8 deletions

View file

@ -75,7 +75,8 @@ class StructuralEntityChoiceHelper
} }
if ($choice instanceof HasMasterAttachmentInterface) { if ($choice instanceof HasMasterAttachmentInterface) {
$tmp['data-image'] = $choice->getMasterPictureAttachment() instanceof Attachment ? $tmp['data-image'] = ($choice->getMasterPictureAttachment() instanceof Attachment
&& $choice->getMasterPictureAttachment()->isPicture()) ?
$this->attachmentURLGenerator->getThumbnailURL($choice->getMasterPictureAttachment(), $this->attachmentURLGenerator->getThumbnailURL($choice->getMasterPictureAttachment(),
'thumbnail_xs') 'thumbnail_xs')
: null : null

View file

@ -204,13 +204,22 @@ class AttachmentSubmitHandler
//Rename blacklisted (unsecure) files to a better extension //Rename blacklisted (unsecure) files to a better extension
$this->renameBlacklistedExtensions($attachment); $this->renameBlacklistedExtensions($attachment);
//Check if we should assign this attachment to master picture //Set / Unset the master picture attachment / preview image
//this is only possible if the attachment is new (not yet persisted to DB)
if ($options['become_preview_if_empty'] && null === $attachment->getID() && $attachment->isPicture()) {
$element = $attachment->getElement(); $element = $attachment->getElement();
if ($element instanceof AttachmentContainingDBElement && !$element->getMasterPictureAttachment() instanceof Attachment) { if ($element instanceof AttachmentContainingDBElement) {
//Make this attachment the master picture if needed and this was requested
if ($options['become_preview_if_empty']
&& $element->getMasterPictureAttachment() === null //Element must not have an preview image yet
&& null === $attachment->getID() //Attachment must be null
&& $attachment->isPicture() //Attachment must be a picture
) {
$element->setMasterPictureAttachment($attachment); $element->setMasterPictureAttachment($attachment);
} }
//If this attachment is the master picture, but is not a picture anymore, dont use it as master picture anymore
if ($element->getMasterPictureAttachment() === $attachment && !$attachment->isPicture()) {
$element->setMasterPictureAttachment(null);
}
} }
return $attachment; return $attachment;