craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 635 forks source link

[5.x]: asset.render() gives Error: Call to a member function getHandle() on null #15426

Closed JeroenJRP closed 3 months ago

JeroenJRP commented 3 months ago

What happened?

Description

When tying to call the render() function on an Asset element, an error is thrown

Steps to reproduce

  1. Query an Asset from twig
  2. Call .render() on the query or resulting element

Expected behavior

Asset is rendered using the corosponding partial

Actual behavior

The error "Call to a member function getHandle() on null" is thrown. This is because the provider for the Asset's FieldLayout is null

Craft CMS version

5.2.8 (upgraded from 4.10.6)

PHP version

8.3

Operating system and version

No response

Database type and version

MariaDB 10.6.18

Image driver and version

No response

Installed plugins and versions

-

brandonkelly commented 3 months ago

Search for that error in storage/logs. What’s the stack trace that follows it?

JeroenJRP commented 3 months ago

Thanks for looking into this, here is the stack trace:

2024-07-29 10:35:09 [web.ERROR] [Error] Error: Call to a member function getHandle() on null in /var/www/html/vendor/craftcms/cms/src/helpers/ElementHelper.php:958
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/base/Element.php(6135): craft\helpers\ElementHelper::renderElements(Array, Array)
#1 /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php(1635): craft\base\Element->render(Array)
#2 /var/www/html/vendor/craftcms/cms/src/helpers/Template.php(148): twig_get_attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\elements\Asset), 'render', Array, 'method', false, false, false, 5)
#3 /var/www/html/storage/runtime/compiled_templates/41/41185e982ea65f2e98bc0c1741030420.php(48): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\elements\Asset), 'render', Array, 'method', false, false, false, 5)
#4 /var/www/html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_ed567827fad49bed744892ad0759d46f->doDisplay(Array, Array)
#5 /var/www/html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#6 /var/www/html/vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#7 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(38): Twig\Template->render(Array)
#8 /var/www/html/vendor/twig/twig/src/Environment.php(280): Twig\TemplateWrapper->render(Array)
#9 /var/www/html/vendor/craftcms/cms/src/web/View.php(494): Twig\Environment->render('_blocks/entry/i...', Array)
#10 /var/www/html/vendor/craftcms/cms/src/helpers/ElementHelper.php(965): craft\web\View->renderTemplate('_blocks/entry/i...', Array, 'site')
#11 /var/www/html/vendor/craftcms/cms/src/base/Element.php(6135): craft\helpers\ElementHelper::renderElements(Array, Array)
#12 /var/www/html/vendor/craftcms/ckeditor/src/data/Entry.php(37): craft\base\Element->render()
#13 /var/www/html/vendor/craftcms/ckeditor/src/data/FieldData.php(175): craft\ckeditor\data\Entry->getHtml()
#14 [internal function]: craft\ckeditor\data\FieldData->craft\ckeditor\data\{closure}(Object(craft\ckeditor\data\Entry), 1)
#15 /var/www/html/vendor/illuminate/collections/Arr.php(600): array_map(Object(Closure), Array, Array)
#16 /var/www/html/vendor/illuminate/collections/Collection.php(778): Illuminate\Support\Arr::map(Array, Object(Closure))
#17 /var/www/html/vendor/craftcms/ckeditor/src/data/FieldData.php(175): Illuminate\Support\Collection->map(Object(Closure))
#18 /var/www/html/vendor/craftcms/ckeditor/src/data/FieldData.php(49): craft\ckeditor\data\FieldData->render()
#19 /var/www/html/storage/runtime/compiled_templates/c7/c7e28e116e29984ea022288af71382a1.php(44): craft\ckeditor\data\FieldData->__toString()
#20 /var/www/html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_9b93125c18d66ad81f588c66c7b58180->doDisplay(Array, Array)
#21 /var/www/html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#22 /var/www/html/vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#23 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(38): Twig\Template->render(Array)
#24 /var/www/html/vendor/twig/twig/src/Environment.php(280): Twig\TemplateWrapper->render(Array)
#25 /var/www/html/vendor/craftcms/cms/src/web/View.php(494): Twig\Environment->render('_blocks/entry/t...', Array)
#26 /var/www/html/vendor/craftcms/cms/src/helpers/ElementHelper.php(965): craft\web\View->renderTemplate('_blocks/entry/t...', Array, 'site')
#27 /var/www/html/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1949): craft\helpers\ElementHelper::renderElements(Array, Array)
#28 /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php(1635): craft\elements\db\ElementQuery->render()
#29 /var/www/html/vendor/craftcms/cms/src/helpers/Template.php(148): twig_get_attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\elements\db\EntryQuery), 'render', Array, 'method', false, false, false, 7)
#30 /var/www/html/storage/runtime/compiled_templates/02/02be08eba877ecd61ccfe7527b7a6938.php(59): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\elements\db\EntryQuery), 'render', Array, 'method', false, false, false, 7)
#31 /var/www/html/vendor/twig/twig/src/Template.php(171): __TwigTemplate_069fc94aad57178a5e3434004584c3ef->block_content(Array, Array)
#32 /var/www/html/storage/runtime/compiled_templates/3e/3e40b9b537d42b52c51f886bcba6eaa8.php(125): Twig\Template->displayBlock('content', Array, Array)
#33 /var/www/html/vendor/twig/twig/src/Template.php(171): __TwigTemplate_9eaa6320ba0f124a7cb22103b4eecf05->block_htmlPage(Array, Array)
#34 /var/www/html/storage/runtime/compiled_templates/65/6596ec5d963fd130cbb7290dc32d80b8.php(139): Twig\Template->displayBlock('htmlPage', Array, Array)
#35 /var/www/html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_fd1f213bfd9542c66fbdff95870b8686->doDisplay(Array, Array)
#36 /var/www/html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#37 /var/www/html/storage/runtime/compiled_templates/3e/3e40b9b537d42b52c51f886bcba6eaa8.php(44): Twig\Template->display(Array, Array)
#38 /var/www/html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_9eaa6320ba0f124a7cb22103b4eecf05->doDisplay(Array, Array)
#39 /var/www/html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#40 /var/www/html/storage/runtime/compiled_templates/02/02be08eba877ecd61ccfe7527b7a6938.php(43): Twig\Template->display(Array, Array)
#41 /var/www/html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_069fc94aad57178a5e3434004584c3ef->doDisplay(Array, Array)
#42 /var/www/html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#43 /var/www/html/vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#44 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(38): Twig\Template->render(Array)
#45 /var/www/html/vendor/twig/twig/src/Environment.php(280): Twig\TemplateWrapper->render(Array)
#46 /var/www/html/vendor/craftcms/cms/src/web/View.php(494): Twig\Environment->render('index.twig', Array)
#47 /var/www/html/vendor/craftcms/cms/src/web/View.php(547): craft\web\View->renderTemplate('index.twig', Array)
#48 /var/www/html/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(57): craft\web\View->renderPageTemplate('index.twig', Array, 'site')
#49 /var/www/html/vendor/yiisoft/yii2/web/Response.php(1109): craft\web\TemplateResponseFormatter->format(Object(craft\web\Response))
#50 /var/www/html/vendor/craftcms/cms/src/web/Response.php(338): yii\web\Response->prepare()
#51 /var/www/html/vendor/yiisoft/yii2/web/Response.php(340): craft\web\Response->prepare()
#52 /var/www/html/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#53 /var/www/html/public_html/index.php(12): yii\base\Application->run()
#54 {main} {"memory":5854064,"exception":"[object] (Error(code: 0): Call to a member function getHandle() on null at /var/www/html/vendor/craftcms/cms/src/helpers/ElementHelper.php:958)"} 
brandonkelly commented 3 months ago

