From fbab3a9851900fa7ef8d0a74ca6a235152aa44f2 Mon Sep 17 00:00:00 2001 From: yanniboi Date: Fri, 13 May 2016 19:16:53 -0400 Subject: [PATCH 1/6] Issue #2648334 by yanniboi: Made a start at drag and drop reordering of rule actions. --- config/schema/rules.expression.schema.yml | 3 + src/Engine/ExpressionBase.php | 29 +++++++ src/Engine/ExpressionInterface.php | 16 ++++ src/Form/Expression/ActionContainerForm.php | 96 ++++++++++++++++----- src/Form/Expression/RuleForm.php | 9 ++ 5 files changed, 132 insertions(+), 21 deletions(-) diff --git a/config/schema/rules.expression.schema.yml b/config/schema/rules.expression.schema.yml index ab6d4068..ad9c328c 100644 --- a/config/schema/rules.expression.schema.yml +++ b/config/schema/rules.expression.schema.yml @@ -47,6 +47,9 @@ rules_expression.rules_action: uuid: type: string label: 'UUID' + weight: + type: integer + label: 'Weight' action_id: type: string label: 'Action plugin ID' diff --git a/src/Engine/ExpressionBase.php b/src/Engine/ExpressionBase.php index 7eb6d295..e4ecb3db 100644 --- a/src/Engine/ExpressionBase.php +++ b/src/Engine/ExpressionBase.php @@ -37,6 +37,13 @@ abstract class ExpressionBase extends PluginBase implements ExpressionInterface */ protected $uuid; + /** + * The weight of this expression. + * + * @var integer + */ + protected $weight; + /** * Constructor. * @@ -71,6 +78,7 @@ public function getConfiguration() { return [ 'id' => $this->getPluginId(), 'uuid' => $this->uuid, + 'weight' => $this->weight, ] + $this->configuration; } @@ -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; } @@ -148,4 +163,18 @@ public function setUuid($uuid) { $this->uuid = $uuid; } + /** + * {@inheritdoc} + */ + public function getWeight() { + return $this->weight; + } + + /** + * {@inheritdoc} + */ + public function setWeight($weight) { + $this->weight = $weight; + } + } diff --git a/src/Engine/ExpressionInterface.php b/src/Engine/ExpressionInterface.php index 89daff48..0711e122 100644 --- a/src/Engine/ExpressionInterface.php +++ b/src/Engine/ExpressionInterface.php @@ -79,6 +79,22 @@ public function getUuid(); */ public function setUuid($uuid); + /** + * Returns the weight of this expression if it is nested in another expression. + * + * @return integer + * The weight if this expression is nested. + */ + public function getWeight(); + + /** + * Sets the weight of this expression in an expression tree. + * + * @param integer $weight + * The weight to set. + */ + public function setWeight($weight); + /** * Verifies that this expression is configured correctly. * diff --git a/src/Form/Expression/ActionContainerForm.php b/src/Form/Expression/ActionContainerForm.php index 254793ba..6d94feee 100644 --- a/src/Form/Expression/ActionContainerForm.php +++ b/src/Form/Expression/ActionContainerForm.php @@ -39,35 +39,71 @@ public function form(array $form, FormStateInterface $form_state) { ]; $form['action_table']['table'] = [ + '#type' => 'table', '#theme' => 'table', - '#caption' => $this->t('Actions'), - '#header' => [$this->t('Elements'), $this->t('Operations')], - '#empty' => t('None'), + '#header' => [ + $this->t('Elements'), + $this->t('Weight'), + [ + 'data' => $this->t('Operations'), + 'colspan' => 3, + ], + ], + '#tabledrag' => [ + [ + 'action' => 'order', + 'relationship' => 'sibling', + 'group' => 'action-weight', + 'limit' => 1, + ], + ], ]; + $form['action_table']['table']['#empty'] = $this->t('None'); 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(), - ]), - ], + $uuid = $action->getUuid(); + $form['action_table']['table'][$uuid]['#item'] = $action; + + // TableDrag: Mark the table row as draggable. + $form['action_table']['table'][$uuid]['#attributes']['class'][] = 'draggable'; + + // TableDrag: Sort the table row according to its existing/configured weight. + $form['action_table']['table'][$uuid]['#weight'] = $action->getWeight(); + $form['action_table']['table'][$uuid]['title'] = ['#markup' => $action->getLabel()]; + + $form['action_table']['table'][$uuid]['weight'] = [ + '#type' => 'weight', + '#delta' => 50, + '#default_value' => 0, + '#attributes' => ['class' => ['action-weight']] + ]; + + // Operations (dropbutton) column. + $form['action_table']['table'][$uuid]['operations'] = [ + 'data' => [ + '#type' => 'dropbutton', + '#links' => [ + 'edit' => [ + 'title' => $this->t('Edit'), + 'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.edit', [ + 'uuid' => $uuid, + ]), + ], + 'delete' => [ + 'title' => $this->t('Delete'), + 'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.delete', [ + 'uuid' => $uuid, + ]), ], ], ], ]; + + $form['action_table']['table'][$uuid]['id'] = [ + '#type' => 'hidden', + '#value' => $uuid, + '#attributes' => ['class' => ['action-id']] + ]; } // @todo Put this into the table as last row and style it like it was in @@ -85,4 +121,22 @@ 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(); + + foreach ($values as $uuid => $expression) { + $action = $rule_expression->getExpression($uuid); + $action->setWeight($expression['weight']); + $action->setConfiguration($action->getConfiguration()); + } + + $this->getRulesUiHandler()->updateComponent($component); + } + } diff --git a/src/Form/Expression/RuleForm.php b/src/Form/Expression/RuleForm.php index 49c1635f..04dab37b 100644 --- a/src/Form/Expression/RuleForm.php +++ b/src/Form/Expression/RuleForm.php @@ -41,4 +41,13 @@ public function form(array $form, FormStateInterface $form_state) { return $form; } + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // @todo implement drag and drop for conditions. + //$this->rule->getConditions()->getFormHandler()->submitForm($form, $form_state); + $this->rule->getActions()->getFormHandler()->submitForm($form, $form_state); + } + } From d47be107b681dfa62e2ae8d8ee824bb298de8094 Mon Sep 17 00:00:00 2001 From: yanniboi Date: Sat, 14 May 2016 11:17:08 -0400 Subject: [PATCH 2/6] Issue #2648334 by yanniboi: Working sortable actions by weight. --- src/Engine/ExpressionBase.php | 13 +++++++++++ src/Form/Expression/ActionContainerForm.php | 25 ++++++++++++--------- src/Form/ReactionRuleEditForm.php | 2 ++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/Engine/ExpressionBase.php b/src/Engine/ExpressionBase.php index e4ecb3db..0b5ba760 100644 --- a/src/Engine/ExpressionBase.php +++ b/src/Engine/ExpressionBase.php @@ -177,4 +177,17 @@ public function setWeight($weight) { $this->weight = $weight; } + /** + * {@inheritdoc} + */ + public function expressionSortHelper($a, $b) { + $a_weight = $a->getWeight(); + $b_weight = $b->getWeight(); + if ($a_weight == $b_weight) { + return 0; + } + + return ($a_weight < $b_weight) ? -1 : 1; + } + } diff --git a/src/Form/Expression/ActionContainerForm.php b/src/Form/Expression/ActionContainerForm.php index 6d94feee..7b0af854 100644 --- a/src/Form/Expression/ActionContainerForm.php +++ b/src/Form/Expression/ActionContainerForm.php @@ -40,7 +40,6 @@ public function form(array $form, FormStateInterface $form_state) { $form['action_table']['table'] = [ '#type' => 'table', - '#theme' => 'table', '#header' => [ $this->t('Elements'), $this->t('Weight'), @@ -49,20 +48,32 @@ public function form(array $form, FormStateInterface $form_state) { 'colspan' => 3, ], ], + '#attributes' => [ + 'id' => 'rules_actions_table' + ], '#tabledrag' => [ [ 'action' => 'order', 'relationship' => 'sibling', 'group' => 'action-weight', - 'limit' => 1, ], ], ]; $form['action_table']['table']['#empty'] = $this->t('None'); + + // Get hold of actions. + // @todo See if we can have a 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) { $uuid = $action->getUuid(); - $form['action_table']['table'][$uuid]['#item'] = $action; // TableDrag: Mark the table row as draggable. $form['action_table']['table'][$uuid]['#attributes']['class'][] = 'draggable'; @@ -74,7 +85,7 @@ public function form(array $form, FormStateInterface $form_state) { $form['action_table']['table'][$uuid]['weight'] = [ '#type' => 'weight', '#delta' => 50, - '#default_value' => 0, + '#default_value' => $action->getWeight(), '#attributes' => ['class' => ['action-weight']] ]; @@ -98,12 +109,6 @@ public function form(array $form, FormStateInterface $form_state) { ], ], ]; - - $form['action_table']['table'][$uuid]['id'] = [ - '#type' => 'hidden', - '#value' => $uuid, - '#attributes' => ['class' => ['action-id']] - ]; } // @todo Put this into the table as last row and style it like it was in diff --git a/src/Form/ReactionRuleEditForm.php b/src/Form/ReactionRuleEditForm.php index 1ef7235a..efae6581 100644 --- a/src/Form/ReactionRuleEditForm.php +++ b/src/Form/ReactionRuleEditForm.php @@ -113,6 +113,8 @@ protected function actions(array $form, FormStateInterface $form_state) { */ public function save(array $form, FormStateInterface $form_state) { $this->rulesUiHandler->getForm()->submitForm($form, $form_state); + $component = $this->rulesUiHandler->getComponent(); + $this->entity->updateFromComponent($component); // Persist changes by saving the entity. parent::save($form, $form_state); From 841f768d7d1a7c71bb6ac25214ae3b413d54e5e0 Mon Sep 17 00:00:00 2001 From: yanniboi Date: Sat, 14 May 2016 11:36:15 -0400 Subject: [PATCH 3/6] Issue #2648334 by yanniboi: Working sortable conditions by weight. --- src/Engine/ExpressionInterface.php | 15 +++ src/Form/Expression/ActionContainerForm.php | 1 + .../Expression/ConditionContainerForm.php | 103 ++++++++++++++---- src/Form/Expression/RuleForm.php | 3 +- 4 files changed, 99 insertions(+), 23 deletions(-) diff --git a/src/Engine/ExpressionInterface.php b/src/Engine/ExpressionInterface.php index 0711e122..dca750af 100644 --- a/src/Engine/ExpressionInterface.php +++ b/src/Engine/ExpressionInterface.php @@ -95,6 +95,21 @@ public function getWeight(); */ 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($a, $b); + /** * Verifies that this expression is configured correctly. * diff --git a/src/Form/Expression/ActionContainerForm.php b/src/Form/Expression/ActionContainerForm.php index 7b0af854..65d7dde2 100644 --- a/src/Form/Expression/ActionContainerForm.php +++ b/src/Form/Expression/ActionContainerForm.php @@ -73,6 +73,7 @@ public function form(array $form, FormStateInterface $form_state) { @uasort($actions, [$this->actionSet, 'expressionSortHelper']); foreach ($actions as $action) { + /* @var $action \Drupal\rules\Engine\ExpressionInterface */ $uuid = $action->getUuid(); // TableDrag: Mark the table row as draggable. diff --git a/src/Form/Expression/ConditionContainerForm.php b/src/Form/Expression/ConditionContainerForm.php index 6a23fbba..d18ef250 100644 --- a/src/Form/Expression/ConditionContainerForm.php +++ b/src/Form/Expression/ConditionContainerForm.php @@ -39,31 +39,74 @@ public function form(array $form, FormStateInterface $form_state) { ]; $form['conditions']['table'] = [ - '#theme' => 'table', + '#type' => 'table', '#caption' => $this->t('Conditions'), - '#header' => [$this->t('Elements'), $this->t('Operations')], - '#empty' => t('None'), + '#header' => [ + $this->t('Elements'), + $this->t('Weight'), + [ + 'data' => $this->t('Operations'), + 'colspan' => 3, + ], + ], + '#attributes' => [ + 'id' => 'rules_conditions_table' + ], + '#tabledrag' => [ + [ + 'action' => 'order', + 'relationship' => 'sibling', + 'group' => 'condition-weight', + ], + ], ]; + $form['conditions']['table']['#empty'] = $this->t('None'); + + // Get hold of conditions. + // @todo See if we can have a getExpressions method of ExpressionContainerBase. + $conditions = []; foreach ($this->conditionContainer as $condition) { - $form['conditions']['table']['#rows'][] = [ - 'element' => $condition->getLabel(), - 'operations' => [ - 'data' => [ - '#type' => 'dropbutton', - '#links' => [ - 'edit' => [ - 'title' => $this->t('Edit'), - 'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.edit', [ - 'uuid' => $condition->getUuid(), - ]), - ], - 'delete' => [ - 'title' => $this->t('Delete'), - 'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.delete', [ - 'uuid' => $condition->getUuid(), - ]), - ], + $conditions[] = $condition; + } + + // Sort conditions by weight. + @uasort($conditions, [$this->conditionContainer, 'expressionSortHelper']); + + foreach ($conditions as $condition) { + /* @var $condition \Drupal\rules\Engine\ExpressionInterface */ + $uuid = $condition->getUuid(); + + // TableDrag: Mark the table row as draggable. + $form['conditions']['table'][$uuid]['#attributes']['class'][] = 'draggable'; + + // TableDrag: Sort the table row according to its existing/configured weight. + $form['conditions']['table'][$uuid]['#weight'] = $condition->getWeight(); + $form['conditions']['table'][$uuid]['title'] = ['#markup' => $condition->getLabel()]; + + $form['conditions']['table'][$uuid]['weight'] = [ + '#type' => 'weight', + '#delta' => 50, + '#default_value' => $condition->getWeight(), + '#attributes' => ['class' => ['condition-weight']] + ]; + + // Operations (dropbutton) column. + $form['conditions']['table'][$uuid]['operations'] = [ + 'data' => [ + '#type' => 'dropbutton', + '#links' => [ + 'edit' => [ + 'title' => $this->t('Edit'), + 'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.edit', [ + 'uuid' => $condition->getUuid(), + ]), + ], + 'delete' => [ + 'title' => $this->t('Delete'), + 'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.delete', [ + 'uuid' => $condition->getUuid(), + ]), ], ], ], @@ -85,4 +128,22 @@ 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(); + + foreach ($values as $uuid => $expression) { + $action = $rule_expression->getExpression($uuid); + $action->setWeight($expression['weight']); + $action->setConfiguration($action->getConfiguration()); + } + + $this->getRulesUiHandler()->updateComponent($component); + } + } diff --git a/src/Form/Expression/RuleForm.php b/src/Form/Expression/RuleForm.php index 04dab37b..f060e9b4 100644 --- a/src/Form/Expression/RuleForm.php +++ b/src/Form/Expression/RuleForm.php @@ -45,8 +45,7 @@ public function form(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - // @todo implement drag and drop for conditions. - //$this->rule->getConditions()->getFormHandler()->submitForm($form, $form_state); + $this->rule->getConditions()->getFormHandler()->submitForm($form, $form_state); $this->rule->getActions()->getFormHandler()->submitForm($form, $form_state); } From 7ea6be9313c3689750947773e9cae90f119d53f5 Mon Sep 17 00:00:00 2001 From: yanniboi Date: Sat, 14 May 2016 11:46:37 -0400 Subject: [PATCH 4/6] Issue #2648334 by yanniboi: Tidied up code style. --- src/Engine/ExpressionInterface.php | 7 +++--- src/Form/Expression/ActionContainerForm.php | 22 ++++++++++--------- .../Expression/ConditionContainerForm.php | 20 +++++++++-------- src/Form/Expression/RuleForm.php | 6 +++-- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/Engine/ExpressionInterface.php b/src/Engine/ExpressionInterface.php index dca750af..e0b55865 100644 --- a/src/Engine/ExpressionInterface.php +++ b/src/Engine/ExpressionInterface.php @@ -80,9 +80,10 @@ public function getUuid(); public function setUuid($uuid); /** - * Returns the weight of this expression if it is nested in another expression. + * Returns the weight of this expression if it is nested in another + * expression. * - * @return integer + * @return int * The weight if this expression is nested. */ public function getWeight(); @@ -90,7 +91,7 @@ public function getWeight(); /** * Sets the weight of this expression in an expression tree. * - * @param integer $weight + * @param int $weight * The weight to set. */ public function setWeight($weight); diff --git a/src/Form/Expression/ActionContainerForm.php b/src/Form/Expression/ActionContainerForm.php index 65d7dde2..29c26a77 100644 --- a/src/Form/Expression/ActionContainerForm.php +++ b/src/Form/Expression/ActionContainerForm.php @@ -63,7 +63,7 @@ public function form(array $form, FormStateInterface $form_state) { $form['action_table']['table']['#empty'] = $this->t('None'); // Get hold of actions. - // @todo See if we can have a getExpressions method of ExpressionContainerBase. + // @todo See if we can add getExpressions method of ExpressionContainerBase. $actions = []; foreach ($this->actionSet as $action) { $actions[] = $action; @@ -75,35 +75,37 @@ public function form(array $form, FormStateInterface $form_state) { 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. - $form['action_table']['table'][$uuid]['#attributes']['class'][] = 'draggable'; + $row['#attributes']['class'][] = 'draggable'; - // TableDrag: Sort the table row according to its existing/configured weight. - $form['action_table']['table'][$uuid]['#weight'] = $action->getWeight(); - $form['action_table']['table'][$uuid]['title'] = ['#markup' => $action->getLabel()]; + // TableDrag: Sort the table row according to its existing weight. + $row['#weight'] = $action->getWeight(); + $row['title'] = ['#markup' => $action->getLabel()]; - $form['action_table']['table'][$uuid]['weight'] = [ + $row['weight'] = [ '#type' => 'weight', '#delta' => 50, '#default_value' => $action->getWeight(), - '#attributes' => ['class' => ['action-weight']] + '#attributes' => ['class' => ['action-weight']], ]; // Operations (dropbutton) column. - $form['action_table']['table'][$uuid]['operations'] = [ + $rules_ui_handler = $this->getRulesUiHandler(); + $row['operations'] = [ 'data' => [ '#type' => 'dropbutton', '#links' => [ 'edit' => [ 'title' => $this->t('Edit'), - 'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.edit', [ + 'url' => $rules_ui_handler->getUrlFromRoute('expression.edit', [ 'uuid' => $uuid, ]), ], 'delete' => [ 'title' => $this->t('Delete'), - 'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.delete', [ + 'url' => $rules_ui_handler->getUrlFromRoute('expression.delete', [ 'uuid' => $uuid, ]), ], diff --git a/src/Form/Expression/ConditionContainerForm.php b/src/Form/Expression/ConditionContainerForm.php index d18ef250..c30aa763 100644 --- a/src/Form/Expression/ConditionContainerForm.php +++ b/src/Form/Expression/ConditionContainerForm.php @@ -64,7 +64,7 @@ public function form(array $form, FormStateInterface $form_state) { $form['conditions']['table']['#empty'] = $this->t('None'); // Get hold of conditions. - // @todo See if we can have a getExpressions method of ExpressionContainerBase. + // @todo See if we can add getExpressions method of ExpressionContainerBase. $conditions = []; foreach ($this->conditionContainer as $condition) { $conditions[] = $condition; @@ -76,35 +76,37 @@ public function form(array $form, FormStateInterface $form_state) { foreach ($conditions as $condition) { /* @var $condition \Drupal\rules\Engine\ExpressionInterface */ $uuid = $condition->getUuid(); + $row = &$form['conditions']['table'][$uuid]; // TableDrag: Mark the table row as draggable. - $form['conditions']['table'][$uuid]['#attributes']['class'][] = 'draggable'; + $row['#attributes']['class'][] = 'draggable'; // TableDrag: Sort the table row according to its existing/configured weight. - $form['conditions']['table'][$uuid]['#weight'] = $condition->getWeight(); - $form['conditions']['table'][$uuid]['title'] = ['#markup' => $condition->getLabel()]; + $row['#weight'] = $condition->getWeight(); + $row['title'] = ['#markup' => $condition->getLabel()]; - $form['conditions']['table'][$uuid]['weight'] = [ + $row['weight'] = [ '#type' => 'weight', '#delta' => 50, '#default_value' => $condition->getWeight(), - '#attributes' => ['class' => ['condition-weight']] + '#attributes' => ['class' => ['condition-weight']], ]; // Operations (dropbutton) column. - $form['conditions']['table'][$uuid]['operations'] = [ + $rules_ui_handler = $this->getRulesUiHandler(); + $row['operations'] = [ 'data' => [ '#type' => 'dropbutton', '#links' => [ 'edit' => [ 'title' => $this->t('Edit'), - 'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.edit', [ + 'url' => $rules_ui_handler->getUrlFromRoute('expression.edit', [ 'uuid' => $condition->getUuid(), ]), ], 'delete' => [ 'title' => $this->t('Delete'), - 'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.delete', [ + 'url' => $rules_ui_handler->getUrlFromRoute('expression.delete', [ 'uuid' => $condition->getUuid(), ]), ], diff --git a/src/Form/Expression/RuleForm.php b/src/Form/Expression/RuleForm.php index f060e9b4..67cad998 100644 --- a/src/Form/Expression/RuleForm.php +++ b/src/Form/Expression/RuleForm.php @@ -45,8 +45,10 @@ public function form(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $this->rule->getConditions()->getFormHandler()->submitForm($form, $form_state); - $this->rule->getActions()->getFormHandler()->submitForm($form, $form_state); + $conditions = $this->rule->getConditions(); + $conditions->getFormHandler()->submitForm($form, $form_state); + $actions = $this->rule->getActions(); + $actions->getFormHandler()->submitForm($form, $form_state); } } From 418dad7fd8ecbfde4d3c0e60a3d58cb78143f019 Mon Sep 17 00:00:00 2001 From: yanniboi Date: Sat, 14 May 2016 19:31:26 -0400 Subject: [PATCH 5/6] Issue #2648334 by yanniboi: Added weight to expression schema and tidied up code style. --- config/schema/rules.expression.schema.yml | 15 +++++++++++++++ src/Engine/ExpressionBase.php | 2 +- src/Engine/ExpressionInterface.php | 7 +++---- src/Form/Expression/ActionContainerForm.php | 12 +++++++----- src/Form/Expression/ConditionContainerForm.php | 14 ++++++++------ 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/config/schema/rules.expression.schema.yml b/config/schema/rules.expression.schema.yml index ad9c328c..1af97121 100644 --- a/config/schema/rules.expression.schema.yml +++ b/config/schema/rules.expression.schema.yml @@ -7,6 +7,9 @@ rules_expression: uuid: type: string label: 'UUID' + weight: + type: integer + label: 'Weight' rules_expression.rules_condition: type: rules_expression @@ -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' @@ -76,6 +82,9 @@ rules_expression.rules_and: uuid: type: string label: 'UUID' + weight: + type: integer + label: 'Weight' negate: type: boolean label: 'Negate' @@ -95,6 +104,9 @@ rules_expression.rules_action_set: uuid: type: string label: 'UUID' + weight: + type: integer + label: 'Weight' actions: type: sequence label: 'Actions' @@ -111,6 +123,9 @@ rules_expression.rules_rule: uuid: type: string label: 'UUID' + weight: + type: integer + label: 'Weight' conditions: type: rules_expression.[id] label: 'Conditions' diff --git a/src/Engine/ExpressionBase.php b/src/Engine/ExpressionBase.php index 0b5ba760..3f2b0df4 100644 --- a/src/Engine/ExpressionBase.php +++ b/src/Engine/ExpressionBase.php @@ -180,7 +180,7 @@ public function setWeight($weight) { /** * {@inheritdoc} */ - public function expressionSortHelper($a, $b) { + public function expressionSortHelper(ExpressionInterface $a, ExpressionInterface $b) { $a_weight = $a->getWeight(); $b_weight = $b->getWeight(); if ($a_weight == $b_weight) { diff --git a/src/Engine/ExpressionInterface.php b/src/Engine/ExpressionInterface.php index e0b55865..e2f03488 100644 --- a/src/Engine/ExpressionInterface.php +++ b/src/Engine/ExpressionInterface.php @@ -80,11 +80,10 @@ public function getUuid(); public function setUuid($uuid); /** - * Returns the weight of this expression if it is nested in another - * expression. + * Returns the weight of this expression. * * @return int - * The weight if this expression is nested. + * The weight of this expression. */ public function getWeight(); @@ -109,7 +108,7 @@ public function setWeight($weight); * @return int * The comparison result for uasort(). */ - public function expressionSortHelper($a, $b); + public function expressionSortHelper(ExpressionInterface $a, ExpressionInterface $b); /** * Verifies that this expression is configured correctly. diff --git a/src/Form/Expression/ActionContainerForm.php b/src/Form/Expression/ActionContainerForm.php index 29c26a77..9eb0a69e 100644 --- a/src/Form/Expression/ActionContainerForm.php +++ b/src/Form/Expression/ActionContainerForm.php @@ -49,7 +49,7 @@ public function form(array $form, FormStateInterface $form_state) { ], ], '#attributes' => [ - 'id' => 'rules_actions_table' + 'id' => 'rules_actions_table', ], '#tabledrag' => [ [ @@ -138,10 +138,12 @@ public function submitForm(array &$form, FormStateInterface $form_state) { /* @var $rule_expression \Drupal\rules\Plugin\RulesExpression\Rule */ $rule_expression = $component->getExpression(); - foreach ($values as $uuid => $expression) { - $action = $rule_expression->getExpression($uuid); - $action->setWeight($expression['weight']); - $action->setConfiguration($action->getConfiguration()); + if ($values) { + foreach ($values as $uuid => $expression) { + $action = $rule_expression->getExpression($uuid); + $action->setWeight($expression['weight']); + $action->setConfiguration($action->getConfiguration()); + } } $this->getRulesUiHandler()->updateComponent($component); diff --git a/src/Form/Expression/ConditionContainerForm.php b/src/Form/Expression/ConditionContainerForm.php index c30aa763..029c9244 100644 --- a/src/Form/Expression/ConditionContainerForm.php +++ b/src/Form/Expression/ConditionContainerForm.php @@ -50,7 +50,7 @@ public function form(array $form, FormStateInterface $form_state) { ], ], '#attributes' => [ - 'id' => 'rules_conditions_table' + 'id' => 'rules_conditions_table', ], '#tabledrag' => [ [ @@ -81,7 +81,7 @@ public function form(array $form, FormStateInterface $form_state) { // TableDrag: Mark the table row as draggable. $row['#attributes']['class'][] = 'draggable'; - // TableDrag: Sort the table row according to its existing/configured weight. + // TableDrag: Sort the table row according to its weight. $row['#weight'] = $condition->getWeight(); $row['title'] = ['#markup' => $condition->getLabel()]; @@ -139,10 +139,12 @@ public function submitForm(array &$form, FormStateInterface $form_state) { /* @var $rule_expression \Drupal\rules\Plugin\RulesExpression\Rule */ $rule_expression = $component->getExpression(); - foreach ($values as $uuid => $expression) { - $action = $rule_expression->getExpression($uuid); - $action->setWeight($expression['weight']); - $action->setConfiguration($action->getConfiguration()); + if ($values) { + foreach ($values as $uuid => $expression) { + $action = $rule_expression->getExpression($uuid); + $action->setWeight($expression['weight']); + $action->setConfiguration($action->getConfiguration()); + } } $this->getRulesUiHandler()->updateComponent($component); From c6bcf0f2f7b72d7d8967d3685bf8e24cbf1e0416 Mon Sep 17 00:00:00 2001 From: yanniboi Date: Sat, 14 May 2016 20:03:50 -0400 Subject: [PATCH 6/6] Issue #2648334 by yanniboi: Sort expressions by weight before executing in containers. --- src/Plugin/RulesExpression/ActionSet.php | 10 ++++++++++ src/Plugin/RulesExpression/RulesAnd.php | 10 ++++++++++ src/Plugin/RulesExpression/RulesLoop.php | 11 +++++++++++ src/Plugin/RulesExpression/RulesOr.php | 10 ++++++++++ 4 files changed, 41 insertions(+) diff --git a/src/Plugin/RulesExpression/ActionSet.php b/src/Plugin/RulesExpression/ActionSet.php index 3284fa9b..9fae3797 100644 --- a/src/Plugin/RulesExpression/ActionSet.php +++ b/src/Plugin/RulesExpression/ActionSet.php @@ -27,7 +27,17 @@ protected function allowsMetadataAssertions() { * {@inheritdoc} */ public function executeWithState(ExecutionStateInterface $state) { + // Get hold of actions. + // @todo See if we can add getExpressions method of ExpressionContainerBase. + $actions = []; foreach ($this->actions as $action) { + $actions[] = $action; + } + + // Sort actions by weight. + @uasort($actions, [$this->actions, 'expressionSortHelper']); + foreach ($actions as $action) { + /* @var $action \Drupal\rules\Engine\ExpressionInterface */ $action->executeWithState($state); } } diff --git a/src/Plugin/RulesExpression/RulesAnd.php b/src/Plugin/RulesExpression/RulesAnd.php index f1e5b61f..1d509a51 100644 --- a/src/Plugin/RulesExpression/RulesAnd.php +++ b/src/Plugin/RulesExpression/RulesAnd.php @@ -32,7 +32,17 @@ public function isEmpty() { * {@inheritdoc} */ public function evaluate(ExecutionStateInterface $state) { + // Get hold of conditions. + // @todo See if we can add getExpressions method of ExpressionContainerBase. + $conditions = []; foreach ($this->conditions as $condition) { + $conditions[] = $condition; + } + + // Sort conditions by weight. + @uasort($conditions, [$this->conditions, 'expressionSortHelper']); + foreach ($conditions as $condition) { + /* @var $condition \Drupal\rules\Engine\ExpressionInterface */ if (!$condition->executeWithState($state)) { return FALSE; } diff --git a/src/Plugin/RulesExpression/RulesLoop.php b/src/Plugin/RulesExpression/RulesLoop.php index da2a7f05..4d12eec4 100644 --- a/src/Plugin/RulesExpression/RulesLoop.php +++ b/src/Plugin/RulesExpression/RulesLoop.php @@ -38,7 +38,18 @@ public function executeWithState(ExecutionStateInterface $state) { foreach ($list_data as $item) { $state->setVariableData($list_item_name, $item); + + // Get hold of actions. + // @todo See if we can add getExpressions method of ExpressionContainerBase. + $actions = []; foreach ($this->actions as $action) { + $actions[] = $action; + } + + // Sort actions by weight. + @uasort($actions, [$this->actions, 'expressionSortHelper']); + foreach ($actions as $action) { + /* @var $action \Drupal\rules\Engine\ExpressionInterface */ $action->executeWithState($state); } } diff --git a/src/Plugin/RulesExpression/RulesOr.php b/src/Plugin/RulesExpression/RulesOr.php index a599161d..7eedca9b 100644 --- a/src/Plugin/RulesExpression/RulesOr.php +++ b/src/Plugin/RulesExpression/RulesOr.php @@ -19,7 +19,17 @@ class RulesOr extends ConditionExpressionContainer { * {@inheritdoc} */ public function evaluate(ExecutionStateInterface $state) { + // Get hold of conditions. + // @todo See if we can add getExpressions method of ExpressionContainerBase. + $conditions = []; foreach ($this->conditions as $condition) { + $conditions[] = $condition; + } + + // Sort conditions by weight. + @uasort($conditions, [$this->conditions, 'expressionSortHelper']); + foreach ($conditions as $condition) { + /* @var $condition \Drupal\rules\Engine\ExpressionInterface */ if ($condition->executeWithState($state)) { return TRUE; }