Skip to content
Snippets Groups Projects
Commit 205b92e1 authored by Kevin Paxman's avatar Kevin Paxman
Browse files

Merge branch 'feature/ISTWCMS-6306-ibiki-related_links_fix' into '1.1.x'

ISTWCMS-6306: Refactor link handling logic with reusable method.

See merge request !261
parents 21ac4480 47328d80
No related branches found
No related tags found
1 merge request!261ISTWCMS-6306: Refactor link handling logic with reusable method.
......@@ -183,7 +183,7 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface {
}
}
elseif ($index == 'background_color') {
if ($link_info['items'][$count]['background'] == 'image') {
if ($link_info['items'][$count]['background'] == 'image' || $link_info['items'][$count]['background'] == 'none') {
$links['items'][$count][$index] = '';
}
else {
......@@ -242,7 +242,7 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface {
// because there's no need to send them through the alias
// lookup and they are for the current page so don't need
// the base path.
if (strpos($value, '#') === 0) {
if (str_starts_with($value, '#')) {
$links['items'][$count][$index] = $value;
}
// If this is not an external link, get the friendly path
......@@ -253,16 +253,8 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface {
->getCurrentRequest()
->getBasePath();
// Remove the base path from the url, so we can get a correct
// value back from the path alias.
$pos = strpos($value, $base_path);
if ($pos !== FALSE) {
$value = substr_replace($value, '', $pos, strlen($base_path));
}
$value = $this->prepareLink($value, $base_path);
// Get the friendly path. Anything that doesn't have
// one will return the "real" path.
$value = str_replace("internal:", "", $value);
$pathauto = $this->pathAliasManager
->getAliasByPath($value);
......@@ -278,6 +270,29 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface {
$links['items'][$count][$index] = $value;
}
}
elseif ($index === 'title') {
// See if link title needs to be modified.
if (empty($item['title'])) {
$node_path = $this->pathAliasManager->getPathByAlias($url);
if (preg_match('/^\/node\/(\d+)$/', $node_path, $matches)) {
$node_id = $matches[1];
if ($node_id) {
$node_title = $this->entityTypeManager->getStorage('node')
->load($node_id)
?->getTitle();
if ($node_title) {
$links['items'][$count]['title'] = $node_title;
}
}
}
}
else {
$links['items'][$count]['title'] = $value;
}
}
else {
$links['items'][$count][$index] = $value;
}
......@@ -359,7 +374,6 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface {
$group_class = 'group-order-weight';
$form['link_style'] = [
'#type' => 'details',
'#type' => 'details',
'#title' => $this->t('Link style'),
'#open' => TRUE,
......@@ -456,7 +470,7 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface {
// If there is a link already set, setup the details to
// have the link in the title so when it is closed,
// we can see what the actual is.
$value_uri = str_replace("internal:", "", $links['items'][$i]['uri'] ?? '');
$value_uri = $this->prepareLink($links['items'][$i]['uri'] ?? '');
if (isset($links['items'][$i]['uri'])) {
$link = $this->t('Link (@uri)', ['@uri' => $value_uri]);
......@@ -907,4 +921,42 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface {
];
}
/**
* Prepares and sanitizes a link by removing specific base path prefixes.
*
* This method removes the base path and 'internal:/' prefix from the
* provided link to ensure it is in the desired format.
*
* @param string $link
* The link to be sanitized.
* @param string|null $base_path
* The base path to be removed if the link starts with it.
*
* @return string
* The sanitized link without the specified prefixes.
*/
private function prepareLink(string $link, ?string $base_path = NULL): string {
if (empty($link)) {
return $link;
}
// If a link starts with base_path, remove it.
if (str_starts_with($link, $base_path . '/')) {
$link = substr_replace($link, '', 0, strlen($base_path));
}
// Remove internal:/ from the link.
if (str_starts_with($link, 'internal:/')) {
$link = substr_replace($link, '', 0, strlen('internal:/'));
}
// Prepend leading slash if not found.
if (!str_starts_with($link, '/')) {
$link = '/' . $link;
}
return $link;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment