Problem/Motivation

This is a regression introduced in #3062573: The menus are sorted by machine name and not by label

The website encountered an unexpected error. Please try again later.
Error: Undefined class constant 'MAX_BUNDLE_NUMBER' in Drupal\admin_toolbar_tools\SearchLinks->getLinks() (line 185 of modules/contrib/admin_toolbar/admin_toolbar_tools/src/SearchLinks.php).
Drupal\admin_toolbar_tools\SearchLinks->getLinks() (Line: 289)
Drupal\admin_toolbar_tools\Controller\ToolbarController->search()

Proposed resolution

same constant should be used in ExtraLinks.php and SearchLinks.php

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

oknate created an issue. See original summary.

oknate’s picture

Status: Active » Needs review
FileSize
1.62 KB

Here's a fix. We should have test coverage so this doesn't happen again. I'll see if I can set that up.

oknate’s picture

Test only patch that demonstrates the bug.

oknate’s picture

Combining the test coverage with the fix. The test coverage is very minimal. I'd like to expand test coverage of menus in search.

UPDATE: I'm adding additional test coverage below.

The last submitted patch, 3: 3065013-3--TEST-ONLY.patch, failed testing. View results

oknate’s picture

Adding additional test coverage for menus in the admin_toolbar.search route.

oknate’s picture

1) removing "Ava", I'm noticing this is showing up in both the admin toolbar and in the search links. I'm looking into that.
2) alphabetizing the names used for creating menus in the test.

oknate’s picture

I figured why "Ava" was showing in both the toolbar and the search links. We were missing a use statement that meant that sort wasn't working properly in the SearchLinks.php class:

diff --git a/admin_toolbar_tools/src/SearchLinks.php b/admin_toolbar_tools/src/SearchLinks.php
index a7a92b3..703edbe 100644
--- a/admin_toolbar_tools/src/SearchLinks.php
+++ b/admin_toolbar_tools/src/SearchLinks.php
@@ -12,6 +12,7 @@ use Drupal\Core\Language\LanguageInterface;
 use Drupal\Core\Routing\RouteProviderInterface;
 use Drupal\Core\StringTranslation\StringTranslationTrait;
 use Drupal\Core\Url;
+use Drupal\system\Entity\Menu;

It took me a while to figure out why I was not getting the expected output. Finally, I ran this code:

$menus = $this->entityTypeManager->getStorage('menu')->loadMultiple();
      uasort($menus, [Menu::class, 'sort']);
      $menus = array_slice($menus, 0, self::MAX_BUNDLE_NUMBER);

in ExtraLinks.php:

Array ( [0] => ada [1] => admin [2] => amara [3] => amelia [4] => arabella [5] => asher [6] => astrid [7] => atticus [8] => aurora [9] => ava )

in SearchLinks.php:
Array ( [0] => account [1] => ada [2] => admin [3] => amara [4] => amelia [5] => arabella [6] => asher [7] => astrid [8] => atticus [9] => aurora )

So it was the sorting that was breaking. At first I thought maybe it was an issue with the offset, but I tested and our use of offset is correct.

oknate’s picture

Adding test coverage for the same two arrays of menus within the toolbar. Asserting that links after the first 10 don't appear, and the the first 10 do appear.

adriancid’s picture

Status: Needs review » Needs work
+++ b/admin_toolbar_tools/src/SearchLinks.php
@@ -182,7 +182,7 @@ class SearchLinks {
-      $menus = array_slice($menus, self::MAX_BUNDLE_NUMBER);

The patch fail to apply, we don't have this line in the dev branch.

oknate’s picture

Hmm. Taking another look. I see:

$menus = array_slice($menus, self::MAX_BUNDLE_NUMBER);

on line 185 of SearchLinks.php in the 8.x-2.x-dev branch, which is a problem because that constant doesn't exist. I had copied it from ExtraLinks.php and forgot that I had changed the constant name in that class.

Can you check again? If we didn't have that line, how would the patches above apply when running the automated tests?

oknate’s picture

Assigned: oknate » Unassigned

  • adriancid committed f387d9c on 8.x-2.x authored by oknate
    Issue #3065013 by oknate, adriancid:  Undefined class constant '...
adriancid’s picture

Status: Needs work » Fixed

Sorry, I just forgot to made git pull, as I'm working in another laptop now :-D

oknate’s picture

No problem, thanks for helping me fix the regression I introduced!

oknate’s picture

Few people are using the 8.x-2.x branch yet. I only caught it because I was testing how another module works with admin_toolbar 8.x-2.x-dev installed.

But now we have a bit more test coverage, so hopefully it will prevent regressions for the search feature.

adriancid’s picture

Thanks for your help on this module @oknate

  • adriancid committed c34838b on 8.x-2.x
    Issue #3065013 by oknate, adriancid:  Undefined class constant '...

  • adriancid committed 8bd798f on 8.x-2.x
    Issue #3065013 by oknate, adriancid:  Undefined class constant '...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.