diff --git a/core/modules/workflows/src/Plugin/WorkflowTypeBase.php b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php index 9f2e6770fb..c64b0df604 100644 --- a/core/modules/workflows/src/Plugin/WorkflowTypeBase.php +++ b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php @@ -286,8 +286,11 @@ protected static function labelWeightMultisort($objects) { } // Sort weights, labels, and keys in the same order as each other. array_multisort( + // Use the numerical weight as the primary sort. $weights, SORT_NUMERIC, SORT_ASC, + // When objects have the same weight, sort them alphabetically by label. $labels, SORT_NATURAL, SORT_ASC, + // Ensure that the keys (the object IDs) are sorted in the same order as the weights. $keys ); // Combine keys and weights to make sure the weights are keyed with the diff --git a/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php b/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php index 78d247d022..b15b24099c 100644 --- a/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php +++ b/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php @@ -322,4 +322,73 @@ public function testNumericIds() { $this->assertEquals(789, $workflow->getTypePlugin()->getTransition(101112)->to()->id()); } + /** + * Test the sorting of states and transitions by weight and label. + */ + public function testSorting() { + $workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_complex_test', 'label' => 'Test']); + $workflow + ->getTypePlugin() + ->setConfiguration([ + 'states' => [ + 'twoa' => [ + 'label' => 'twoa', + 'weight' => 2, + ], + 'three' => [ + 'label' => 'three', + 'weight' => 3, + ], + 'twob' => [ + 'label' => 'twob', + 'weight' => 2, + ], + 'one' => [ + 'label' => 'one', + 'weight' => 1, + ], + ], + 'transitions' => [ + 'three' => [ + 'label' => 'three', + 'from' => ['three'], + 'to' => 'three', + 'weight' => 3, + ], + 'twoa' => [ + 'label' => 'twoa', + 'from' => ['twoa'], + 'to' => 'twoa', + 'weight' => 2, + ], + 'one' => [ + 'label' => 'one', + 'from' => ['one'], + 'to' => 'one', + 'weight' => 1, + ], + 'twob' => [ + 'label' => 'twob', + 'from' => ['twob'], + 'to' => 'twob', + 'weight' => 2, + ], + ], + ]); + $workflow->save(); + + $this->drupalLogin($this->createUser(['administer workflows'])); + $this->drupalGet('admin/config/workflow/workflows/manage/test'); + $expected_states = ['one', 'twoa', 'twob', 'three']; + $elements = $this->xpath('//details[@id="edit-states-container"]//table/tbody/tr'); + foreach ($elements as $key => $element) { + $this->assertEquals($expected_states[$key], $element->find('xpath', 'td')->getText()); + } + $expected_transitions = ['one', 'twoa', 'twob', 'three']; + $elements = $this->xpath('//details[@id="edit-transitions-container"]//table/tbody/tr'); + foreach ($elements as $key => $element) { + $this->assertEquals($expected_transitions[$key], $element->find('xpath', 'td')->getText()); + } + } + }