Change record status: 
Introduced in version: 
1.2.3
Description: 

What changed

The four BuildFieldTrait methods previously required a
CacheableMetadata $cacheable_metadata object as their first
argument. Callers were responsible for creating it, passing it to the method,
and applying it to the build array afterwards.

  // Before
  $cacheable_metadata = CacheableMetadata::createFromRenderArray($build);
  $build[] = $this->buildReferencedEntities(
    $cacheable_metadata,
    $entity->field_paragraphs,
    'full',
  );
  $cacheable_metadata->applyTo($build);
  return $build;
  

EntityViewBuilderPluginAbstract now owns a
$this->cacheableMetadata property that is automatically
initialized from the incoming $build array at the start of
build() and applied to the returned array at the end. The four
methods now use this property directly, so callers no longer need to create or
manage a CacheableMetadata object.

  // After
  $build[] = $this->buildReferencedEntities(
    $entity->field_paragraphs,
    'full',
  );
  return $build;
  

The affected method signatures are:

Method Old first argument New first argument
buildEntities() CacheableMetadata $cacheable_metadata array $entities
buildReferencedEntities() CacheableMetadata $cacheable_metadata ?EntityReferenceFieldItemListInterface $reference_field
buildEntitiesWithViewModes() CacheableMetadata $cacheable_metadata array $entities
buildReferencedEntitiesWithViewModes() CacheableMetadata $cacheable_metadata ?EntityReferenceFieldItemListInterface $reference_field

Who is affected

Anyone who has upgraded to 1.2.0 and has a custom plugin that extends
EntityViewBuilderPluginAbstract and calls one of the four affected
methods, passing a CacheableMetadata object as the first
argument.

Action required

Remove the CacheableMetadata create/pass/apply boilerplate from
your build*() methods and drop the first argument from any calls
to the affected methods.

If you load entities or run entity queries outside of these methods, add
them to $this->cacheableMetadata directly instead:

  // Loading an entity directly:
  $sidebar = $this->entityTypeManager->getStorage('block_content')->load(42);
  if ($sidebar) {
    $this->cacheableMetadata->addCacheableDependency($sidebar);
    $build[] = $this->entityTypeManager
      ->getViewBuilder('block_content')
      ->view($sidebar);
  }

  // Entity query:
  $this->cacheableMetadata->addCacheTags(['node_list']);