[1.1.x] Manage URL without preprocesses

Created on 30 October 2024, about 2 months ago

Problem/Motivation

SDC is not allowing prepocesses on UI components, and it is

Find a way of dealing with URL

menu, button and link components

In 📌 Automatic conversion to SDC & UI Patterns 2.x Postponed , we get rid of those 3 preprocesses:

function ui_suite_dsfr_preprocess_pattern_footer_menu(array &$variables) {
  foreach ($variables['items'] as &$item) {
    /* Some stuff we can remove... */
    // It seems UrlHelper::setAllowedProtocols() doesn't support mailto.
    if (!str_starts_with(strtolower($item['url']), 'mailto:')
        && UrlHelper::isExternal($item['url'])
        && !UrlHelper::externalIsLocal($item['url'], \Drupal::request()->getSchemeAndHttpHost())) {
      $item['link_attributes']['target'] = '_blank';
    }
  }
}
function ui_suite_dsfr_preprocess_pattern_button(array &$variables) {
  if (empty($variables['url'])) {
    return;
  }
  if ($variables['url'] instanceof MarkupInterface) {
    $variables['url'] = (string) $variables['url'];
  }
  if ($variables['url'] instanceof Url) {
    $variables['url'] = $variables['url']->toString();
  }
  // It seems UrlHelper::setAllowedProtocols() doesn't support mailto.
  if (!str_starts_with(strtolower($variables['url']), 'mailto:')
    && UrlHelper::isExternal($variables['url'])
    && !UrlHelper::externalIsLocal($variables['url'], \Drupal::request()->getSchemeAndHttpHost())) {
    $variables['external'] = TRUE;
  }
}
function ui_suite_dsfr_preprocess_pattern_link(array &$variables) {
  if (empty($variables['url'])) {
    return;
  }
  if ($variables['url'] instanceof MarkupInterface) {
    $variables['url'] = (string) $variables['url'];
  }
  if ($variables['url'] instanceof Url) {
    $variables['url'] = $variables['url']->toString();
  }
  // It seems UrlHelper::setAllowedProtocols() doesn't support mailto.
  if (!str_starts_with(strtolower($variables['url']), 'mailto:')
    && UrlHelper::isExternal($variables['url'])
    && !UrlHelper::externalIsLocal($variables['url'], \Drupal::request()->getSchemeAndHttpHost())) {
    $variables['external'] = TRUE;
  }
}

footer component

🐛 The button and no link routes and attributes are not retrieved from the url object in the menu_footer pattern. Active is proposing to call hook_preprocess_pattern_footer_menu.

nav_menu component

🐛 Don't show menu element wen empty and no link Active is proposing to manipulate item.url.routeName in the template.

Proposed resolution

What do we do?

The rules:

  • No preprocesses
  • No URL object manipulation in templates
  • No checks on routes in templates

Can we leverage LinksPropType, UrlPropType?

📌 Task
Status

Active

Version

1.0

Component

Code

Created by

🇫🇷France pdureau Paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024