Verified Commit 787aa9be authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2927311 by DanielVeza, smustgrave, Gribnif, richard.thomas, larowlan:...

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

(cherry picked from commit fcc16f49)
parent f09c8346
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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 @@ public static function preRenderLinks(array $element) {
      $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) {
+5 −0
Original line number Diff line number Diff line
@@ -161,6 +161,11 @@ public function testDifferentPermissions() {
    $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'));
  }

  /**
+3 −0
Original line number Diff line number Diff line
@@ -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