class ContextConfig
Class for value objects helping with context configuration.
Hierarchy
- class \Drupal\rules\Context\ContextConfig
Expanded class hierarchy of ContextConfig
See also
\Drupal\rules\Context\ContextHandlerTrait
31 files declare their use of ContextConfig
- ActionExpressionContainer.php in src/
Engine/ ActionExpressionContainer.php - ActionExpressionContainerInterface.php in src/
Engine/ ActionExpressionContainerInterface.php - AutoSaveTest.php in tests/
src/ Unit/ Integration/ Engine/ AutoSaveTest.php - ConditionExpressionContainer.php in src/
Engine/ ConditionExpressionContainer.php - ConditionExpressionContainerInterface.php in src/
Engine/ ConditionExpressionContainerInterface.php
File
-
src/
Context/ ContextConfig.php, line 13
Namespace
Drupal\rules\ContextView source
class ContextConfig {
/**
* The config array.
*
* @var array
*/
protected $config = [
'context_values' => [],
'context_mapping' => [],
'context_processors' => [],
'provides_mapping' => [],
];
/**
* Creates a context config object.
*
* @param array $values
* (optional) Some initial values to set. In the same format as returned
* from static::toArray().
*
* @return $this
*/
public static function create(array $values = []) {
return new static($values);
}
/**
* Constructs the object.
*
* @param array $values
* Some initial values to set. In the same format as returned from
* static::toArray().
*/
protected function __construct(array $values) {
$this->config = $values + $this->config;
}
/**
* Maps the data specified by the selector to the given context.
*
* @param string $context_name
* The name of the context.
* @param string $property_path
* A valid property path; e.g., "node.uid.target_id".
*
* @throws \Drupal\rules\Exception\LogicException
* Thrown if a context value and map are set for a given context at the same
* time.
*
* @return $this
*/
public function map($context_name, $property_path) {
if (isset($this->config['context_values'][$context_name])) {
throw new LogicException("Cannot map a context value and pre-define it at the same time.");
}
$this->config['context_mapping'][$context_name] = $property_path;
return $this;
}
/**
* Sets a pre-defined value for the given context.
*
* @param string $context_name
* The name of the context.
* @param mixed $value
* The value to set for the context. The value must be a valid value for the
* context's data type, unless a data processor takes care of processing it
* to a valid value.
*
* @throws \Drupal\rules\Exception\LogicException
* Thrown if a context value and map are set for a given context at the same
* time.
*
* @return $this
*/
public function setValue($context_name, $value) {
if (isset($this->config['context_mapping'][$context_name])) {
throw new LogicException("Cannot map a context value and pre-define it at the same time.");
}
$this->config['context_values'][$context_name] = $value;
return $this;
}
/**
* Maps the name of a provided context.
*
* @param string $provided_context_name
* The name of the provided context.
* @param string $context_name
* The context name under which the provided context should be registered.
*
* @return $this
*/
public function provideAs($provided_context_name, $context_name) {
$this->config['provides_mapping'][$provided_context_name] = $context_name;
return $this;
}
/**
* Sets an arbitrary configuration value under the given key.
*
* This may be used for setting any configuration options that are not making
* use of the context system.
*
* @param string $key
* The config key to set.
* @param mixed $value
* The value to set for the config key.
*
* @return $this
*/
public function setConfigKey($key, $value) {
$this->config[$key] = $value;
return $this;
}
/**
* Configures a data processor for the given context.
*
* @param string $context_name
* The name of the context.
* @param string $plugin_id
* The id of the data processor plugin to use.
* @param array $options
* (optional) An array of plugin configuration, as used by the plugin.
*
* @return $this
*/
public function process($context_name, $plugin_id, array $options = []) {
$this->config['context_processors'][$context_name][$plugin_id] = $options;
return $this;
}
/**
* Negates the result of the plugin (or not).
*
* Only applicable to condition plugins.
*
* @param bool $bool
* Whether to negate the result.
*
* @return $this
*/
public function negateResult($bool = TRUE) {
return $this->setConfigKey('negate', $bool);
}
/**
* Exports the configuration to an array.
*
* @return array
* The config array, with the following keys set:
* - context_map: An array of data selectors, keyed by context name.
* - context_values: An array of context values, keyed by context name.
* - context_processors: An array of data processor config, keyed by context
* name and process plugin id.
* - provides_mapping: An array of context names to use for provided
* context, keyed by provided context name.
* - Any other other config keys that have been set.
*/
public function toArray() {
return $this->config;
}
/**
* Checks the config for the given plugin.
*
* @param \Drupal\Core\Plugin\ContextAwarePluginInterface $plugin
* An instance of the plugin for which the config has been created.
*
* @todo Implement.
*/
public function checkConfig(CoreContextAwarePluginInterface $plugin) {
// @todo Implement.
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
ContextConfig::$config | protected | property | The config array. |
ContextConfig::checkConfig | public | function | Checks the config for the given plugin. |
ContextConfig::create | public static | function | Creates a context config object. |
ContextConfig::map | public | function | Maps the data specified by the selector to the given context. |
ContextConfig::negateResult | public | function | Negates the result of the plugin (or not). |
ContextConfig::process | public | function | Configures a data processor for the given context. |
ContextConfig::provideAs | public | function | Maps the name of a provided context. |
ContextConfig::setConfigKey | public | function | Sets an arbitrary configuration value under the given key. |
ContextConfig::setValue | public | function | Sets a pre-defined value for the given context. |
ContextConfig::toArray | public | function | Exports the configuration to an array. |
ContextConfig::__construct | protected | function | Constructs the object. |