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

The context handler service (\Drupal\Core\Plugin\Context\ContextHandlerInterface) can filter a set of plugin definitions by a set of context values. However, prior to Drupal 8.6.0, it could only filter array-based plugin definitions with a context key containing a set of context definitions (i.e., instances of \Drupal\Component\Plugin\Context\ContextDefinitionInterface). Object-based plugin definitions were not supported.

As of #2961822: Support object-based plugin definitions in ContextHandler, object-based plugin definitions are now supported as well, as long as they implement the new \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface. To help developers, \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionTrait has been added as well. This trait implements all methods of the interface so that you can easily add it to plugin definition classes.

Object-based plugin definitions can be filtered by context thusly (assuming that $plugin_definitions is an array of objects implementing ContextAwarePluginDefinitionInterface):

use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;

$contexts = [
  'name' => new Context(ContextDefinition::create('string'), 'druplicon'),
];

/* @var \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface[] $plugin_definitions */
\Drupal::service('context.handler')->filterPluginDefinitionsByContexts($contexts, $plugin_definitions);
Impacts: 
Module developers