Problem/Motivation
I'm using focal_point in conjunction with Drupal Core 9.5.0 media and PHP 8.1.12. I recently installed the modules media_thumbnails and media_thumbnails_pdf which auto generate a thumbnail for PDF files using imagemagick. The thumbnail generates when I save a media item but I am greeted by a WSOD with the "The website encountered an unexpected error. Please try again later." message. In the log messages I see:
TypeError: Drupal\focal_point\FocalPointManager::saveCropEntity(): Argument #3 ($width) must be of type int, null given, called in /app/web/modules/contrib/focal_point/focal_point.module on line 99 in Drupal\focal_point\FocalPointManager->saveCropEntity() (line 94 of /app/web/modules/contrib/focal_point/src/FocalPointManager.php)
Full trace:
TypeError: Drupal\focal_point\FocalPointManager::saveCropEntity(): Argument #3 ($width) must be of type int, null given, called in /app/web/modules/contrib/focal_point/focal_point.module on line 99 in Drupal\focal_point\FocalPointManager->saveCropEntity() (line 94 of /app/web/modules/contrib/focal_point/src/FocalPointManager.php)
#0 /app/web/modules/contrib/focal_point/focal_point.module(99): Drupal\focal_point\FocalPointManager->saveCropEntity(50.0, 50.0, NULL, NULL, Object(Drupal\crop\Entity\Crop))
#1 [internal function]: focal_point_entity_update(Object(Drupal\media\Entity\Media))
#2 /app/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(426): call_user_func_array(Object(Closure), Array)
#3 /app/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(405): Drupal\Core\Extension\ModuleHandler->Drupal\Core\Extension\{closure}(Object(Closure), 'focal_point')
#4 /app/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(433): Drupal\Core\Extension\ModuleHandler->invokeAllWith('entity_update', Object(Closure))
#5 /app/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(251): Drupal\Core\Extension\ModuleHandler->invokeAll('entity_update', Array)
#6 /app/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(903): Drupal\Core\Entity\EntityStorageBase->invokeHook('update', Object(Drupal\media\Entity\Media))
#7 /app/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(598): Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('update', Object(Drupal\media\Entity\Media))
#8 /app/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(784): Drupal\Core\Entity\EntityStorageBase->doPostSave(Object(Drupal\media\Entity\Media), true)
#9 /app/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(523): Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object(Drupal\media\Entity\Media), true)
#10 /app/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(804): Drupal\Core\Entity\EntityStorageBase->save(Object(Drupal\media\Entity\Media))
#11 /app/web/core/modules/media/src/MediaStorage.php(29): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object(Drupal\media\Entity\Media))
#12 /app/web/core/lib/Drupal/Core/Entity/EntityBase.php(339): Drupal\media\MediaStorage->save(Object(Drupal\media\Entity\Media))
#13 /app/web/core/lib/Drupal/Core/Entity/EntityForm.php(285): Drupal\Core\Entity\EntityBase->save()
#14 /app/web/core/modules/media/src/MediaForm.php(61): Drupal\Core\Entity\EntityForm->save(Array, Object(Drupal\Core\Form\FormState))
#15 [internal function]: Drupal\media\MediaForm->save(Array, Object(Drupal\Core\Form\FormState))
#16 /app/web/core/lib/Drupal/Core/Form/FormSubmitter.php(114): call_user_func_array(Array, Array)
#17 /app/web/core/lib/Drupal/Core/Form/FormSubmitter.php(52): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState))
#18 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(595): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState))
#19 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(323): Drupal\Core\Form\FormBuilder->processForm('media_svg_edit_...', Array, Object(Drupal\Core\Form\FormState))
#20 /app/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\media\MediaForm), Object(Drupal\Core\Form\FormState))
#21 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#22 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#23 /app/web/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#24 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#25 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#26 /app/vendor/symfony/http-kernel/HttpKernel.php(169): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#27 /app/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#28 /app/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#29 /app/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#30 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#31 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#32 /app/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#33 /app/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#34 /app/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#35 /app/web/core/lib/Drupal/Core/DrupalKernel.php(707): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#36 /app/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#37 {main}.
Steps to reproduce
Install D9.5.
Install focal_point
Upload PDF to Drupal media library.
Install media_thumbnails and media_thumbnails_pdf modules
Go to media library and edit and save the PDF media item
Observe WSOD and error message in logs
Go to media library and see thumbnail indeed generated
Go to /app/web/modules/contrib/focal_point/focal_point.module on line 99 and kint or ksm or dump the $item.
There is no width or height properties because $item is a media item and does not have those properties.
Proposed resolution
Unsure. To be clear though I am not attempting to use focal_point in the generation of the thumbnail so I'm unclear as to why focal_point is even firing?
Remaining tasks
Unsure.
User interface changes
None.
API changes
Unsure.
Data model changes
Unsure.
If I can provide anything further that may be helpful please let me know.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | 3328807-focal_point-image_width_when_no_image_to_crop.patch | 546 bytes | maxwellkeeble |
Issue fork focal_point-3328807
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
grcwolfI had the same error and did some debugging.
In the end, the problem was that the file public://media-icons/generic/generic.png did not exist, and therefore it had no width.
(This directory should also contain audio.png, no-thumbnail.png, video.png)
I simply copied those files from another instance and the error disappeared.
However, I have no idea why I was suddenly missing those files.
Check if those files exist. If not, try adding them. This might also solve the issue for you.
The error is caused because media can't calculate the width of a file that doesn't exist.
So it isn't really the fault of focal point.
But maybe focal point should add a check to make sure the width and height are set. If it could simply through an exception.
This would be better in my opinion as you can catch an exception and handle it, instead of having an uncatchable type error which "breaks" everything.
Comment #3
maxwellkeeble commentedI had the same issue with a PDF media item. The generic.png did not appear to be why, as those files exist. Here is a patch that at least prevents WSOD.
I agree an exception should be managed by Focal Point - if $image doesn't have width it shouldn't be trying to crop it, or should at least fail to crop gracefully.
Comment #4
adamzimmermann commentedI just ran into this same issue.
I followed the steps here, and I also related the two issues.
https://www.drupal.org/project/drupal/issues/3060509#comment-14196948
While copying the files solves this, I also like the idea of preventing/handling the error better too as proposed in #3 above.
Comment #5
jcnventuraComment #6
jcnventuraI've tested this code on a site running PHP 8.1, that was throwing the error, and after applying the patch in #3, the exception is no longer thrown.
Comment #7
lindsay.wils commentedI had the same error, but in a different situation.
I have a custom media type that I use for handling SVG images. When using a Media reference field and uploading an SVG through the media library modal, upon pressing 'Save and insert' or 'Save and select' this same error was being thrown. I am not getting WSOD, just a JS error, which stops the media item from being attached to the referenced field, but it is actually created, so simple closing, opening again and selected the newly created item works.
Seemingly the focal point module is trying to apply some default settings to the SVG image, but failing as it cant find a width parameter. Just like the original poster, I do not have the 'Image (Focal Point)' widget selected in my media types form display mode, just the standard Image widget type, so not sure why Focal Point is trying to do anything here.
This patch also fixes the error for me, thank you.
Comment #10
mandclu commentedLike @lindsay.wils I ran into this issue while working with an SVG file. I found my own, slightly different fix before finding this issue: instead of passing zeroes into FocalPointManager::saveCropEntity() if item width and/or height are null, I modified the enclosing check for a focal point value to also check for values in the height and width. I personally don't see the value in attempting to apply a focal point to an image that is zero or null in either dimension.
I also tested the patch in #3 and it did also resolve the fatal errors I was seeing, so I'm not reverted the status to needs review, as any fix at this point would be better than none at all. That said, if anyone in this thread is inclined to test the merge request I've created, it would be appreciated.
Comment #11
tvb commentedI tested merge request !13 and it resolved the issue.
Comment #12
pcate commented+1 RTBC. Ran into this issue when uploading an SVG file like @mandclu. Applying merge request !13 solved the issue. Did not test patch #3.
Comment #13
hudriI've tested MR !13 sucessfully with svg_image_field module.
Without this patch I was not able to save media entities because it tried to generate a thumbnail image style. (I guess this is because svg_image_field does return a the SVG uri itself to display previews in the media library widgets, but those thumbnails don't seem to use the field widget or field formatter settings).
Comment #14
heddn+1 on the fix here.
Comment #15
loopy1492 commentedCan also confirm this fixed it for us. Drupal Core 9.5.5, PHP 8.1, Focal Point 2.0.0-alpha2.
Comment #16
rajeshreeputracan we have test for the same
Comment #17
anybodyTest might include deleting a file from the file system, which still exists in the database.
Another test might be to upload a file type that can not be handled by this module, like an SVG (.svg) but is allowed for image fields.
Comment #19
rajeshreeputraThank you for working on this, merged release will be followed shortly.
Comment #20
rajeshreeputraComment #21
Sana.Neyazi commentedWorking fine with Php8.1 and drupal core 9.5.7
Comment #22
rajeshreeputra