-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathActionContainerForm.php
More file actions
152 lines (130 loc) · 4.16 KB
/
ActionContainerForm.php
File metadata and controls
152 lines (130 loc) · 4.16 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
146
147
148
149
150
151
152
<?php
namespace Drupal\rules\Form\Expression;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\rules\Ui\RulesUiHandlerTrait;
use Drupal\rules\Engine\ActionExpressionContainerInterface;
/**
* Form handler for action containers.
*/
class ActionContainerForm implements ExpressionFormInterface {
use StringTranslationTrait;
use RulesUiHandlerTrait;
use ExpressionFormTrait;
/**
* The rule expression object this form is for.
*
* @var \Drupal\rules\Engine\ActionExpressionContainerInterface
*/
protected $actionSet;
/**
* Creates a new object of this class.
*/
public function __construct(ActionExpressionContainerInterface $action_set) {
$this->actionSet = $action_set;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form['action_table_container'] = [
'#type' => 'container',
];
$form['action_table_container']['action_table'] = [
'#type' => 'table',
'#header' => [
$this->t('Elements'),
$this->t('Weight'),
[
'data' => $this->t('Operations'),
'colspan' => 3,
],
],
'#attributes' => [
'id' => 'rules_actions_table',
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'action-weight',
],
],
];
$form['action_table']['table']['#empty'] = $this->t('None');
// Get hold of actions.
// @todo See if we can add getExpressions method of ExpressionContainerBase.
$actions = [];
foreach ($this->actionSet as $action) {
$actions[] = $action;
}
// Sort actions by weight.
@uasort($actions, [$this->actionSet, 'expressionSortHelper']);
foreach ($actions as $action) {
/* @var $action \Drupal\rules\Engine\ExpressionInterface */
$uuid = $action->getUuid();
$row = &$form['action_table_container']['action_table'][$uuid];
// TableDrag: Mark the table row as draggable.
$row['#attributes']['class'][] = 'draggable';
// TableDrag: Sort the table row according to its existing weight.
$row['#weight'] = $action->getWeight();
$row['title'] = ['#markup' => $action->getLabel()];
$row['weight'] = [
'#type' => 'weight',
'#delta' => 50,
'#default_value' => $action->getWeight(),
'#attributes' => ['class' => ['action-weight']],
];
// Operations column.
$rules_ui_handler = $this->getRulesUiHandler();
$row['operations'] = [
'data' => [
'#type' => 'operations',
'#links' => [
'edit' => [
'title' => $this->t('Edit'),
'url' => $rules_ui_handler->getUrlFromRoute('expression.edit', [
'uuid' => $uuid,
]),
],
'delete' => [
'title' => $this->t('Delete'),
'url' => $rules_ui_handler->getUrlFromRoute('expression.delete', [
'uuid' => $uuid,
]),
],
],
],
];
}
// @todo Put this into the table as last row and style it like it was in
// Drupal 7 Rules.
$form['add_action'] = [
'#theme' => 'menu_local_action',
'#link' => [
'title' => $this->t('Add action'),
'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.add', [
'expression_id' => 'rules_action',
]),
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValue('action_table');
$component = $this->getRulesUiHandler()->getComponent();
/* @var $rule_expression \Drupal\rules\Plugin\RulesExpression\Rule */
$rule_expression = $component->getExpression();
if ($values) {
foreach ($values as $uuid => $expression) {
$action = $rule_expression->getExpression($uuid);
$action->setWeight($expression['weight']);
$action->setConfiguration($action->getConfiguration());
}
}
$this->getRulesUiHandler()->updateComponent($component);
}
}