You are here

protected function ContextHandlerTrait::prepareContextWithMetadata in Rules 8.3

Prepares plugin context based upon the set context configuration.

The configuration is applied as far as possible without having execution time data. That means, the configured context values are set and context is refined while leveraging the definitions of selected data.

Parameters

\Drupal\Core\Plugin\ContextAwarePluginInterface $plugin: The plugin that is prepared.

\Drupal\rules\Context\ExecutionMetadataStateInterface $metadata_state: The metadata state, prepared for the current expression.

Throws

\Drupal\Component\Plugin\Exception\ContextException Thrown if the plugin tries to access some not-defined context. As this is a developer error, this should not be caught.

See also

::prepareContext()

4 calls to ContextHandlerTrait::prepareContextWithMetadata()
ActionExpression::checkIntegrity in src/Plugin/RulesExpression/ActionExpression.php
Verifies that this expression is configured correctly.
ActionExpression::prepareExecutionMetadataState in src/Plugin/RulesExpression/ActionExpression.php
Prepares the execution metadata state by adding metadata to it.
ConditionExpression::checkIntegrity in src/Plugin/RulesExpression/ConditionExpression.php
Verifies that this expression is configured correctly.
ConditionExpression::prepareExecutionMetadataState in src/Plugin/RulesExpression/ConditionExpression.php
Prepares the execution metadata state by adding metadata to it.

File

src/Context/ContextHandlerTrait.php, line 121

Class

ContextHandlerTrait
Provides methods for handling context based on the plugin configuration.

Namespace

Drupal\rules\Context

Code

protected function prepareContextWithMetadata(CoreContextAwarePluginInterface $plugin, ExecutionMetadataStateInterface $metadata_state) {
  if (isset($this->configuration['context_values'])) {
    foreach ($this->configuration['context_values'] as $name => $value) {
      $plugin
        ->setContextValue($name, $value);
    }
  }
  if ($plugin instanceof ContextAwarePluginInterface) {
    $selected_data = $this
      ->getSelectedData($metadata_state);

    // Getting context values may lead to undocumented exceptions if context
    // is not set right now. So catch those exceptions.
    // @todo Remove once https://www.drupal.org/node/2677162 is fixed in core.
    try {
      $plugin
        ->refineContextDefinitions($selected_data);
    } catch (ContextException $e) {
      if (strpos($e
        ->getMessage(), 'context is required') === FALSE) {
        throw $e;
      }
    }
  }
}