Thanks! Craft 5.2.9 is out now with a fix for that.

JeroenJRP commented 3 months ago

Thanks!

JeroenJRP commented 3 months ago

@brandonkelly Hmm, the PHP error is indeed gone, but now the "Element “[name of the asset]” doesn’t have a field layout provider that defines a handle, so it can’t be rendered with a partial template." error is thrown. Am I missing something? I would think the asset volume would be the layout provider here.

I tested this again by setting a profile photo and creating a twig template that only contains the line {{ currentUser.photo.render() }} to make sure no nesting of templates or fields were causing problems, but i'm still getting the no field layout provider error.

2024-07-30 09:42:27 [web.ERROR] [yii\base\InvalidConfigException] yii\base\InvalidConfigException: Element “Onstuimig Avatar 2020” doesn’t have a field layout provider that defines a handle, so it can’t be rendered with a partial template. in /var/www/html/vendor/craftcms/cms/src/helpers/ElementHelper.php:960
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/base/Element.php(6139): craft\helpers\ElementHelper::renderElements(Array, Array)
#1 /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php(1635): craft\base\Element->render()
#2 /var/www/html/vendor/craftcms/cms/src/helpers/Template.php(148): twig_get_attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\elements\Asset), 'render', Array, 'method', false, false, false, 2)
#3 /var/www/html/storage/runtime/compiled_templates/16/16ff2f4806d42b92006e96c8de63fc25.php(39): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\elements\Asset), 'render', Array, 'method', false, false, false, 2)
#4 /var/www/html/vendor/twig/twig/src/Template.php(394): __TwigTemplate_749a5e467d8717f99713fd41a1d8b029->doDisplay(Array, Array)
#5 /var/www/html/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#6 /var/www/html/vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#7 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(38): Twig\Template->render(Array)
#8 /var/www/html/vendor/twig/twig/src/Environment.php(280): Twig\TemplateWrapper->render(Array)
#9 /var/www/html/vendor/craftcms/cms/src/web/View.php(494): Twig\Environment->render('imagetest', Array)
#10 /var/www/html/vendor/craftcms/cms/src/web/View.php(547): craft\web\View->renderTemplate('imagetest', Array)
#11 /var/www/html/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(57): craft\web\View->renderPageTemplate('imagetest', Array, 'site')
#12 /var/www/html/vendor/yiisoft/yii2/web/Response.php(1109): craft\web\TemplateResponseFormatter->format(Object(craft\web\Response))
#13 /var/www/html/vendor/craftcms/cms/src/web/Response.php(338): yii\web\Response->prepare()
#14 /var/www/html/vendor/yiisoft/yii2/web/Response.php(340): craft\web\Response->prepare()
#15 /var/www/html/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#16 /var/www/html/public_html/index.php(12): yii\base\Application->run()
#17 {main} {"memory":4333352,"exception":"[object] (yii\\base\\InvalidConfigException(code: 0): Element “Onstuimig Avatar 2020” doesn’t have a field layout provider that defines a handle, so it can’t be rendered with a partial template. at /var/www/html/vendor/craftcms/cms/src/helpers/ElementHelper.php:960)"} 
brandonkelly commented 3 months ago

Sorry, looks like element partial template rendering was just broken for non-entry element types. Just fixed that for the next release.

You can grab the fix early by changing your craftcms/cms requirement in composer.json to;

"craftcms/cms": "5.x-dev as 5.2.9",

Then run composer update.

JeroenJRP commented 3 months ago

Works perfectly now, thanks!

brandonkelly commented 3 months ago

Craft 5.2.10 is out with that fix. Thanks again!