18.12.2025 | Dominik Wille

How to pass a context to a condition plugin instance

ResolveConditionsWithContext.php
use ConditionAccessResolverTrait;


$conditions = [];
foreach ($conditions as $condition_id => $condition) {
  if ($condition instanceof ContextAwarePluginInterface) {
    try {
      $contexts = $this->contextRepository->getRuntimeContexts(array_values($condition->getContextMapping()));
      $this->contextHandler->applyContextMapping($condition, $contexts);
    }
    catch (MissingValueContextException $e) {
      $missing_value = TRUE;
    }
    catch (ContextException $e) {
      $missing_context = TRUE;
    }
  }
  $conditions[$condition_id] = $condition;
}

$this->resolveConditions($conditions, 'and')

Issue:

You want to manually create a condition instance (e.g.: "am I on an article?") but it will initially lack a runtime context which would usually be provided by the integrating UI / system, e.g. visibility settings on blocks.

Solution:

Use the snippet provided to insert all available runtime contexts. This way, yours will be included.