Metatag contrib module to override the (visible) page title ("metatag_page_title")

Created on 19 June 2024, 9 days ago
Updated 20 June 2024, 8 days ago

Problem/Motivation

Metatag offers great functionality and standards to create metadata patterns.

The page title (<h1>) isn't really metadata. Its use is still close to it, especially for SEO purpose. I guess a Metatag submodule (or contrib?) might still be the right place to implement page title (<h1>) patterns.

Drupal renders the page title through the page-title.html.twig template using the PageTitle Render Element wich is called from PageTitleBlock typically.
The source of the page title is typically a route β†’ / Controller.

For nodes (and most other entities) for example, the view page title is the label returned from the NodeViewController:

/**
   * The _title_callback for the page that renders a single node.
   *
   * @param \Drupal\Core\Entity\EntityInterface $node
   *   The current node.
   *
   * @return string
   *   The page title.
   */
  public function title(EntityInterface $node) {
    return $this->entityRepository->getTranslationFromContext($node)->label();
  }

And here comes the typical challenge:
In some cases you want the page title to differ from the entity label to add further context, but without changing the entity label

This is especially the case for entities that are kind of "Value entities" and not marketing entities. For example:

  • a product with name "Screwdriver" but the page title should be "Buy our Screwdriver"
  • a node with title "Mr. Lee" but the page title should be "Our Team: Mr. Lee"
  • a user with name "Anton", but the page title should be "Anton's profile"
  • ...

where you never want to change the entity label to have a page title like this.

The reasons may for example be

  • SEO
  • Giving context
  • Marketing / textual requirements
  • ...

So while this isn't a typical Metatag, I think Metatag already has the perfect infrastructure and might be the place where to expect this for advanced use-cases.

For that reason, I think that functionality shouldn't live in Metatag base modoule, but in a submodule or contrib.

In some simple cases, we were able to use
https://www.drupal.org/project/node_title_ps β†’

Related modules are:

but I couldn't find a module solving this at a level comparable to how Metatag could solve that at the entity type / bundle level and the entity (override) level out of the box.

I'd be super happy to get some (maintainer and community) feedback on the idea. We needed this again and again in our projects.

Further notes:

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

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

Comments & Activities

  • Issue created by @Anybody
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    This is an interesting idea. I haven't ran into the use case for it myself, but that's not to say it isn't valid.

    A few questions on the implementation:

    • UX - how do you accurately distinguish from the title and the page title in a way that doesn't confuse content admins?
    • How much customization would be needed to avoid the page_title plugin from rendering as a meta tag?
    • Would any API changes be needed to extract the page_title value for use in the page-title.html.twig?
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Thanks for the rapid feedback @DamienMcKenna!!

    We'll elaborate on this and potentially make a first implementation, I guess in a sandbox module, but let's see! I think we can then give better answers on the points rased!

    Thank you!!

  • πŸ‡ΊπŸ‡ΈUnited States thejimbirch Cape Cod, Massachusetts

    I don't see how this is metatag module related as you said, it is not meta data related, will not print in the head.

    I have however done similar things. A second field field_h1 and a minor if statement in twig templates would do the trick.

    I think the challenge would be to have a content admin look to the metatag field in that admin UI for this. I would think this field should be right next to the title/label field.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Thanks for the feedback @thejimbirch. I partially agree, but on the other hand I think this is mainly a SEO / Site builder issue, nothing for regular editors in most cases. And as such you'd typically look into the Metatags or learn to do so.
    Of course this isn't perfect, but compared to other solutions, I think it's better.
    We should place this "Page title" override in its own fieldset within the Metatags area, definitely.

    Some more notes:

    • I've also done custom field implementations to solve this, but I'd like to provide a general solution for this recurring requirement
    • I still think it's very close to the Metatag functionality and area
    • I'm still open to solve it differently, inspired by metatags
    • I think the discussion shows that it propably shouldn't be part of metatag, but be solved in contrib
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
Production build 0.69.0 2024