0

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 Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid->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): Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid->preRender(Array) 
#1 /var/www/html/core/modules/views/src/Plugin/views/display/Page.php(183): Drupal\views\ViewExecutable->render() 
#2 /var/www/html/core/modules/views/src/ViewExecutable.php(1630): Drupal\views\Plugin\views\display\Page->execute() 
#3 /var/www/html/core/modules/views/src/Element/View.php(77): Drupal\views\ViewExecutable->executeDisplay('alle', Array) 
#4 [internal function]: Drupal\views\Element\View::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): Drupal\Core\Render\Renderer->doRender(Array, false) 
#7 /var/www/html/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(226): Drupal\Core\Render\Renderer->render(Array, false) 
#8 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() 
#9 /var/www/html/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(227): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) 
#10 /var/www/html/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(117): Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch)) 
#11 /var/www/html/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch)) 
#12 [internal function]: Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher)) 
#13 /var/www/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher)) 
#14 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(156): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent)) 
#15 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) 
#16 /var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) 
#17 /var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) 
#18 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) 
#19 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) 
#20 /var/www/html/modules/jsonapi/src/StackMiddleware/FormatSetter.php(51): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) 
#21 /var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\jsonapi\StackMiddleware\FormatSetter->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) 
#22 /var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) 
#23 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) 
#24 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(669): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) 
#25 /var/www/html/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) 
#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();
...
            }
        }
    }
  • Seems that you are adding a taxonomy term ids that don't exist. – Hudri Jan 3 at 16:38
  • No. If a term does not exist, then it is created. The code here is simplified... – Antonín Slejška Jan 3 at 16:42
  • @AntonínSlejška What's version of drupal? Maybe have problem with publish and unpublish of term. And let ensure term exist first. – Jonh Jan 3 at 17:09
  • @Jonh There is Drupal 8.6. It is a good idea. I will try it. Thanks. – Antonín Slejška Jan 3 at 17:57
0

I believe it has to do with how you are trying to assign values to the field.

I think you want:

$node = Node::create([
  'type' => 'article',
  'status' => 1,
  'title' => 'Some title',
]);


$node->set('field_taxonomy', [1, 2, 3]);
$node->save();
  • Both examples should work the same - I suspect it’s because target_id is missing from the array – Clive Jan 3 at 16:41
  • Also, probably want to ensure the terms exist first. – Kevin Jan 3 at 16:42
  • Thanks Kevin. I have tried this approach, but it did not solve the issue. Both approaches return in my case exactly the same results. – Antonín Slejška Jan 3 at 16:45
  • line 149 has two method calls, first $term->bundle() and then $vocabulary->label(). So the term exists, but the bundle is NULL or not existing. – 4k4 Jan 3 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.