CivicrmBreadcrumbBuilder is getting invalid URLs and breaking
Overview
I ran into this exception while trying to open most any page in CiviCase (using compucorp's CiviCase but I don't think that matters).
InvalidArgumentException: The user-entered string 'https://example.com/civicrm/case/a/?case_type_category=1#/case?case_type_category=1' must begin with a '/', '?', or '#'. in Drupal\Core\Url::fromUserInput() (line 213 of core/lib/Drupal/Core/Url.php).
I found that the breadcrumbs list contained:
- /
- /civicrm?reset=1
- https://example.com/civicrm/case/a/?case_type_category=1#/case?case_type_category=1
So somehow the items being added to breadcrumbs list weren't getting parsed correctly. I worked around this temporarily by modifying the build() function to only add valid items:
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb->addCacheContexts(['url']);
$breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
foreach ($this->civicrmPageState->getBreadcrumbs() as $name => $url) {
// All urls here have been passed trough CRM_Utils_System::url, so we have
// to parse and decode them before creating a drupal Url object.
> if (substr($url, 0, 1) == "/") {
$url = Url::fromUserInput(html_entity_decode($url));
$breadcrumb->addLink(new Link($name, $url));
> }
}
return $breadcrumb;
}
Since it's not a big deal to me to not have the current page in breadcrumbs, and this was an easy way to fix.
I'm not sure how the invalid url was added to the breadcrumbs, and whether that's happening in the extension or in core, but it does seem like a little checking to be sure an invalid breadcrumb doesn't cause the whole page to break would be useful in core.
Environment information
- CiviCRM: 5.38.0