Skip to content

Commit

Permalink
#2799361 - Add drag and drop to actions and conditions from #2648334.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Sasser committed Oct 4, 2016
1 parent bff3e8d commit d58ca75
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 43 deletions.
18 changes: 18 additions & 0 deletions config/schema/rules.expression.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ rules_expression:
uuid:
type: string
label: 'UUID'
weight:
type: integer
label: 'Weight'

rules_expression.rules_condition:
type: rules_expression
Expand All @@ -18,6 +21,9 @@ rules_expression.rules_condition:
uuid:
type: string
label: 'UUID'
weight:
type: integer
label: 'Weight'
condition_id:
type: string
label: 'Condition plugin ID'
Expand Down Expand Up @@ -47,6 +53,9 @@ rules_expression.rules_action:
uuid:
type: string
label: 'UUID'
weight:
type: integer
label: 'Weight'
action_id:
type: string
label: 'Action plugin ID'
Expand All @@ -73,6 +82,9 @@ rules_expression.rules_and:
uuid:
type: string
label: 'UUID'
weight:
type: integer
label: 'Weight'
negate:
type: boolean
label: 'Negate'
Expand All @@ -92,6 +104,9 @@ rules_expression.rules_action_set:
uuid:
type: string
label: 'UUID'
weight:
type: integer
label: 'Weight'
actions:
type: sequence
label: 'Actions'
Expand All @@ -108,6 +123,9 @@ rules_expression.rules_rule:
uuid:
type: string
label: 'UUID'
weight:
type: integer
label: 'Weight'
conditions:
type: rules_expression.[id]
label: 'Conditions'
Expand Down
42 changes: 42 additions & 0 deletions src/Engine/ExpressionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ abstract class ExpressionBase extends PluginBase implements ExpressionInterface
*/
protected $uuid;

/**
* The weight of this expression.
*
* @var integer
*/
protected $weight;

/**
* Constructor.
*
Expand Down Expand Up @@ -71,6 +78,7 @@ public function getConfiguration() {
return [
'id' => $this->getPluginId(),
'uuid' => $this->uuid,
'weight' => $this->weight,
] + $this->configuration;
}

Expand All @@ -82,6 +90,13 @@ public function setConfiguration(array $configuration) {
if (isset($configuration['uuid'])) {
$this->uuid = $configuration['uuid'];
}
if (isset($configuration['weight'])) {
$this->weight = $configuration['weight'];
}
else {
$this->weight = 0;
}

return $this;
}

Expand Down Expand Up @@ -148,4 +163,31 @@ public function setUuid($uuid) {
$this->uuid = $uuid;
}

/**
* {@inheritdoc}
*/
public function getWeight() {
return $this->weight;
}

/**
* {@inheritdoc}
*/
public function setWeight($weight) {
$this->weight = $weight;
}

/**
* {@inheritdoc}
*/
public function expressionSortHelper(ExpressionInterface $a, ExpressionInterface $b) {
$a_weight = $a->getWeight();
$b_weight = $b->getWeight();
if ($a_weight == $b_weight) {
return 0;
}

return ($a_weight < $b_weight) ? -1 : 1;
}

}
31 changes: 31 additions & 0 deletions src/Engine/ExpressionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@ public function getUuid();
*/
public function setUuid($uuid);

/**
* Returns the weight of this expression.
*
* @return int
* The weight of this expression.
*/
public function getWeight();

/**
* Sets the weight of this expression in an expression tree.
*
* @param int $weight
* The weight to set.
*/
public function setWeight($weight);

/**
* Sorts an array of expressions by 'weight' property.
*
* Callback for uasort().
*
* @param \Drupal\rules\Engine\ExpressionInterface $a
* First item for comparison.
* @param \Drupal\rules\Engine\ExpressionInterface $b
* Second item for comparison.
*
* @return int
* The comparison result for uasort().
*/
public function expressionSortHelper(ExpressionInterface $a, ExpressionInterface $b);

/**
* Verifies that this expression is configured correctly.
*
Expand Down
108 changes: 86 additions & 22 deletions src/Form/Expression/ActionContainerForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,75 @@ public function form(array $form, FormStateInterface $form_state) {
];

$form['action_table']['table'] = [
'#theme' => 'table',
'#caption' => $this->t('Actions'),
'#header' => [$this->t('Elements'), $this->t('Operations')],
'#empty' => t('None'),
'#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) {
$form['action_table']['table']['#rows'][] = [
'element' => $action->getLabel(),
'operations' => [
'data' => [
'#type' => 'dropbutton',
'#links' => [
'edit' => [
'title' => $this->t('Edit'),
'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.edit', [
'uuid' => $action->getUuid(),
]),
],
'delete' => [
'title' => $this->t('Delete'),
'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.delete', [
'uuid' => $action->getUuid(),
]),
],
$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']['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 (dropbutton) column.
$rules_ui_handler = $this->getRulesUiHandler();
$row['operations'] = [
'data' => [
'#type' => 'dropbutton',
'#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,
]),
],
],
],
Expand All @@ -85,4 +129,24 @@ public function form(array $form, FormStateInterface $form_state) {
return $form;
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValue('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);
}

}
Loading

0 comments on commit d58ca75

Please sign in to comment.