Previously, context value objects were mutable, and provided a setContextValue()
method.
In order to make them immutable, three changes were made.
First, the setContextValue()
method was removed from both \Drupal\Core\Plugin\Context\ContextInterface
and \Drupal\Component\Plugin\Context\ContextInterface
Second, the ability to set a value was added to the constructor, as the second parameter. This is optional as to not break BC.
Third, because \Drupal\Core\Plugin\Context\ContextInterface
also tracks cacheable metadata associated with a given context value, that information should be preserved when replacing a context object. Therefore a method was added to ContextInterface:
public static function createFromContext(ContextInterface $old_context, $value);
Usage:
Old code
function whatever(ContextInterface $some_context) {
$some_context->setContextValue('some new value');
return $some_context;
}
New code
function whatever(ContextInterface $some_context) {
$context = Context::createFromContext($some_context, 'some new value');
return $context;
}