Unnecessary HTML attributes

Created on 22 November 2022, about 2 years ago
Updated 15 October 2024, 2 months ago

Problem/Motivation

The HTML generated for the title looks like:

<h2 class="block-title">
  <span><a href="URL" target="" title="">TITLE</a></span>
</h2>

Target and title could only be added when actually specified.

🐛 Bug report
Status

Active

Version

1.1

Component

Code

Created by

🇭🇺Hungary djg_tram

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom altcom_neil

    Hi, the simplest fix I found was simply to check the value is not empty and set the attribute to NULL if it is:

    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
    @@ -93,8 +93,8 @@ function block_title_link_preprocess_block(&$variables) {
             '#title' => $blockLabel,
             '#url' => Url::fromUri($tileLink),
             '#attributes' => [
    -          'target' => $linkTarget,
    -          'title' => $linkTitle,
    +          'target' => $linkTarget ?: NULL,
    +          'title' => $linkTitle ?: NULL,
             ],
           ];
         }
    

    The attributes are then not rendered if the value is NULL.

Production build 0.71.5 2024