drupal|October 07, 2017|2 min read

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.

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 implement cron tasks (hook_cron and hook_cron_queue_info) that takes much time

Drupal 7: How to implement cron tasks (hook_cron and hook_cron_queue_info) that takes much time

hook_cron() suggests to put tasks that executes in shorter time or non-resource…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…