How do I build Breadcrumbs for a certain node? In a node preprocess hook I am getting breadcrumbs for the currently viewed node by doing the following:

$vars['breadcrumb'] = Drupal::service('breadcrumb')
    -&gt;build(Drupal::routeMatch())-&gt;toRenderable();


This works fine as it uses Drupal::routeMatch() to get the matched route for the current request.

But I also want to get the breadcrumb for each node in a listing of several nodes. In this case I cannot use Drupal::routeMatch() since I want to get the breadcrumb for each node in the listing, not the current page. The problem is I cannot find a way of getting a RouteMatchInterface object (needed by the BreadcrumbBuilder) from a node object. I can get the url containing route information via $node-&gt;urlInfo()but there seems to be no way of getting from there to a route match either. 

How can this be done?

Edit:

I found a way to get the route match for a certain node. Not sure if it's the ideal way but it gets the job done:

$routeName = $node-&gt;urlInfo()-&gt;getRouteName();
$routeParameters = $node-&gt;urlInfo()-&gt;getRouteParameters();
$route = Drupal::service('router.route_provider')-&gt;getRouteByName($routeName);
$routeMatch = new RouteMatch($routeName, $route, $routeParameters);


However passing the $routeMatch into PathBasedBredcrumbBuilder::create does not generate bredcrumbs for that route! 

On closer inspection it seems that the built-in PathBasedBreadCrumbBuilder class completely ignores the passed in $routeMatch parameter and instead builds the breadcrumbs using the path of the current page, fetched using the injected RequestContext object. I think I would need to swap out this service with my own implementation which uses the passed in parameters somehow. To be continued...
I eventually solved it by creating my own subclass of the PathBasedBredcrumbBuilder from core. This class unlike core's version actually uses the passed in route_match and returns breadcrumbs based on that which makes it possible to get breadcrumbs for a certain node (or any other given route). 

I still need to calculate the route match from the current node to pass to the build method (as shown in the question). It feels a bit awkward since the route_match is converted back into a url object in the build method but  it works and it is the cleanest solution i've come up with so far.

Below is the subclass:

namespace Drupal\contextual_breadcrumbs;

use Drupal\system\PathBasedBreadcrumbBuilder;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

class ContextualPathBasedBreadcrumbBuilder extends PathBasedBreadcrumbBuilder {

    /**
    * {@inheritdoc}
    */
    public function build(RouteMatchInterface $route_match) {

      /// Set request context from passed in $route_match
      $url = Url::fromRouteMatch($route_match);
      if ($request = $this-&gt;getRequestForPath($url-&gt;toString(), [])) {
        $context = new RequestContext();
        $context-&gt;fromRequest($request);
        $this-&gt;context = $context;
      } 

      // Build breadcrumbs using new context ($route_match is unused)
      return parent::build($route_match);
    }

}