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

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();
  }
}
  • 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? – usernameabc Jan 2 '18 at 21:38
  • Both do the same, but since the specific method exists, why not use it? I'll change it in the code. – 4k4 Jan 2 '18 at 22:17
  • I have used the setAbsolute() method. It works in all my test cases. – usernameabc Jan 3 '18 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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