Skip to content

Commit d58ca75

Browse files
author
Daniel Sasser
committed
#2799361 - Add drag and drop to actions and conditions from #2648334.
1 parent bff3e8d commit d58ca75

File tree

11 files changed

+316
-43
lines changed

11 files changed

+316
-43
lines changed

config/schema/rules.expression.schema.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ rules_expression:
77
uuid:
88
type: string
99
label: 'UUID'
10+
weight:
11+
type: integer
12+
label: 'Weight'
1013

1114
rules_expression.rules_condition:
1215
type: rules_expression
@@ -18,6 +21,9 @@ rules_expression.rules_condition:
1821
uuid:
1922
type: string
2023
label: 'UUID'
24+
weight:
25+
type: integer
26+
label: 'Weight'
2127
condition_id:
2228
type: string
2329
label: 'Condition plugin ID'
@@ -47,6 +53,9 @@ rules_expression.rules_action:
4753
uuid:
4854
type: string
4955
label: 'UUID'
56+
weight:
57+
type: integer
58+
label: 'Weight'
5059
action_id:
5160
type: string
5261
label: 'Action plugin ID'
@@ -73,6 +82,9 @@ rules_expression.rules_and:
7382
uuid:
7483
type: string
7584
label: 'UUID'
85+
weight:
86+
type: integer
87+
label: 'Weight'
7688
negate:
7789
type: boolean
7890
label: 'Negate'
@@ -92,6 +104,9 @@ rules_expression.rules_action_set:
92104
uuid:
93105
type: string
94106
label: 'UUID'
107+
weight:
108+
type: integer
109+
label: 'Weight'
95110
actions:
96111
type: sequence
97112
label: 'Actions'
@@ -108,6 +123,9 @@ rules_expression.rules_rule:
108123
uuid:
109124
type: string
110125
label: 'UUID'
126+
weight:
127+
type: integer
128+
label: 'Weight'
111129
conditions:
112130
type: rules_expression.[id]
113131
label: 'Conditions'

src/Engine/ExpressionBase.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ abstract class ExpressionBase extends PluginBase implements ExpressionInterface
3737
*/
3838
protected $uuid;
3939

40+
/**
41+
* The weight of this expression.
42+
*
43+
* @var integer
44+
*/
45+
protected $weight;
46+
4047
/**
4148
* Constructor.
4249
*
@@ -71,6 +78,7 @@ public function getConfiguration() {
7178
return [
7279
'id' => $this->getPluginId(),
7380
'uuid' => $this->uuid,
81+
'weight' => $this->weight,
7482
] + $this->configuration;
7583
}
7684

@@ -82,6 +90,13 @@ public function setConfiguration(array $configuration) {
8290
if (isset($configuration['uuid'])) {
8391
$this->uuid = $configuration['uuid'];
8492
}
93+
if (isset($configuration['weight'])) {
94+
$this->weight = $configuration['weight'];
95+
}
96+
else {
97+
$this->weight = 0;
98+
}
99+
85100
return $this;
86101
}
87102

@@ -148,4 +163,31 @@ public function setUuid($uuid) {
148163
$this->uuid = $uuid;
149164
}
150165

166+
/**
167+
* {@inheritdoc}
168+
*/
169+
public function getWeight() {
170+
return $this->weight;
171+
}
172+
173+
/**
174+
* {@inheritdoc}
175+
*/
176+
public function setWeight($weight) {
177+
$this->weight = $weight;
178+
}
179+
180+
/**
181+
* {@inheritdoc}
182+
*/
183+
public function expressionSortHelper(ExpressionInterface $a, ExpressionInterface $b) {
184+
$a_weight = $a->getWeight();
185+
$b_weight = $b->getWeight();
186+
if ($a_weight == $b_weight) {
187+
return 0;
188+
}
189+
190+
return ($a_weight < $b_weight) ? -1 : 1;
191+
}
192+
151193
}

src/Engine/ExpressionInterface.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,37 @@ public function getUuid();
7979
*/
8080
public function setUuid($uuid);
8181

