[beta5] Add consistency to link and button patterns

Created on 4 October 2023, 9 months ago

Problem/Motivation

There are a few issues with those preprocesses:

function ui_suite_dsfr_preprocess_pattern_link(array &$variables) {
  if (empty($variables['url'])) {
    return;
  }
  // Mark external urls.
  if (UrlHelper::isExternal($variables['url'])
    && !UrlHelper::externalIsLocal($variables['url'], \Drupal::request()->getSchemeAndHttpHost())) {
    $variables['is_external'] = TRUE;
    $variables['target'] = 'blank';
  }
  if (isset($variables['target']) && $variables['target'] === 'blank') {
    $variables['title'] = t('@title - new window', ['@title' => !empty($variables['title']) ? $variables['title'] : $variables['label']]);
  }
}

function ui_suite_dsfr_preprocess_pattern_button(array &$variables) {
  if (empty($variables['url'])) {
    return;
  }
  // Mark external urls.
  if (UrlHelper::isExternal($variables['url'])
    && !UrlHelper::externalIsLocal($variables['url'], \Drupal::request()->getSchemeAndHttpHost())) {
    $attribute = $variables['attributes'] ?? new Attribute;
    $attribute->setAttribute('rel', 'noopener');
    $variables['attributes'] = $attribute;
    $variables['target'] = 'blank';
  }
  if (isset($variables['target']) && $variables['target'] === 'blank') {
    $variables['title'] = t('@title - new window', ['@title' => !empty($variables['title']) ? $variables['title'] : $variables['label']]);
  }
}
  1. Pattern preprocess needs to be removed (if possible) or simplified to the max, because they are not allowed by SDC and will be removed from UI Patterns 2.x
  2. Those preprocesses do the same thing but a bit differently

Proposed resolution

Some variables values can move to Twig templates: target, title, attributes...

We can promote external as a boolean component prop (setting), and have a unified and simplified code:

function ui_suite_dsfr_preprocess_pattern_link(array &$variables) {
  if (empty($variables['url'])) {
    return;
  }
  if (UrlHelper::isExternal($variables['url'])
    && !UrlHelper::externalIsLocal($variables['url'], \Drupal::request()->getSchemeAndHttpHost())) {
    $variables['external'] = TRUE;
  }
}

function ui_suite_dsfr_preprocess_pattern_button(array &$variables) {
  if (empty($variables['url'])) {
    return;
  }
  if (UrlHelper::isExternal($variables['url'])
    && !UrlHelper::externalIsLocal($variables['url'], \Drupal::request()->getSchemeAndHttpHost())) {
    $variables['external'] = TRUE;
  }
}

Let's also try to remove force_internal prop from link component

API changes

If we remove force_internal, it will be a breaking change. So, ⚠️ was added to issue title. Breaking changes are still allowed in UI Suite DSFR, until we reach RC1.

πŸ“Œ Task
Status

Fixed

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.69.0 2024