Call to undefined method stdClass::label() in template_preprocess_calendar_stripe_legend()
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3591667. --> Reported by: [troybthompson](https://www.drupal.org/user/183560) >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>On my calendar page, I was receiving a white page with the error:</p> <pre>Error: Call to undefined method stdClass::label() in template_preprocess_calendar_stripe_legend() (line 747 of modules/contrib/calendar/calendar.theme.inc).<br>call_user_func_array() (Line: 261)<br>Drupal\Core\Theme\ThemeManager-&gt;render() (Line: 490)<br>Drupal\Core\Render\Renderer-&gt;doRender() (Line: 248)<br>Drupal\Core\Render\Renderer-&gt;render() (Line: 484)<br>Drupal\Core\Template\TwigExtension-&gt;escapeFilter() (Line: 90)<br>__TwigTemplate_055df43571660829bb743cbb0348c664-&gt;block_content() (Line: 432)<br>Twig\Template-&gt;yieldBlock() (Line: 73)<br>__TwigTemplate_055df43571660829bb743cbb0348c664-&gt;doDisplay() (Line: 388)<br>Twig\Template-&gt;yield() (Line: 344)<br>Twig\Template-&gt;display() (Line: 359)<br>Twig\Template-&gt;render() (Line: 51)<br>Twig\TemplateWrapper-&gt;render() (Line: 33)<br>twig_render_template() (Line: 348)<br>Drupal\Core\Theme\ThemeManager-&gt;render() (Line: 490)<br>Drupal\Core\Render\Renderer-&gt;doRender() (Line: 503)<br>Drupal\Core\Render\Renderer-&gt;doRender() (Line: 248)<br>Drupal\Core\Render\Renderer-&gt;render() (Line: 484)<br>Drupal\Core\Template\TwigExtension-&gt;escapeFilter() (Line: 117)<br>__TwigTemplate_a86a2880add9af7d4e6774f6abbf78ff-&gt;doDisplay() (Line: 388)<br>Twig\Template-&gt;yield() (Line: 344)<br>Twig\Template-&gt;display() (Line: 359)<br>Twig\Template-&gt;render() (Line: 51)<br>Twig\TemplateWrapper-&gt;render() (Line: 33)<br>twig_render_template() (Line: 348)<br>Drupal\Core\Theme\ThemeManager-&gt;render() (Line: 490)<br>Drupal\Core\Render\Renderer-&gt;doRender() (Line: 248)<br>Drupal\Core\Render\Renderer-&gt;render() (Line: 484)<br>Drupal\Core\Template\TwigExtension-&gt;escapeFilter() (Line: 98)<br>__TwigTemplate_af55e46dd1980d4f1bc6003d7c1bf37c-&gt;doDisplay() (Line: 388)<br>Twig\Template-&gt;yield() (Line: 344)<br>Twig\Template-&gt;display() (Line: 359)<br>Twig\Template-&gt;render() (Line: 51)<br>Twig\TemplateWrapper-&gt;render() (Line: 33)<br>twig_render_template() (Line: 348)<br>Drupal\Core\Theme\ThemeManager-&gt;render() (Line: 490)<br>Drupal\Core\Render\Renderer-&gt;doRender() (Line: 248)<br>Drupal\Core\Render\Renderer-&gt;render() (Line: 158)<br>Drupal\Core\Render\MainContent\HtmlRenderer-&gt;Drupal\Core\Render\MainContent\{closure}() (Line: 637)<br>Drupal\Core\Render\Renderer-&gt;executeInRenderContext() (Line: 153)<br>Drupal\Core\Render\MainContent\HtmlRenderer-&gt;renderResponse() (Line: 90)<br>Drupal\Core\EventSubscriber\MainContentViewSubscriber-&gt;onViewRenderArray()<br>call_user_func() (Line: 111)<br>Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher-&gt;dispatch() (Line: 186)<br>Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw() (Line: 76)<br>Symfony\Component\HttpKernel\HttpKernel-&gt;handle() (Line: 53)<br>Drupal\Core\StackMiddleware\Session-&gt;handle() (Line: 48)<br>Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle() (Line: 28)<br>Drupal\Core\StackMiddleware\ContentLength-&gt;handle() (Line: 32)<br>Drupal\big_pipe\StackMiddleware\ContentLength-&gt;handle() (Line: 116)<br>Drupal\page_cache\StackMiddleware\PageCache-&gt;pass() (Line: 90)<br>Drupal\page_cache\StackMiddleware\PageCache-&gt;handle() (Line: 48)<br>Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle() (Line: 51)<br>Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle() (Line: 36)<br>Drupal\Core\StackMiddleware\AjaxPageState-&gt;handle() (Line: 51)<br>Drupal\Core\StackMiddleware\StackedHttpKernel-&gt;handle() (Line: 741)<br>Drupal\Core\DrupalKernel-&gt;handle() (Line: 19)</pre><h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>I am not a PHP programmer so since the page was completely broken, in a pinch I put the error and calendar.theme.inc into ChatGPT and it proposed the following:<br> ---<br> The failure is here:<br> foreach ($vocab as $term) {<br> $options[$term-&gt;tid] = $term-&gt;label();<br> }<br> inside:<br> function template_preprocess_calendar_stripe_legend(array &amp;$vars)<br> The issue is that:<br> \Drupal::entityTypeManager()<br> -&gt;getStorage('taxonomy_term')<br> -&gt;loadTree($vid);<br> does not return taxonomy entity objects.<br> It returns lightweight stdClass objects from the tree API.<br> Those objects have:<br> $term-&gt;tid<br> $term-&gt;name<br> but NOT:<br> $term-&gt;label()<br> So this line:<br> $term-&gt;label()<br> will always fatal with exactly the error you&rsquo;re seeing.<br> The fix is simple.<br> Replace:<br> $options[$term-&gt;tid] = $term-&gt;label();<br> with:<br> $options[$term-&gt;tid] = $term-&gt;name;<br> So the corrected block becomes:<br> case 'taxonomy':<br> $vocabularies = (array) $row_options['colors']['calendar_colors_vocabulary'];</p> <p> foreach ($vocabularies as $vid) {<br> $vocab = \Drupal::entityTypeManager()<br> -&gt;getStorage('taxonomy_term')<br> -&gt;loadTree($vid);</p> <p> foreach ($vocab as $term) {<br> $options[$term-&gt;tid] = $term-&gt;name;<br> }<br> }<br> break;<br> Why this happens:<br> loadTree() is optimized for performance<br> It intentionally returns plain objects instead of full entities<br> Full taxonomy entities support label()<br> Tree objects only expose name</p> <p>---</p> <p>So on line 747 I replaced "$options[$term-&gt;tid] = $term-&gt;label();" with "$options[$term-&gt;tid] = $term-&gt;name;" and it appears to have solved the problem. </p> <p>But I don't know enough to know if it's the correct solution. Can someone verify that change should be made?</p>
issue