1

I am use Easy Breadcrumb module for a D8 site. I have everything I need with this module as it alters the existing Breadcrumb block, but it outputs relative URLs.

How can I change the relative URLs to absolute?

Current issue (Breadcrumb Output):

<ol>
<li>
    <a href="/level-one">level One</a>
</li>
<li>
    <a href="/level-one/level-two">level two</a>
</li>
<li>
    <a href="/level-one/level-two/level-three">level three</a>
</li>
<li>
Current Level
</li>
</ol>

Expected output:

<ol>
<li>
    <a href="https://www.domain.com/level-one">level One</a>
</li>
<li>
    <a href="https://www.domain.com/level-one/level-two">level two</a>
</li>
<li>
    <a href="https://www.domain.com/level-one/level-two/level-three">level three</a>
</li>
<li>
Current Level
</li>
</ol>
0

1 Answer 1

0

You can alter the breadcrumbs of other bread crumb builders (core or contrib) in a hook_system_breadcrumb_alter():

/**
 * Implements hook_system_breadcrumb_alter().
 */
function mymodule_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context) {
  $links = $breadcrumb->getLinks();
  foreach ($links as $link) {
    $url = $link->getUrl();
    $url->setAbsolute();
  }
}
3
  • I was reviewing url::setOption and stumbled upon url::setAbsolute and couldn't figure out which one is prefered. I understand that setOption takes in two parameters, but setAbsolute also works except it specifically takes 1 parameter and always sets the absolute to True (assuming default is used) or false if that is passed. What would be recommended? Commented Jan 2, 2018 at 21:38
  • Both do the same, but since the specific method exists, why not use it? I'll change it in the code.
    – 4uk4
    Commented Jan 2, 2018 at 22:17
  • I have used the setAbsolute() method. It works in all my test cases. Commented Jan 3, 2018 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.