Add a view_builder handler

Created on 12 February 2025, about 2 months ago

Problem/Motivation

I was creating a new content type and I forgot to move the system tags field to the 'hidden' section in the 'Manage display' settings of my content type.

When viewing a test item of my content type, I got this error:

The website encountered an unexpected error. Try again later.

Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException: The "system_tag" entity type did not specify a view_builder handler. in Drupal\Core\Entity\EntityTypeManager->getHandler() (line 265 of core/lib/Drupal/Core/Entity/EntityTypeManager.php).
Drupal\Core\Entity\EntityTypeManager->getViewBuilder() (Line: 304)
Drupal\colorbox\Plugin\Field\FieldFormatter\ColorboxEntityReferenceFormatter->viewElements() (Line: 91)
Drupal\Core\Field\FormatterBase->view() (Line: 76)
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->view() (Line: 268)
Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple() (Line: 282)
Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildMultiple() (Line: 340)
Drupal\Core\Entity\EntityViewBuilder->buildComponents() (Line: 24)
Drupal\node\NodeViewBuilder->buildComponents() (Line: 282)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple() (Line: 239)
Drupal\Core\Entity\EntityViewBuilder->build()
call_user_func_array() (Line: 113)
Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 870)
Drupal\Core\Render\Renderer->doCallback() (Line: 432)
Drupal\Core\Render\Renderer->doRender() (Line: 248)
Drupal\Core\Render\Renderer->render() (Line: 238)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 638)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 231)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare() (Line: 128)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse() (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()
call_user_func() (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 186)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 44)
Drupal\redirect_after_login\RedirectMiddleware->handle() (Line: 53)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 116)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 741)
Drupal\Core\DrupalKernel->handle() (Line: 19)

Steps to reproduce

At this moment my project is not yet updated and runs on Drupal 10.4.1.
Enable the colorbox module.
Add or edit a content type, add system tags, go to Manage display and place the system tags in the content section.
Now view a node of your content type.

Proposed resolution

I am not sure if this should be fixed in the colorbox module or here, but the most easy fix seems to be in the system_tags module.

Edit system_tags/src/Entity/SystemTag.php
At line 16 underneath * handlers = { add this:
"view_builder" = "Drupal\Core\Entity\EntityViewBuilder",

So the docblock becomes:

/**
 * Defines the SystemTag class.
 *
 * @package Drupal\system_tags\Entity
 *
 * @ConfigEntityType(
 *   id = "system_tag",
 *   label = @Translation("System Tag"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\system_tags\SystemTagListBuilder",
 *     "form" = {
 *       "add" = "Drupal\system_tags\Form\SystemTagForm",
 *       "edit" = "Drupal\system_tags\Form\SystemTagForm",
 *       "delete" = "Drupal\system_tags\Form\SystemTagDeleteForm"
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\system_tags\SystemTagHtmlRouteProvider"
 *     },
 *     "access" = "Drupal\system_tags\SystemTagAccessControlHandler",
 *   },
 *   config_prefix = "system_tag",
 *   admin_permission = "administer system tags",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid"
 *   },
 *   config_export = {
 *     "id",
 *     "label",
 *   },
 *   links = {
 *     "add-form" = "/admin/structure/system_tags/add",
 *     "edit-form" = "/admin/structure/system_tags/{system_tag}",
 *     "delete-form" = "/admin/structure/system_tags/{system_tag}/delete",
 *     "collection" = "/admin/structure/system_tags"
 *   }
 * )
 */

Remaining tasks

Decide if this should be fixed here in system_tags or in colorbox module first.
If its in system_tags, then create a patch or MR to add "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",. Otherwise make issue with a suggested solution in the colorbox module.

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇧🇪Belgium flyke

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @flyke
  • 🇧🇪Belgium lammensj

    @flyke, this should indeed be fixed in this module. Do you want to create the patch? I believe you already have a system in place where you could test it, right?

Production build 0.71.5 2024