Update metatags actions

Created on 14 June 2024, 17 days ago
Updated 22 June 2024, 9 days ago

Problem/Motivation

We have a project where we use Augmentor AI to generate content for metatags (like description) and set the tag using text from the rendered entity that was fed into the AI. We have accomplished this using the ECA's module serialization / serialization, but it adds an extra layer of complication. For example it's difficult to test if the description is empty in the given format.

Currently, as far as I know this module only triggers on alter events of tags, not for example a presave of a node. Would it be worth considering to add an action to allow the user to set / update these tags as the metatag module does not provide tokens for this (only in the context of current page).

Example of our current flow:

Use case example:

πŸ’¬ Support request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States j-barnes

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

Comments & Activities

  • Issue created by @j-barnes
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    The problem is, that we only have access to metatags data model, when they dispatch the hook_metatags_alter hook. That's the only place where other modules, e.g. ECA, have a chance to change data that the metatag module has under their control.

    But I assumed that if an entity gets updated, that the metatag module then also dispatches that hook, so you should still be able to take action at that event.

  • πŸ‡ΊπŸ‡ΈUnited States j-barnes

    That makes sense, I was afraid that might create an endless loop - but it looks like metatag doesn't resave the entity after setting the tag. However this means that it also does not persist the data? It looks like this may be a better use case during the page processing. Not a huge deal though, we can still achieve the result with a presave / serialize action. Thanks!

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    The way this seems to work is that metatag data is a content entity of its own which is referenced by the content entity that you're editing. As that reference doesn't change, just the metatag entity, they can save that without triggering another entity save for the parent. So, using that event should also ensure that the data is persisted.

  • πŸ‡ΊπŸ‡ΈUnited States j-barnes

    Gave it another go, but looks like it works only on the page attachment / page processing and does not persist on the field.

    Here is the full example:

    Looks like it only triggers an alter event on update if there is already a value, so in that case I put a random description.

    Simple alter tag event, we set the new tag.

    This updates correctly on the page load / source code with the new value, but it does not persist in the actual field. So in order to leverage this our AI would need to be called on each page hit, which would be a lot.

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    Looking into the metatag.api.php file of the metatag module, it looks like they call the alter hook at various places, e.g. when processing tokens. But not during saving or updating their metatag entities.

    Now, that makes we wonder, if you couldn't start with the Presave content entity event and store the value you computed to the metatag field of your node with the Entity: Set field value action.

Production build 0.69.0 2024