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

It has long been possible to create a context object wrapping around an entity. However, the syntax was rather cumbersome, and is now deprecated:

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

// This will raise a deprecation notice.
$context = new Context(new ContextDefinition('entity:' . $entity->getEntityTypeId()), $entity);

As of #2932462: Add EntityContextDefinition for the 80% use case, entity contexts have a dedicated class (Drupal\Core\Plugin\Context\EntityContext) and a matching context definition class (Drupal\Core\Plugin\Context\EntityContextDefinition). These classes have some nice factory methods to make them easier to instantiate:

use Drupal\Core\Plugin\Context\EntityContext;

// Create a context object from an entity...
$context = EntityContext::fromEntity(\Drupal::entityTypeManager()->getStorage('node')->load(33));

// ...or from an entity type definition...
$context = EntityContext::fromEntityType(\Drupal::entityTypeManager()->getDefinition('user'));

// ...or just an entity type ID.
$context = EntityContext::fromEntityTypeId('node');

If you want to create a context definition only (without an accompanying Context object), you can use EntityContextDefinition:

use Drupal\Core\Plugin\Context\EntityContextDefinition;

// Create a context definition from an entity type ID...
$context_definition = EntityContextDefinition::fromEntityTypeId('taxonomy_term');

// ...or with an entity...
$context_definition = EntityContextDefinition::fromEntity($entity);

// ...or an entity type definition.
$context_definition = EntityContextDefinition::fromEntityType(\Drupal::entityTypeManager()->getDefinition('media'));
Impacts: 
Module developers