From 9f87da2aa2ef5dfd6f4c7938dc96cccd3dcf99ed Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Sun, 15 Nov 2020 22:26:19 -0500 Subject: [PATCH] Saving and dragging and basic order works now TODO: i want hierarchies to be limited, max 3 in depth? I feel 3 is a nice number I want limit on plugins here. All this should go in the validation of the form inside `strawberryRunnerPostProcessorEntityListBuilder` --- ...ryRunnerPostProcessorEntityListBuilder.php | 20 +++++++-------- .../strawberryRunnerPostprocessorEntity.php | 25 ++++++++++++++++++- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/Entity/Controller/strawberryRunnerPostProcessorEntityListBuilder.php b/src/Entity/Controller/strawberryRunnerPostProcessorEntityListBuilder.php index a7f07a2..93855f7 100644 --- a/src/Entity/Controller/strawberryRunnerPostProcessorEntityListBuilder.php +++ b/src/Entity/Controller/strawberryRunnerPostProcessorEntityListBuilder.php @@ -112,9 +112,9 @@ public function buildForm(array $form, FormStateInterface $form_state) { [ 'action' => 'match', 'relationship' => 'parent', - 'group' => 'tabledrag-test-parent', - 'subgroup' => 'tabledrag-test-parent', - 'source' => 'tabledrag-test-id', + 'group' => 'tabledrag-postprocessor-parent', + 'subgroup' => 'tabledrag-postprocessor-parent', + 'source' => 'tabledrag-postprocessor-id', 'hidden' => TRUE, 'limit' => 2, ]; @@ -122,7 +122,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { [ 'action' => 'depth', 'relationship' => 'group', - 'group' => 'tabledrag-test-depth', + 'group' => 'tabledrag-postprocessor-depth', 'hidden' => TRUE, ]; dpm($form); @@ -163,18 +163,18 @@ public function buildRow(EntityInterface $entity) { $row['id'] = [ '#type' => 'hidden', '#value' => $entity->id(), - '#attributes' => ['class' => ['tabledrag-test-id']], + '#attributes' => ['class' => ['tabledrag-postprocessor-id']], ]; $row['parent'] = [ '#type' => 'hidden', '#default_value' => $entity->getParent(), '#parents' => [$this->entitiesKey, $entity->id(), 'parent'], - '#attributes' => ['class' => ['tabledrag-test-parent']], + '#attributes' => ['class' => ['tabledrag-postprocessor-parent']], ]; $row['depth'] = [ '#type' => 'hidden', '#default_value' => $entity->getDepth(), - '#attributes' => ['class' => ['tabledrag-test-depth']], + '#attributes' => ['class' => ['tabledrag-postprocessor-depth']], ]; $row['active'] = $entity->isActive() ? [ '#markup' => $this->t('Yes')] : [ '#markup' =>$this->t('No')]; @@ -183,18 +183,18 @@ public function buildRow(EntityInterface $entity) { public function submitForm(array &$form, FormStateInterface $form_state) { foreach ($form_state->getValue($this->entitiesKey) as $id => $value) { - dpm($value); - if (isset($this->entities[$id]) && + + if (isset($this->entities[$id]) && ( $this->entities[$id]->get($this->weightKey) != $value['weight'] || $this->entities[$id]->getDepth() != $value['depth'] || $this->entities[$id]->getParent() != $value['parent'] + ) ) { // Save entity only when its weight or depth or parent was changed. $this->entities[$id]->set($this->weightKey, $value['weight']); $this->entities[$id]->setDepth($value['depth']); $this->entities[$id]->setParent($value['parent']); $this->entities[$id]->save(); - dpm($this->entities[$id]); } } } diff --git a/src/Entity/strawberryRunnerPostprocessorEntity.php b/src/Entity/strawberryRunnerPostprocessorEntity.php index 86bea24..fe9f0ff 100644 --- a/src/Entity/strawberryRunnerPostprocessorEntity.php +++ b/src/Entity/strawberryRunnerPostprocessorEntity.php @@ -193,7 +193,7 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) if ($a instanceof strawberryRunnerPostprocessorEntityInterface && $b instanceof strawberryRunnerPostprocessorEntityInterface) { if ($a->isActive() && $b->isActive()) { - return parent::sort($a, $b); + return static::hierarchicalSort($a, $b); } elseif (!$a->isActive()) { return -1; @@ -206,4 +206,27 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) return parent::sort($a, $b); } + /** + * Helper callback for uasort() to sort configuration entities by weight, parent and label. + */ + public static function hierarchicalSort(ConfigEntityInterface $a, ConfigEntityInterface $b) { + $a_parent = isset($a->parent) ? $a->parent : ''; + $b_parent = isset($b->parent) ? $b->parent : ''; + + $a_weight = isset($a->weight) ? $a->weight : 0; + $b_weight = isset($b->weight) ? $b->weight : 0; + if ($a_parent == $b->id()) { + return 1; + } + if ($b_parent == $a->id()) { + return -1; + } + if ($a_weight == $b_weight) { + $a_label = $a->label(); + $b_label = $b->label(); + return strnatcasecmp($a_label, $b_label); + } + return ($a_weight < $b_weight) ? -1 : 1; + } + }