~/drupal/32-how-trim-meta-description-tag-value-generated-metatag-module-max-160-characters.md

How to trim Meta Description tag value generated by Metatag module, to max 160 characters

// TL;DR

Override the Metatag module output using hook_metatag_metatags_view_alter to trim the meta description to 160 characters.

How to trim Meta Description tag value generated by Metatag module, to max 160 characters

Introduction

I was using a drupal website, and using metatag module to have all nice SEO metatags in my page. The content type is of article type.

I was using On page optimization of the article pages, and found that meta description value was exceeding the maximum 160 character limit. Then, my struggle started to find a way to limit this description limit. And, I found a solution.

More Context

To give you more context to my problem, I was using "Long text and Summary" type of widget in Body field of Article content type. Which has a summary field.

And, in my metatag setting, I have set the value of Meta description to:[node:summary] token. Which was perfectly being replaced with the content of my article text. But, the problem was that it was coming out way more than 160 characters.

The drupal code actually put everything till first text break(paragraph break) into summary field.

My Vision of Solution

I didn’t want to write custom code for this, which I could. But, drupal best practices says, that we should not change code of Core modules. Rather do an override kind of stuff. So, I was looking for some hooks, and override methods which can help me.

My Struggle Started

So, the modules in my target are:
  • Field (Core module)
  • Metatag
  • Token
  • Node (Core module)

1. Field Module

I found the code where the field "Long text with summary" was placed. But, it was just have settings to render widget, and some validation

2. Metatag

I searched for the code who was filling the token: "node:summary". Couldn't find much.

3. Token

Then, looked for how this token was exposed. This leads me to node module.

4. Node

I did a grep of "node:summary", I just found something in test files. Then, later I found the file: node.tokens.inc, which was exposing the tokens, and was responsible in filling the values there.

I saw this code for summary:

$trim_length = $instance['display']['teaser']['settings']['trim_length'];

So, this shows that the code was getting the max length value from teaser display settings.

Solution

Goto: Content type -> Article -> Manage Display -> Teaser (on top right) ![How to trim Meta Description tag value generated by Metatag module, to max 160 characters](./xdrupal-content-type.png.pagespeed.ic.C5Rrl6E4x7.png)

Then, click on setting button for Body. In my case, it was 600. I changed it to 160. And, Voila.

To test, just view the pages. You will not need to update(edit/save) them.

Enjoy.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

How to add alt attribute of images in all of my drupal articles or other content type

How to add alt attribute of images in all of my drupal articles or other content type

I have a custom content type, and there are around 2000 number of nodes in my…

List all the Node ids which do not have images from my domain

List all the Node ids which do not have images from my domain

I use drupal-7 in my website. I used to write articles and put images in that…

Drupal 8: How to Export and Import View

Drupal 8: How to Export and Import View

You have created some views, and want to port it to your production environment…

Drupal Code: Fetch Link, Title, Category names, tag names from every article

Drupal Code: Fetch Link, Title, Category names, tag names from every article

See the code below: The output will be 4 columns separated by comma. You can…

Drupal: How to block a user by email programatically

Drupal: How to block a user by email programatically

Many times, while administering your drupal website, you must have encountered…

Drupal 7: How to save a node programmatically and add an image field from a public URL

Drupal 7: How to save a node programmatically and add an image field from a public URL

Note: I have public URLs of these images, which I want to save. return…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…