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

Plugin derivers are often used to iterate over another set of data and create plugin definitions for each item.

When that set of data changes, manual calls must be made to clear all of the plugin definitions.
These calls are typically put in hook implementations.

With this change, plugin derivers can specify cache tags for the plugin definitions.

For example, the \Drupal\layout_builder\Plugin\Derivative\FieldBlockDeriver loops over the list of fields and created block plugin definitions for each.

Before:

function layout_builder_field_config_insert(FieldConfigInterface $field_config) {
  \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
}

class FieldBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
  public function getDerivativeDefinitions($base_plugin_definition) {
    foreach ($this->entityFieldManager->getFieldMap() as $entity_type_id => $entity_field_map) {
      // ...
    }
  }
}

After:

use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableDependencyTrait;

class ExtraFieldBlockDeriver extends DeriverBase implements ContainerDeriverInterface, CacheableDependencyInterface {
  use CacheableDependencyTrait;

  public function getDerivativeDefinitions($base_plugin_definition) {
    $this->addCacheableDependency($this->entityFieldManager);

    foreach ($this->entityFieldManager->getFieldMap() as $entity_type_id => $entity_field_map) {
      // ...
    }
  }
}
Impacts: 
Module developers