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']]);
}
}
- 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
- 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.