82+
/**
83+
* Returns the weight of this expression.
84+
*
85+
* @return int
86+
* The weight of this expression.
87+
*/
88+
public function getWeight();
89+
90+
/**
91+
* Sets the weight of this expression in an expression tree.
92+
*
93+
* @param int $weight
94+
* The weight to set.
95+
*/
96+
public function setWeight($weight);
97+
98+
/**
99+
* Sorts an array of expressions by 'weight' property.
100+
*
101+
* Callback for uasort().
102+
*
103+
* @param \Drupal\rules\Engine\ExpressionInterface $a
104+
* First item for comparison.
105+
* @param \Drupal\rules\Engine\ExpressionInterface $b
106+
* Second item for comparison.
107+
*
108+
* @return int
109+
* The comparison result for uasort().
110+
*/
111+
public function expressionSortHelper(ExpressionInterface $a, ExpressionInterface $b);
112+
82113
/**
83114
* Verifies that this expression is configured correctly.
84115
*

src/Form/Expression/ActionContainerForm.php

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,75 @@ public function form(array $form, FormStateInterface $form_state) {
3939
];
4040

4141
$form['action_table']['table'] = [
42-
'#theme' => 'table',
43-
'#caption' => $this->t('Actions'),
44-
'#header' => [$this->t('Elements'), $this->t('Operations')],
45-
'#empty' => t('None'),
42+
'#type' => 'table',
43+
'#header' => [
44+
$this->t('Elements'),
45+
$this->t('Weight'),
46+
[
47+
'data' => $this->t('Operations'),
48+
'colspan' => 3,
49+
],
50+
],
51+
'#attributes' => [
52+
'id' => 'rules_actions_table',
53+
],
54+
'#tabledrag' => [
55+
[
56+
'action' => 'order',
57+
'relationship' => 'sibling',
58+
'group' => 'action-weight',
59+
],
60+
],
4661
];
4762

63+
$form['action_table']['table']['#empty'] = $this->t('None');
64+
65+
// Get hold of actions.
66+
// @todo See if we can add getExpressions method of ExpressionContainerBase.
67+
$actions = [];
4868
foreach ($this->actionSet as $action) {
49-
$form['action_table']['table']['#rows'][] = [
50-
'element' => $action->getLabel(),
51-
'operations' => [
52-
'data' => [
53-
'#type' => 'dropbutton',
54-
'#links' => [
55-
'edit' => [
56-
'title' => $this->t('Edit'),
57-
'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.edit', [
58-
'uuid' => $action->getUuid(),
59-
]),
60-
],
61-
'delete' => [
62-
'title' => $this->t('Delete'),
63-
'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.delete', [
64-
'uuid' => $action->getUuid(),
65-
]),
66-
],
69+
$actions[] = $action;
70+
}
71+
72+
// Sort actions by weight.
73+
@uasort($actions, [$this->actionSet, 'expressionSortHelper']);
74+
75+
foreach ($actions as $action) {
76+
/* @var $action \Drupal\rules\Engine\ExpressionInterface */
77+
$uuid = $action->getUuid();
78+
$row = &$form['action_table']['table'][$uuid];
79+
80+
// TableDrag: Mark the table row as draggable.
81+
$row['#attributes']['class'][] = 'draggable';
82+
83+
// TableDrag: Sort the table row according to its existing weight.
84+
$row['#weight'] = $action->getWeight();
85+
$row['title'] = ['#markup' => $action->getLabel()];
86+
87+
$row['weight'] = [
88+
'#type' => 'weight',
89+
'#delta' => 50,
90+
'#default_value' => $action->getWeight(),
91+
'#attributes' => ['class' => ['action-weight']],
92+
];
93+
94+
// Operations (dropbutton) column.
95+
$rules_ui_handler = $this->getRulesUiHandler();
96+
$row['operations'] = [
97+
'data' => [
98+
'#type' => 'dropbutton',
99+
'#links' => [
100+
'edit' => [
101+
'title' => $this->t('Edit'),
102+
'url' => $rules_ui_handler->getUrlFromRoute('expression.edit', [
103+
'uuid' => $uuid,
104+
]),
105+
],
106+
'delete' => [
107+
'title' => $this->t('Delete'),
108+
'url' => $rules_ui_handler->getUrlFromRoute('expression.delete', [
109+
'uuid' => $uuid,
110+
]),
67111
],
68112
],
69113
],
@@ -85,4 +129,24 @@ public function form(array $form, FormStateInterface $form_state) {
85129
return $form;
86130
}
87131

132+
/**
133+
* {@inheritdoc}
134+
*/
135+
public function submitForm(array &$form, FormStateInterface $form_state) {
136+
$values = $form_state->getValue('table');
137+
$component = $this->getRulesUiHandler()->getComponent();
138+
/* @var $rule_expression \Drupal\rules\Plugin\RulesExpression\Rule */
139+
$rule_expression = $component->getExpression();
140+
141+
if ($values) {
142+
foreach ($values as $uuid => $expression) {
143+
$action = $rule_expression->getExpression($uuid);
144+
$action->setWeight($expression['weight']);
145+
$action->setConfiguration($action->getConfiguration());
146+
}
147+
}
148+
149+
$this->getRulesUiHandler()->updateComponent($component);
150+
}
151+
88152
}

0 commit comments

Comments
 (0)