Problem/Motivation

Assume the node type 'Product' with the additional view modes 'Specifications' and 'Reviews'. How do I link to the view_mode_page patterned URLs with ?

Input:

$url = Url::fromRoute('view_mode_page.display_entity', ['view_mode' => 'specifications', 'entity_type' => 'node', 'entity_id' => $node->id()]);
$output = Link::fromTextAndUrl($this->t('Specifications'), $url)->toString();

Expected output:

<a href="/products/lawn-mower/specifications">Specifications</a>

Actual output:

<a href="/view_mode_page/specifications/node/3">Specifications</a>

Proposed resolution

Either:

  1. Provide canonical URLs for view_mode_page routes based on the underlying entity canonical URL.
  2. Document how to link to the view_mode_page patterned URL using Drupal\Core\Link

Remaining tasks

To be determined.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

idebr created an issue. See original summary.

davy-r’s picture

Assigned: Unassigned » davy-r
Category: Support request » Feature request

Unfortunately the processOutbound-method isn't added yet in the DynamicPathProcessor, so it's just not possible at this moment.. I started working on this a while ago, but hadn't had time to finish it..

idebr’s picture

Status: Active » Needs review
StatusFileSize
new3.36 KB

Attached patch rewrites the outbound view_mode_page paths to the first applicable view_mode_page_pattern.

tvoesenek’s picture

StatusFileSize
new2.98 KB
new779 bytes

Small fix to prevent double language prefix in the generated urls when language is enabled.

tvoesenek’s picture

StatusFileSize
new2.97 KB
new768 bytes

Better patch, using the path alias without the language prefix, instead of stripping it from the url.

idebr’s picture

Component: Documentation » Code
StatusFileSize
new1.15 KB
new5.79 KB
new3.83 KB

The outbound path processor did not check for content translations. I have updated the patch with a fix that extends the changes in #2881652: The inbound path processor does not check for content translations, so this patch now depends on the related issue.

For testing purposes I have attached a combined patch as well.

idebr’s picture

StatusFileSize
new5.85 KB

view_mode_page-path_processor_full-2875737-6-do-not-test.patch did not contain the code from both this issue and #2881652: The inbound path processor does not check for content translations. Attached patch does.

idebr’s picture

StatusFileSize
new5.85 KB

The patch in #7 had an indentation error in view_mode_page.services.yml

wilfred waltman’s picture

StatusFileSize
new693 bytes
new5.85 KB

Fixed indention error on previous patch #7.

idebr’s picture

Url can be called with additional options, for example to generate a link to a different language. This option should be applied to the outbound path processor.

Again a combined patch with #2881652: The inbound path processor does not check for content translations is available for testing purposes.

  • davy-r committed 067c8e9 on 8.x-3.x authored by idebr
    Issue #2875737 by idebr, Tom Voesenek, Wilfred Waltman: How to generate...
  • davy-r committed 80f6c3a on 8.x-3.x authored by idebr
    Issue #2875737 by idebr, Tom Voesenek, Wilfred Waltman: How to generate...
davy-r’s picture

Status: Needs review » Fixed

Thanks for contributing!

Status: Fixed » Closed (fixed)

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