Submodule to override the (visible) page title

Created on 19 June 2024, 6 months 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.

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

Merge Requests

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
  • πŸ‡©πŸ‡ͺGermany Grevil

    I think that a submodule could be a valid option to resolve this issue!

    If Damien doesn't like it, we can always port it as a contrib module!

  • Merge request !136First stab β†’ (Open) created by Anybody
  • πŸ‡©πŸ‡ͺGermany Grevil
  • Pipeline finished with Success
    5 months ago
    Total: 276s
    #246620
  • Status changed to Postponed 5 months ago
  • πŸ‡©πŸ‡ͺGermany Grevil

    Alright, we discussed this internally and since there is currently no proper way to override the raw page heading (h1) before it gets rendered through twig and the fact, that we abuse the metatag system (Manually dettaching the "page_heading" meta tag so it won't be rendered as a real meta tag), this shouldn't be implemented as an official metatag submodule, but rather as a seperate "dirty quickfix" contrib module.

    Once πŸ“Œ Implement a central point to override the raw page heading (h1) (e.g. hook_page_heading_alter()) Active is fixed, we could rethink implementing this as an official submodule, but until then, we should set this POSTPONED.

  • Pipeline finished with Failed
    5 months ago
    #246850
  • Pipeline finished with Failed
    5 months ago
    #246880
  • πŸ‡©πŸ‡ͺGermany Grevil

    Here is the contrib module, if anyone is interested: https://www.drupal.org/project/metatag_page_heading β†’ .

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

    GREAT work @Grevil! That's super useful, I could have used it many many times already, thank you! :) πŸŽ‰πŸŽ‰πŸŽ‰

    Maybe others are also interested to give it a try?

    So let's keep this postponed until one day @DamienMcKenna wants it in Metatag (or not) ;)

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

    @damienmckenna should we leave this in contrib and simply link it on the module page?
    Or how would you like to proceed here? We can of course also merge it into Metatag, if you'd lke to, but I think it might be quite specific.

Production build 0.71.5 2024