How can I tweak the amount of characters for [node:summary], so that the description metatag never shows more than 150 characters?

On admin/config/search/metatags/config/node I can only see for description token [node:summary], which does not allow me to control the amount of characters that get displayed on the source code. I would need to trim it to max 150 characters, but I have no idea how/where this can be done.


1 Answer 1


You can do a few different things. Assuming you are using the metatag module.

1) Prevent metatags from saving with more than 150 chars using a presave hook:

function hook_metatag_presave(&$metatags, $entity_type, $entity_id, $revision_id, $langcode) {
  dsm($metatags);//debug what the array is. You can trim the values for the metatags here.

2) Alter the metatag before bing cached:

 * Alter metatags before being cached.
 * This hook is invoked prior to the meta tags for a given page are cached.
 * @param array $output
 *   All of the meta tags to be output for this page in their raw format. This
 *   is a heavily nested array.
 * @param string $instance
 *   An identifier for the current page's page type, typically a combination
 *   of the entity name and bundle name, e.g. "node:story".
 * @param array $options
 *   All of the options used to generate the meta tags.
function hook_metatag_metatags_view_alter(&$output, $instance, $options) {
  if (isset($output['description']['#attached']['drupal_add_html_head'][0][0]['#value'])) {
    $output['description']['#attached']['drupal_add_html_head'][0][0]['#value'] = truncate_utf8($output['description']['#attached']['drupal_add_html_head'][0][0]['#value'],150);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.