Skip to content

Commit

Permalink
Saving and dragging and basic order works now
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
DiegoPino committed Nov 16, 2020
1 parent 477d46a commit 9f87da2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ 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,
];
$form[$this->entitiesKey]['#tabledrag'][] =
[
'action' => 'depth',
'relationship' => 'group',
'group' => 'tabledrag-test-depth',
'group' => 'tabledrag-postprocessor-depth',
'hidden' => TRUE,
];
dpm($form);
Expand Down Expand Up @@ -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')];

Expand All @@ -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]);
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/Entity/strawberryRunnerPostprocessorEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

}

0 comments on commit 9f87da2

Please sign in to comment.