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->render() (Line: 490)<br>Drupal\Core\Render\Renderer->doRender() (Line: 248)<br>Drupal\Core\Render\Renderer->render() (Line: 484)<br>Drupal\Core\Template\TwigExtension->escapeFilter() (Line: 90)<br>__TwigTemplate_055df43571660829bb743cbb0348c664->block_content() (Line: 432)<br>Twig\Template->yieldBlock() (Line: 73)<br>__TwigTemplate_055df43571660829bb743cbb0348c664->doDisplay() (Line: 388)<br>Twig\Template->yield() (Line: 344)<br>Twig\Template->display() (Line: 359)<br>Twig\Template->render() (Line: 51)<br>Twig\TemplateWrapper->render() (Line: 33)<br>twig_render_template() (Line: 348)<br>Drupal\Core\Theme\ThemeManager->render() (Line: 490)<br>Drupal\Core\Render\Renderer->doRender() (Line: 503)<br>Drupal\Core\Render\Renderer->doRender() (Line: 248)<br>Drupal\Core\Render\Renderer->render() (Line: 484)<br>Drupal\Core\Template\TwigExtension->escapeFilter() (Line: 117)<br>__TwigTemplate_a86a2880add9af7d4e6774f6abbf78ff->doDisplay() (Line: 388)<br>Twig\Template->yield() (Line: 344)<br>Twig\Template->display() (Line: 359)<br>Twig\Template->render() (Line: 51)<br>Twig\TemplateWrapper->render() (Line: 33)<br>twig_render_template() (Line: 348)<br>Drupal\Core\Theme\ThemeManager->render() (Line: 490)<br>Drupal\Core\Render\Renderer->doRender() (Line: 248)<br>Drupal\Core\Render\Renderer->render() (Line: 484)<br>Drupal\Core\Template\TwigExtension->escapeFilter() (Line: 98)<br>__TwigTemplate_af55e46dd1980d4f1bc6003d7c1bf37c->doDisplay() (Line: 388)<br>Twig\Template->yield() (Line: 344)<br>Twig\Template->display() (Line: 359)<br>Twig\Template->render() (Line: 51)<br>Twig\TemplateWrapper->render() (Line: 33)<br>twig_render_template() (Line: 348)<br>Drupal\Core\Theme\ThemeManager->render() (Line: 490)<br>Drupal\Core\Render\Renderer->doRender() (Line: 248)<br>Drupal\Core\Render\Renderer->render() (Line: 158)<br>Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 637)<br>Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 153)<br>Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse() (Line: 90)<br>Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()<br>call_user_func() (Line: 111)<br>Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 186)<br>Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)<br>Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)<br>Drupal\Core\StackMiddleware\Session->handle() (Line: 48)<br>Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)<br>Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)<br>Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 116)<br>Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90)<br>Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)<br>Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)<br>Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)<br>Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)<br>Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 741)<br>Drupal\Core\DrupalKernel->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->tid] = $term->label();<br>
}<br>
inside:<br>
function template_preprocess_calendar_stripe_legend(array &$vars)<br>
The issue is that:<br>
\Drupal::entityTypeManager()<br>
->getStorage('taxonomy_term')<br>
->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->tid<br>
$term->name<br>
but NOT:<br>
$term->label()<br>
So this line:<br>
$term->label()<br>
will always fatal with exactly the error you’re seeing.<br>
The fix is simple.<br>
Replace:<br>
$options[$term->tid] = $term->label();<br>
with:<br>
$options[$term->tid] = $term->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>
->getStorage('taxonomy_term')<br>
->loadTree($vid);</p>
<p> foreach ($vocab as $term) {<br>
$options[$term->tid] = $term->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->tid] = $term->label();" with "$options[$term->tid] = $term->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