1

I am having a problem with opening a menu link defined in a module.links.menu.yml in a new browser tab or window. Code for the link:

mymodule.mm_google:
  url: 'https://google.com/'
  parent: mymodule.admin_mm.external_index
  description: 'Search Engine'
  title: 'Google'
  options:
    attributes:
      target: _blank

I am working with Drupal core 8.2.4 8.2.5 and the link currently works except for opening a new browser tab or window.

Update:
The links are opening in a new tab/window when rendered by the Toolbar/Admin Toolbar modules but not when rendered and displayed by admin overview controller \Drupal\system\Controller\SystemController::overview. The controller will also not render included classes for the anchor element.

0

Doing a review of how \Drupal\system\Controller\SystemController::overview builds an Admin Overview page and I found that template_preprocess_admin_block_content was only constructing each link item as an array values that did not include a URL object. While menus built by the admin Toolbar did. The importance of the URL (Drupal\Core\Url) instance being generated is that it includes an Attributes (Drupal\Core\Template\Attribute) instance that Twig can use to render an item's HTML attributes.

The fix:

The template_preprocess_admin_block_content function can be located in a custom module but, there is the requirement to add an overriding template file in the admin theme so it is best to locate the function a custom admin theme. (It is important that you remember the difference between your front-end theme and your admin theme as they are not the same)

/themes/myadmintheme/myadmintheme.theme

/**
 * Implements template_preprocess_admin_block_content().
 */
function myadmintheme_preprocess_admin_block_content(&$variables) {
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $key => $item) {
      $variables['content'][$key]['attributes'] = new Attribute(empty($item['options']['attributes']) ? array() : $item['options']['attributes']);
    }
  }
}


/themes/myadmintheme/templates/admin-block-content.html.twig

{#
/**
 * @file
 * My admin theme implementation for the content of an administrative block.
 *
 * Uses unordered list markup in both compact and extended modes.
 *
 * Available variables:
 * - content: List of administrative menu items. Each menu item contains:
 *   - url: Path to the admin section.
 *   - title: Short name of the section.
 *   - description: Description of the administrative menu item.
 *   - attributes: HTML attributes of the administrative menu item.
 * - attributes: HTML attributes to be added to the element.
 * - compact: Boolean indicating whether compact mode is turned on or not.
 *
 * @see template_preprocess_admin_block_content()
 * @see seven_preprocess_admin_block_content()
 * @see myadmintheme_preprocess_admin_block_content()
 */
#}
{%
  set classes = [
    'admin-list',
    compact ? 'compact',
  ]
%}
{% if content %}
  <ul{{ attributes.addClass(classes) }}>
    {% for item in content %}
      <li><a href="{{ item.url }}"{{ item.attributes }}><span class="label">{{ item.title }}</span><div class="description">{{ item.description }}</div></a></li>
    {% endfor %}
  </ul>
{% endif %}


In the example code here the theme My Admin Theme (/themes/myadmintheme) is a child theme of Drupal's default admin theme Seven.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.