- 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 theEntity: Set field value
action. - πΊπΈUnited States j-barnes
Yep that is the current solution we are leveraging with the serialization / unserialization - takes a few more additional steps, but it does the trick!
- Status changed to Fixed
5 months ago 3:59pm 1 July 2024 - π©πͺGermany jurgenhaas Gottmadingen
Sounds good, thanks for your feedback. Marking as fixed for now.
Automatically closed - issue fixed for 2 weeks with no activity.