Commit a5f07bb1 authored by David Suissa's avatar David Suissa
Browse files

Issue #3557282 by dydave: Fixed phpstan errors by using core utility...

Issue #3557282 by dydave: Fixed phpstan errors by using core utility 'NestedArray::mergeDeep()' instead of directly changing link attributes options array.
parent 10856160
Loading
Loading
Loading
Loading
Loading
+49 −34
Original line number Diff line number Diff line
@@ -6,10 +6,29 @@
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Url;
use Drupal\admin_toolbar\Render\Element\AdminToolbar;

/**
 * Implements hook_help().
 */
function admin_toolbar_help($route_name) {
  switch ($route_name) {
    case 'help.page.admin_toolbar':
      $variables = [
        ':toolbar' => Url::fromRoute('help.page', ['name' => 'toolbar'])->toString(),
        ':automated_cron' => (\Drupal::moduleHandler()->moduleExists('automated_cron')) ? Url::fromRoute('help.page', ['name' => 'automated_cron'])->toString() : '#',
      ];
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Admin Toolbar module enhances the <a href=":toolbar">Toolbar</a> module by providing fast access to all the administrative links at the top of your site. Admin Toolbar remains a very "lightweight" module by closely integrating with all Toolbar functionality. It can be used in conjunction with all the sub modules included on Admin Toolbar, for quick access to system commands such as Flush all caches, <a href=":automated_cron">Run cron</a>, Run Updates, etc.', $variables) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<p>' . t('The Admin Toolbar greatly improves the user experience for those who regularly interact with the site Toolbar by providing fast, full access to all links in the site Toolbar without having to click to get there.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_toolbar_alter().
 */
@@ -54,52 +73,48 @@ function admin_toolbar_toolbar_alter(&$items) {
}

/**
 * Implements hook_help().
 */
function admin_toolbar_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.admin_toolbar':
      $variables = [
        ':toolbar' => Url::fromRoute('help.page', ['name' => 'toolbar'])->toString(),
        ':automated_cron' => (\Drupal::moduleHandler()->moduleExists('automated_cron')) ? Url::fromRoute('help.page', ['name' => 'automated_cron'])->toString() : '#',
      ];
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Admin Toolbar module enhances the <a href=":toolbar">Toolbar</a> module by providing fast access to all the administrative links at the top of your site. Admin Toolbar remains a very "lightweight" module by closely integrating with all Toolbar functionality. It can be used in conjunction with all the sub modules included on Admin Toolbar, for quick access to system commands such as Flush all caches, <a href=":automated_cron">Run cron</a>, Run Updates, etc.', $variables) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<p>' . t('The Admin Toolbar greatly improves the user experience for those who regularly interact with the site Toolbar by providing fast, full access to all links in the site Toolbar without having to click to get there.') . '</p>';
      return $output;
  }
}

/**
 * Adds toolbar-specific attributes to the menu link tree.
 * Adds admin toolbar specific attributes to the menu link tree.
 *
 * Mostly adds icon CSS classes to each link based on its route and a title
 * attribute with the link description for accessibility.
 *
 * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
 *   The menu link tree to manipulate.
 *
 * @return \Drupal\Core\Menu\MenuLinkTreeElement[]
 *   The manipulated menu link tree.
 *
 * @see \Drupal\admin_toolbar\Render\Element\AdminToolbar::preRenderTray()
 * @see admin_toolbar_toolbar_alter()
 */
// phpcs:ignore Drupal.NamingConventions.ValidFunctionName.InvalidPrefix, Drupal.Commenting.FunctionComment.Missing
function toolbar_tools_menu_navigation_links(array $tree) {
  foreach ($tree as $element) {
    // Loop recursively through all subtree elements.
    if ($element->subtree) {
      toolbar_tools_menu_navigation_links($element->subtree);
    }
    $link = $element->link;
    // Get the non-localized title to make the icon class.
    $definition = $link->getPluginDefinition();
    // Prevent PHPSTAN error due to incorrect type hinting as an array of
    // strings (string[]) for property 'options' of class 'MenuLinkTreeElement'
    // in Core, preventing the assignment of an associative array of options.
    // @phpstan-ignore offsetAssign.dimType
    $element->options['attributes']['class'][] = 'toolbar-icon';
    $string = strtolower(str_replace(['.', ' ', '_'], ['-', '-', '-'], $definition['id']));
    // @phpstan-ignore assign.propertyType
    $element->options['attributes']['class'][] = Html::cleanCssIdentifier('toolbar-icon-' . $string);
    // @phpstan-ignore assign.propertyType
    $element->options['attributes']['title'] = $link->getDescription();
    // Get the menu link from the tree element.
    $menu_link = $element->link;

    // Get the plugin ID of the link, which is its route to make the icon class.
    $link_definition = $menu_link->getPluginDefinition();
    // Replace dots, spaces and underscores with hyphens in the route name.
    $link_plugin_route = strtolower(str_replace(['.', ' ', '_'], ['-', '-', '-'], $link_definition['id']));
    $element_options = [
      'attributes' => [
        'class' => [
          'toolbar-icon',
          // Add a specific class for each link based on its route.
          'toolbar-icon-' . Html::cleanCssIdentifier($link_plugin_route),
        ],
        // Add a title attribute for accessibility.
        'title' => $menu_link->getDescription(),
      ],
    ];

    // Merge the new options with the existing ones.
    $element->options = NestedArray::mergeDeep($element->options, $element_options);
  }
  return $tree;
}
+4 −3
Original line number Diff line number Diff line
@@ -21,8 +21,6 @@ class AdminToolbarSettingsFormTest extends BrowserTestBase {
   */
  protected static $modules = [
    'admin_toolbar',
    // Enable the 'user' module to be able to test a link under config.
    'user',
  ];

  /**
@@ -42,7 +40,7 @@ class AdminToolbarSettingsFormTest extends BrowserTestBase {
      'access toolbar',
      'access administration pages',
      'administer site configuration',
      // This permission is needed to display the link to be tested.
      // This permission is needed to display the user admin links to be tested.
      'administer account settings',
    ];
    $this->adminUser = $this->drupalCreateUser($permissions);
@@ -50,6 +48,9 @@ class AdminToolbarSettingsFormTest extends BrowserTestBase {

  /**
   * Test backend admin toolbar settings form fields and submission.
   *
   * Login as an admin user, go to the 'Admin Toolbar settings' form, change
   * all the values, submit the form and check the expected values are applied.
   */
  public function testAdminToolbarSettingsForm(): void {
    /** @var \Drupal\Tests\WebAssert $assert */