Issue #2927311 by DanielVeza, smustgrave, Gribnif, richard.thomas, larowlan: Contextual links are not sorted by weight, contrary to documentation

(cherry picked from commit fcc16f4990)
merge-requests/3604/head
Lee Rowlands 2023-04-11 07:10:23 +10:00
parent 8c76fe6dc3
commit 38937209db
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
3 changed files with 11 additions and 0 deletions

View File

@ -3,6 +3,7 @@
namespace Drupal\contextual\Element;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\Core\Url;
@ -72,6 +73,8 @@ class ContextualLinks extends RenderElement {
$items += $contextual_links_manager->getContextualLinksArrayByGroup($group, $args['route_parameters'], $args['metadata']);
}
uasort($items, [SortArray::class, 'sortByWeightElement']);
// Transform contextual links into parameters suitable for links.html.twig.
$links = [];
foreach ($items as $class => $item) {

View File

@ -161,6 +161,11 @@ class ContextualDynamicContextTest extends BrowserTestBase {
$this->drupalGet(Url::fromRoute('menu_test.contextual_test'));
$this->assertSession()->assertEscaped("<script>alert('Welcome to the jungle!')</script>");
$this->assertSession()->responseContains('<li><a href="' . base_path() . 'menu-test-contextual/1/edit" class="use-ajax" data-dialog-type="modal" data-is-something>Edit menu - contextual</a></li>');
// Test contextual links respects the weight set in *.links.contextual.yml.
$firstLink = $this->assertSession()->elementExists('css', 'ul.contextual-links li:nth-of-type(1) a');
$secondLink = $this->assertSession()->elementExists('css', 'ul.contextual-links li:nth-of-type(2) a');
$this->assertEquals(base_path() . 'menu-test-contextual/1/edit', $firstLink->getAttribute('href'));
$this->assertEquals(base_path() . 'menu-test-contextual/1', $secondLink->getAttribute('href'));
}
/**

View File

@ -3,6 +3,7 @@ menu_test.contextual_hidden_manage:
group: menu_test_menu
route_name: menu_test.contextual_hidden_manage
class: '\Drupal\menu_test\Plugin\Menu\ContextualLink\TestContextualLink'
weight: 2
menu_test.contextual_hidden_manage_edit:
title: 'Edit menu - contextual'
@ -13,8 +14,10 @@ menu_test.contextual_hidden_manage_edit:
class: ['use-ajax']
data-dialog-type: 'modal'
data-is-something: TRUE
weight: 1
menu_test.hidden_block_configure:
title: 'Configure block'
group: menu_test_block
route_name: menu_test.hidden_block_configure
weight: 0