Skip to main content
Image
blog-banner-opensenselabs%20%282%29_0.jpg

HowTo: Add Anchor Tag and Attributes in Drupal 8

article publisher

Gaurav

Drupal

Anchor tags are not only important from SEO perspective, they are also elements of better navigation and great user experience. Often ignored (or rather used unwisely), using anchor tags is definitely one of the best SEO Practices

But the deeper you go, you realize there are various elements that you need to take care of when adding custom attributes. In this blog, we will learn to add tag properties – rel, id, and target – in Drupal 8.

Anchor tags are clickable texts in a hyperlink.

If you are using Drupal, you know it is easy to add the anchor tags with CKEditor. But there are some limitations when using CKEditor. In order to keep the library light, certain features are not available right away and need to be downloaded. To get these advanced features you need to add additional libraries. 

But, Drupal 8 has link attribute module. In this module, we have the advanced options to add the various anchor tags in the webpage. 

Exploring Drupal Link Attribute Module

The Drupal Link Attributes provides a widget that allows users to add attributes (quality) to links. It overtakes the core default widget for menu link content entities, allowing you to set attributes on the menu links. The supported attributes given in the module are id, name, class, target, access key, and rel which means all of these given attributes can be added to your link if you are using the link attribute module.

Installing and Configuring the Link Attribute Module

Download the module from Drupal.org. 

You can install the link_attribute module using the Drush command.

$ drush dl link_attribute
$ drush en link_attribute
configuring the link attribute module

Or you can install and enable it from Administrative UI

  • Enable the module  Content type → Manage field. 
     
  • Once there add a field, let's say sample. Point to note here is that this will be a link, therefore we need to give it a label. Add label.
     
  • Once the field is created  Manage  Form Display Newly created field Sample.link widget options in the drop down
  • Change widget to link with attributes  Configure it by selecting the enabled attributes needed Update Savetesting link attribute module
  • Go to Content Create content Look for the field.  And there you have it.attributes configured shown in the field

The link attribute module is simple and extremely intuitive. This is a nifty module which extends the qualities of link module.

In case you want to add an attribute which is not listed in the pre-defined list then, follow these steps:

Add "<em>mymodule.link_attributes.yml</em>” file to your custom module and make new attributes for example:

adding attributes to the module

If you are adding the field to the configurable field then alter configurable field and in the format of the .yml file and before you re-import it add the extra attributes.

And if you want to add to the base fields then you may follow the link adding attributes to the field.

So this is how you can add the various tag anchor properties to your links widget on your webpage. And also if you want the custom attributes then you can add them using the given link.

Subscribe

Ready to start your digital transformation journey with us?

Related Blogs

SDC: Integrating Storybook & Single Directory Component

Integrating SDC With Storybook OpenSense Labs

Today, we will talk about about Drupal Single Directory Components or SDC and Storybook. Single Directory Components in Drupal allows you…

RFP: How To Create An RFP For Open Source Solutions?

RFP OpenSense Labs

A good Request for Proposals (RFP) or Call for Proposals (CFP) clearly states the goals and expectations of your project and sets the…

Drupal 7 End Of Life: Top Reasons You Should Migrate To Drupal 10

Drupal 7 End Of Life Cover Image OpenSense Labs

Drupal 10 was released in December 2022 and ever since, the community has been pushing its users to do Drupal 7 to 10 migration. As per…