custom_breadcrumbs_entity_extra_field_info() adds a "Breadcrumbs" extra field to every bundle of every entity type. This includes entity types that have no canonical URL, such as Paragraphs.

If that field is enabled on such an entity's display, custom_breadcrumbs_entity_view() calls $entity->toUrl(), which throws:

Drupal\Core\Entity\Exception\UndefinedLinkTemplateException: Cannot generate default URL because no link template 'canonical' or 'edit-form' was found for the 'paragraph' entity type in Drupal\Core\Entity\EntityBase->toUrl() (line 211 of core/lib/Drupal/Core/Entity/EntityBase.php).

Backtrace (top):

#0 modules/contrib/custom_breadcrumbs/custom_breadcrumbs.module(68): Drupal\Core\Entity\EntityBase->toUrl()
#1 [internal function]: custom_breadcrumbs_entity_view(Array, Object(Drupal\paragraphs\Entity\Paragraph), Object(Drupal\Core\Entity\Entity\EntityViewDisplay), 'default')

The error occurs wherever the paragraph is rendered: node pages, Views output and Views UI preview.

Steps to reproduce

  1. Install Custom breadcrumbs 1.1.3 and Paragraphs.
  2. Create a paragraph type. In Manage display, move Breadcrumbs from Disabled to an enabled region and save.
  3. Add a Paragraphs field using that type to a content type.
  4. Create a node with a paragraph of that type and view it.
  5. The exception is logged.

Environment: Drupal core [version], PHP [version].

Proposed resolution

  1. In custom_breadcrumbs_entity_extra_field_info(), skip entity types without a canonical link template, so the field isn't offered where it can't work.
  2. In custom_breadcrumbs_entity_view(), add a defensive check before calling toUrl(), for sites that already have the component enabled on such displays.
--- a/custom_breadcrumbs.module
+++ b/custom_breadcrumbs.module
@@ -47,6 +47,9 @@ function custom_breadcrumbs_entity_extra_field_info() {
   $entityBundleInfo = \Drupal::service('entity_type.bundle.info');
 
   foreach ($entity_types as $key => $type) {
+    if (!$type->hasLinkTemplate('canonical')) {
+      continue;
+    }
     foreach ($entityBundleInfo->getBundleInfo($key) as $bundle_key => $bundle_name) {
       $extra[$key][$bundle_key]['display']['breadcrumbs'] = [
         'label' => t('Breadcrumbs'),
@@ -64,7 +67,7 @@ function custom_breadcrumbs_entity_extra_field_info() {
  * Implements hook_entity_view().
  */
 function custom_breadcrumbs_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
-  if ($display->getComponent('breadcrumbs')) {
+  if ($display->getComponent('breadcrumbs') && $entity->hasLinkTemplate('canonical')) {
     $url = $entity->toUrl();
     $router = \Drupal::service('router.no_access_checks');
     $result = $router->match($url->toString());

Workaround: Disable the Breadcrumbs field in Manage display for the affected paragraph type.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

subir_ghosh created an issue. See original summary.

andres alvarez made their first commit to this issue’s fork.

andres alvarez’s picture

Version: 1.1.3 » 1.x-dev

andres alvarez’s picture

I've opened a merge request that fixes this by handling both points from the issue summary.

Changes

  • custom_breadcrumbs_entity_extra_field_info() now skips entity types that are not content entities or that have no canonical link template. The Breadcrumbs extra field is no longer offered on displays where it can't work, such as Paragraphs.
  • custom_breadcrumbs_entity_view() now returns early when the entity has no canonical link template or is new. Sites that already have the field enabled on such displays no longer hit UndefinedLinkTemplateException from toUrl().
  • A kernel test in BreadcrumbsExtraFieldTest covers both cases.

Testing

Enable the module, then edit the manage display of an entity type without a canonical URL, for example a Paragraphs type. The Breadcrumbs field should no longer be listed. Node displays should still offer it and render as before.

Setting to Needs review.

andres alvarez’s picture

Status: Active » Needs review