Change record status: 
Project: 
Introduced in branch: 
10.4.x
Introduced in version: 
10.4.0
Description: 

Breadcrumb builders have an ::applies() method to see whether they can build the breadcrumb. However, only when this check succeeds does any cacheable metadata get added to the breadcrumb in the ::build() method. This means that any logic in the ::applies() method has no chance to set cacheable metadata in the event of a negative outcome.

This has been fixed by a new argument for the ::applies() method that allows you to set cacheable metadata there. As a quality of life improvement, your ::build() code does not need to repeat whatever cacheable metadata you specify in ::applies().

Before:

  public function applies(RouteMatchInterface $route_match);

After:

  // Drupal 11 interface.
  public function applies(RouteMatchInterface $route_match /* , CacheableMetadata $cacheable_metadata */);

  // Drupal 11 implementation.
  public function applies(RouteMatchInterface $route_match, ?CacheableMetadata $cacheable_metadata = NULL) {
    // @todo Remove null safe operator in Drupal 12.0.0.
    $cacheable_metadata?->addCacheContexts(['foo']);
    // ...
  }

  // Drupal 12.
  public function applies(RouteMatchInterface $route_match, CacheableMetadata $cacheable_metadata);


  // Drupal 12 implementation.
  public function applies(RouteMatchInterface $route_match, CacheableMetadata $cacheable_metadata) {
    $cacheable_metadata->addCacheContexts(['foo']);
    // ...
  }
Impacts: 
Module developers