-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathEditExpressionForm.php
More file actions
145 lines (123 loc) · 4.56 KB
/
EditExpressionForm.php
File metadata and controls
145 lines (123 loc) · 4.56 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
139
140
141
142
143
144
145
<?php
namespace Drupal\rules\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rules\Ui\RulesUiHandlerInterface;
use Drupal\rules\Engine\RulesComponent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* UI form to edit an expression like a condition or action in a rule.
*/
class EditExpressionForm extends FormBase {
/**
* The edited component.
*
* @var \Drupal\rules\Engine\RulesComponent
*/
protected $component;
/**
* The RulesUI handler of the currently active UI.
*
* @var \Drupal\rules\Ui\RulesUiHandlerInterface
*/
protected $rulesUiHandler;
/**
* The UUID of the edited expression in the rule.
*
* @var string
*/
protected $uuid;
/**
* Gets the currently edited expression from the given component.
*
* @param \Drupal\rules\Engine\RulesComponent $component
* The component from which to get the expression.
*
* @return \Drupal\rules\Engine\ExpressionInterface|null
* The expression object.
*/
protected function getEditedExpression(RulesComponent $component) {
$rule_expression = $component->getExpression();
return $rule_expression->getExpression($this->uuid);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, RulesUiHandlerInterface $rules_ui_handler = NULL, $uuid = NULL) {
$this->rulesUiHandler = $rules_ui_handler;
$this->component = is_object($form_state->get('component')) ? $form_state->get('component') : $this->rulesUiHandler->getComponent();
$this->uuid = $form_state->get('uuid') ?: $uuid;
// During form rebuilds, keep track of changes using form state.
$form_state->set('rules_ui_handler', $this->rulesUiHandler);
$form_state->set('component', $this->component);
$form_state->set('uuid', $this->uuid);
$expression = $this->getEditedExpression($this->component, $form_state);
if (!$expression) {
throw new NotFoundHttpException();
}
$form_handler = $expression->getFormHandler();
$form = $form_handler->form($form, $form_state, ['init' => TRUE]);
return $form;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'rules_expression_edit';
}
/**
* Builds an updated component object based upon the submitted form values.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return \Drupal\rules\Engine\RulesComponent
* The updated component.
*/
protected function buildComponent(array $form, FormStateInterface $form_state) {
$component = clone $this->component;
// In order to update the whole component we need to invoke the submission
// handler of the expression form. That way the expression gets changed
// accordingly.
$expression = $this->getEditedExpression($component);
$form_handler = $expression->getFormHandler();
$form_handler->submitForm($form, $form_state);
return $component;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Ensure the object properties are initialized, see
// https://www.drupal.org/node/2669032.
$this->rulesUiHandler = $form_state->get('rules_ui_handler');
$this->component = is_object($form_state->get('component')) ? $form_state->get('component') : $this->rulesUiHandler->getComponent();
$this->uuid = $form_state->get('uuid');
$this->rulesUiHandler->validateLock($form, $form_state);
// @todo: This ignores ExpressionFormInterface::validateForm().
$component = $this->buildComponent($form, $form_state);
$violations = $component->checkIntegrity();
// Only show the violations caused by the edited expression.
foreach ($violations->getFor($this->uuid) as $violation) {
$form_state->setError($form, $violation->getMessage());
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->component = $this->buildComponent($form, $form_state);
$this->rulesUiHandler->updateComponent($this->component);
$form_state->setRedirectUrl($this->rulesUiHandler->getBaseRouteUrl());
}
/**
* Provides the page title on the form.
*/
public function getTitle(RulesUiHandlerInterface $rules_ui_handler, $uuid) {
$this->uuid = $uuid;
$expression = $this->getEditedExpression($rules_ui_handler->getComponent());
return $this->t('Edit @expression', ['@expression' => $expression->getLabel()]);
}
}