Use the toUrl to create the link (compatibility with Forum module)

Created on 14 April 2021, about 3 years ago
Updated 25 January 2024, 5 months ago

Problem/Motivation

If you check the checkbox to link a term while using the hierarchical_term_formatter, this method is used to get the url of the link:

new Url('entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]);

This successfully pulls in the link to the term at /taxonomy/term/x OR at /my-clean-path-to-x. But if a module has altered the uri_callback for an entity, the new uriCallback is ignored.

The best practice for getting a term link is to use $term->toUri, which will pick up that custom uri_callback. It's also the method that the core-provided "Label" formatter uses.

Steps to reproduce

Probably the easiest way to reproduce would be to try to use this formatter in conjunction with the Forum module, which does provide a custom uri callback. If a taxonomy bundle is selected in the Forum settings, its canonical URLs should be altered to /forum/x instead of /taxonomy/term/x. But the heirarchal link formatter still points to /taxonomy/term.

Proposed resolution

Change

$url = new Url('entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]);

to

$url = $term->toUrl();

Remaining tasks

Patch for above

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States mariacha1

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024