Call to a member function label() on null after saving a node with a taxonomy term

I create nodes, which have a taxonomy field, like this:

$node = Node::create([   'type' => 'article',   'status' => 1,   'title' => 'Some title',   ...   'field_taxonomy' => [1, 2, 3], ]); $node->save(); 

The nodes are saved and can be displayed. But the views, which should display articles with selected taxonomy terms, return the following error message:

Error: Call to a member function label() on null in DrupaltaxonomyPluginviewsfieldTaxonomyIndexTid->preRender() (Line 149 in /var/www/html/core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php)  #0 /var/www/html/core/modules/views/src/ViewExecutable.php(1503): DrupaltaxonomyPluginviewsfieldTaxonomyIndexTid->preRender(Array)  #1 /var/www/html/core/modules/views/src/Plugin/views/display/Page.php(183): DrupalviewsViewExecutable->render()  #2 /var/www/html/core/modules/views/src/ViewExecutable.php(1630): DrupalviewsPluginviewsdisplayPage->execute()  #3 /var/www/html/core/modules/views/src/Element/View.php(77): DrupalviewsViewExecutable->executeDisplay('alle', Array)  #4 [internal function]: DrupalviewsElementView::preRenderViewElement(Array)  #5 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(378): call_user_func(Array, Array)  #6 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(195): DrupalCoreRenderRenderer->doRender(Array, false)  #7 /var/www/html/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(226): DrupalCoreRenderRenderer->render(Array, false)  #8 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(582): DrupalCoreRenderMainContentHtmlRenderer->DrupalCoreRenderMainContent{closure}()  #9 /var/www/html/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(227): DrupalCoreRenderRenderer->executeInRenderContext(Object(DrupalCoreRenderRenderContext), Object(Closure))  #10 /var/www/html/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(117): DrupalCoreRenderMainContentHtmlRenderer->prepare(Array, Object(SymfonyComponentHttpFoundationRequest), Object(DrupalCoreRoutingCurrentRouteMatch))  #11 /var/www/html/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): DrupalCoreRenderMainContentHtmlRenderer->renderResponse(Array, Object(SymfonyComponentHttpFoundationRequest), Object(DrupalCoreRoutingCurrentRouteMatch))  #12 [internal function]: DrupalCoreEventSubscriberMainContentViewSubscriber->onViewRenderArray(Object(SymfonyComponentHttpKernelEventGetResponseForControllerResultEvent), 'kernel.view', Object(DrupalComponentEventDispatcherContainerAwareEventDispatcher))  #13 /var/www/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(SymfonyComponentHttpKernelEventGetResponseForControllerResultEvent), 'kernel.view', Object(DrupalComponentEventDispatcherContainerAwareEventDispatcher))  #14 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(156): DrupalComponentEventDispatcherContainerAwareEventDispatcher->dispatch('kernel.view', Object(SymfonyComponentHttpKernelEventGetResponseForControllerResultEvent))  #15 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(68): SymfonyComponentHttpKernelHttpKernel->handleRaw(Object(SymfonyComponentHttpFoundationRequest), 1)  #16 /var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php(57): SymfonyComponentHttpKernelHttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)  #17 /var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): DrupalCoreStackMiddlewareSession->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)  #18 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): DrupalCoreStackMiddlewareKernelPreHandle->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)  #19 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupalpage_cacheStackMiddlewarePageCache->pass(Object(SymfonyComponentHttpFoundationRequest), 1, true)  #20 /var/www/html/modules/jsonapi/src/StackMiddleware/FormatSetter.php(51): Drupalpage_cacheStackMiddlewarePageCache->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)  #21 /var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): DrupaljsonapiStackMiddlewareFormatSetter->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)  #22 /var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): DrupalCoreStackMiddlewareReverseProxyMiddleware->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)  #23 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): DrupalCoreStackMiddlewareNegotiationMiddleware->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)  #24 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(669): StackStackedHttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)  #25 /var/www/html/index.php(19): DrupalCoreDrupalKernel->handle(Object(SymfonyComponentHttpFoundationRequest))  #26 {main}. 

If I manually open the editing form for every of the new nodes and save the node (without changing anything), then the error message disappear and the views display correct content. Resaving all new articles in a bulk operation or clearing all caches do not solve the issue.

The line 149 in /core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php looks like:

127 public function preRender(&$values) { 128     $vocabularies = $this->vocabularyStorage->loadMultiple(); ... 144     foreach ($result as $node_nid => $data) { 145         foreach ($data as $tid => $term) { ... 149             $this->items[$node_nid][$tid]['vocabulary'] = $vocabularies[$term->bundle()]->label(); ...             }         }     } 
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Call to a member function label() on null after saving a node with a taxonomy term

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.