Previously, I recall product variations having a canonical link. However, now when I try to render a variation and have it create a link, I get an exception about a missing "canonical" link on product variation entities.

Was this removed intentionally? If so, is there some other good mechanism for having a display mode of a product variation that links to its parent product with the variation pre-selected? We're trying to have individual variations show up in search_api search results, but this seems to be the only thing preventing it from working fully.

Comments

bmcclure created an issue. See original summary.

bojanz’s picture

It's implemented dynamically in ProductVariation::toUrl().

Where's the exception coming from?

replicaobscura’s picture

I've got a search results View that's showing a Search API index of product variations. The results are showing as rendered entities of a custom view mode ("Search result") of product variations. On the title of the variation, as well as the image field, I've selected in the Form display settings to link to the content. When trying to render the results page with these settings I get the mentioned error that there is no "canonical" link.

replicaobscura’s picture

The first line in my stack trace was the key, I should have posted it sooner. Apparently it's because "revision" is being used as the link $rel, which doesn't get the dynamic "canonical" editing that normally happens.

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\Exception\UndefinedLinkTemplateException: No link template 'canonical' found for the 'commerce_product_variation' entity type in Drupal\Core\Entity\Entity->toUrl() (line 214 of core/lib/Drupal/Core/Entity/Entity.php).
Drupal\commerce_product\Entity\ProductVariation->toUrl('revision', Array) (Line: 171)
Drupal\Core\Entity\Entity->urlInfo('revision') (Line: 120)
Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter->viewElements(Object, 'en') (Line: 80)
Drupal\Core\Field\FormatterBase->view(Object, 'en') (Line: 259)
Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple(Array) (Line: 319)
Drupal\Core\Entity\EntityViewBuilder->buildComponents(Array, Array, Array, 'search_result') (Line: 262)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 219)
Drupal\Core\Entity\EntityViewBuilder->build(Array)
call_user_func(Array, Array) (Line: 376)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 474)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 65)
__TwigTemplate_c5f88a1c8a45d2893b4f99d05ad0c48e79698bb1233ee134b2607365532499f4->doDisplay(Array, Array) (Line: 432)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 403)
Twig_Template->display(Array) (Line: 411)
Twig_Template->render(Array) (Line: 64)
twig_render_template('core/themes/classy/templates/views/views-view-unformatted.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('views_view_unformatted', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 448)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 474)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 126)
__TwigTemplate_dbc8f6cd1ec5b4058375582f945bc856109f570e5bb2dfe991ffbf9323748d10->doDisplay(Array, Array) (Line: 432)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 403)
Twig_Template->display(Array) (Line: 411)
Twig_Template->render(Array) (Line: 64)
twig_render_template('themes/goodway/templates/views/views-view.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('views_view', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 448)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 149)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 652)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
replicaobscura’s picture

I'm unclear on why the "revision" link is being requested on a product variation in the first place. Is this the actual bug? I didn't think variations were currently revisionable.

The parent class's toUrl() method includes this block:

    // Links pointing to the current revision point to the actual entity. So
    // instead of using the 'revision' link, use the 'canonical' link.
    if ($rel === 'revision' && $this instanceof RevisionableInterface && $this->isDefaultRevision()) {
      $rel = 'canonical';
    }

This is what's converting the "revision" $rel into "canonical" and then failing for variations since it's already inside the parent method's call. But if variations aren't revisionable then why the "revision" link is being requested as part of the rendered entity display mode is confusing to me.

replicaobscura’s picture

The default StringFormatter field plugin, if linking to the entity, automatically uses $url = $items->getEntity()->urlInfo('revision'); without knowing if the item is revisionable, as far as I can tell. I'm working on a patch for commerce that'll convert "revision" back to "canonical" before the conditional so that the dynamic link template works still.

replicaobscura’s picture

In actuality, I think this is possibly a core bug, though I don't know if it's already got a separate issue. "revision" should not be assumed a valid link template on every entity, only revisionable entities.

However, since it's easier to fix here for now, this patch fixes the issue for me.

replicaobscura’s picture

replicaobscura’s picture

Status: Active » Needs review
Related issues: +#2850705: StringFormatter assumes "revision" link template exists

Linked to the related core issue I filed. Not 100% sure it's needed yet, but it seems like it to me.

bojanz’s picture

Title: Canonical link for product variations missing? » Can't link to a product variation using core's StringFormatter

More precise title.

  • bojanz committed 42dd5ea on 8.x-2.x authored by bmcclure
    Issue #2847234 by bmcclure, bojanz: Can't link to a product variation...
bojanz’s picture

Status: Needs review » Fixed

Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.