-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathRulesAction.php
More file actions
138 lines (119 loc) · 4.42 KB
/
Copy pathRulesAction.php
File metadata and controls
138 lines (119 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* @file
* Contains \Drupal\rules\Plugin\Expression\RulesAction.
*/
namespace Drupal\rules\Plugin\Expression;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Action\ActionManager;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\rules\Context\ContextHandlerTrait;
use Drupal\rules\Core\RulesActionBase;
use Drupal\rules\Engine\ActionExpressionInterface;
use Drupal\rules\Engine\ExpressionTrait;
use Drupal\rules\Engine\RulesState;
use Drupal\rules\Context\DataProcessorManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides an executable action expression.
*
* This plugin is used to wrap action plugins and is responsible to setup all
* the context necessary, instantiate the action plugin and to execute it.
*
* @Expression(
* id = "rules_action",
* label = @Translation("An executable action.")
* )
*/
class RulesAction extends RulesActionBase implements ContainerFactoryPluginInterface, ActionExpressionInterface {
use ExpressionTrait;
use ContextHandlerTrait;
/**
* The action manager used to instantiate the action plugin.
*
* @var \Drupal\Core\Action\ActionManager
*/
protected $actionManager;
/**
* Constructs a new class instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* Contains the following entries:
* - action_id: The action plugin ID.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Action\ActionManager $action_manager
* The action manager.
* @param \Drupal\rules\Context\DataProcessorManager $processor_manager
* The data processor plugin manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ActionManager $action_manager, DataProcessorManager $processor_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->actionManager = $action_manager;
$this->processorManager = $processor_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition,
$container->get('plugin.manager.action'),
$container->get('plugin.manager.data_processor')
);
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
// If the plugin id has been set already, keep it if not specified.
if (isset($this->configuration['action_id'])) {
$configuration += [
'action_id' => $this->configuration['action_id']
];
}
return parent::setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function executeWithState(RulesState $state) {
$action = $this->actionManager->createInstance($this->configuration['action_id']);
// We have to forward the context values from our configuration to the
// action plugin.
$this->mapContext($action, $state);
// Send the context value through configured data processor before executing
// the action.
$this->processData($action);
$action->execute();
$auto_saves = $action->autoSaveContext();
foreach ($auto_saves as $context_name) {
// Mark parameter contexts for auto saving in the Rules state.
$state->saveChangesLater($this->configuration['context_mapping'][$context_name]);
}
// Now that the action has been executed it can provide additional
// context which we will have to pass back in the evaluation state.
$this->mapProvidedContext($action, $state);
}
/**
* {@inheritdoc}
*/
public function getContextDefinitions() {
// Pass up the context definitions from the action plugin.
$definition = $this->actionManager->getDefinition($this->configuration['action_id']);
return !empty($definition['context']) ? $definition['context'] : [];
}
/**
* {@inheritdoc}
*/
public function getContextDefinition($name) {
// Pass up the context definitions from the action plugin.
$definition = $this->actionManager->getDefinition($this->configuration['action_id']);
if (empty($definition['context'][$name])) {
throw new ContextException(sprintf("The %s context is not a valid context.", $name));
}
return $definition['context'][$name];
}
}