Fatal error if link url is not set but field is not required

Created on 15 October 2024, 2 months ago

Problem/Motivation

If you enable the block title link functionality but do not set a 'Title Link Url' then the page where the block is placed fatal errors:

The website encountered an unexpected error. Try again later.

InvalidArgumentException: The URI '' is invalid. You must use a valid URI scheme. in Drupal\Core\Url::fromUri() (line 293 of core/lib/Drupal/Core/Url.php).
block_title_link_preprocess_block()
call_user_func_array() (Line: 261)

Steps to reproduce

Create a block and place it on a page, enable the 'Block Title Link Settings' but do not set a URL.

Proposed resolution

Add a check for the URL as well as the enabled flag:

diff --git a/block_title_link.module b/block_title_link.module
index c56cfba..351ec7e 100644
--- a/block_title_link.module
+++ b/block_title_link.module
@@ -83,8 +83,8 @@ function block_title_link_preprocess_block(&$variables) {
   // Blocks coming from page manager widget does not have id.
   if (!empty($variables['elements']['#id'])) {
     $block = Block::load($variables['elements']['#id']);
-    if ($block && $block->getThirdPartySetting('block_title_link', 'title_link_enable')) {
-      $tileLink = $block->getThirdPartySetting('block_title_link', 'title_link_url');
+    if ($block && $block->getThirdPartySetting('block_title_link', 'title_link_enable')
+      && ($tileLink = $block->getThirdPartySetting('block_title_link', 'title_link_url'))) {
       $linkTitle = $block->getThirdPartySetting('block_title_link', 'link_title');
       $linkTarget = $block->getThirdPartySetting('block_title_link', 'title_link_target');
🐛 Bug report
Status

Active

Version

1.1

Component

Code

Created by

🇬🇧United Kingdom altcom_neil

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024