I am creating a decoupled Drupal 8 site and am in need of getting all the metatags for a given node (including the defaults) but by accessing a custom route (say 'mybackend/node/1'
). Currently this is not possible because the metatag_get_default_tags()
function utilizes metatag_get_route_entity()
which in turn does this:
$route_match = \Drupal::routeMatch();
$route_name = $route_match->getRouteName();
// Look for a canonical entity view page, e.g. node/{nid}, user/{uid}, etc.
$matches = [];
preg_match('/entity\.(.*)\.canonical/', $route_name, $matches);
and only detects the entity when we are on a drupal entity path. MetatagManager only allows to get tagsFromEntity()
which does not include defaults.
There is no way of getting those defaults from outside the metatag module in an easy way (without copy pasting the code).
I have thought of two possible ways to approach the issue:
- Make the
metatag_get_default_tags()
accept argument (say$entity
) which would default toNULL
and than do something like that:
$entity = is_null($entity) ? metatag_get_route_entity() : $entity;
when loading the entity inside the
metatag_get_default_tags()
. - Modify the
metatag_get_route_entity()
to utilize an event dispatcher to allow other modules to register their entity path patterns. Or maybe use some kind of plugin here?
The first option is much easier but the second one seems more like a better way to go - just needs some more thought as to how exactly to implement that.
Another thing - shouldn`t the metatag_get_default_tags()
and metatag_get_route_entity()
belong to MetatagManager?
I am willing to implement those - just would like to hear thoughts about it. Is such feature even needed?
Comments
Comment #2
trzcinski.t@gmail.com CreditAttribution: trzcinski.t@gmail.com as a volunteer commentedComment #3
trzcinski.t@gmail.com CreditAttribution: trzcinski.t@gmail.com as a volunteer commentedI have prepared a patch in which I moved the whole functionality of getting default metatags into
MetatagManager
service. I have created a few helper methods that allow for getting smaller pieces for the process as well as allowing to get "Raw" metatag data in form of tag_name:tag_value in case anyone needs that.As a cleanup I replaced a few calls to deprecated functions (that`s why I have added a parent issue).
Comments most welcome.
BTW - that`s just a draft and is definitely not ready to be put into the module. I just wanted to see if that`s the correct approach - in such case I will continue to work on that matter.
Comment #4
DamienMcKennaThanks for that @tom_ek, I'll review it soon.
Just to mention it, when you upload a patch please don't forget to set the status to "needs review", that kicks off the testbots to run the patch against the tests we've written. Also, the "Assigned" field is for when you're actively working on something, it isn't for saying "I worked on this", we can see that from your comment :) Lastly, I use the "Parent issue" field for tracking what changes go in which releases, please use "Related issues" to cross-reference other issues.
Thanks again.
Comment #6
trzcinski.t@gmail.com CreditAttribution: trzcinski.t@gmail.com as a volunteer commentedThanks a lot for the info Damien. I`ll remember that.
I am willing on working on that further. Just would like to hear your thoughts about the idea / the way I started to implement that before digging too deep.
Comment #8
trzcinski.t@gmail.com CreditAttribution: trzcinski.t@gmail.com as a volunteer commentedAdding a fix for the test.
Comment #10
DamienMcKenna@tom_ek: Sorry, I misread your comment and though you were finished. Feel free to reassign it to yourself if you want to continue working on it.
I can't really make recommendations for the best approach, sorry, I don't know D8 well enough to compare approaches.
Comment #11
trzcinski.t@gmail.com CreditAttribution: trzcinski.t@gmail.com as a volunteer commentedRerolled the patch as it stopped applying. @Damien - I am thinking on the matter and will try to implement something soon. Just got a few very busy days.
Comment #12
trzcinski.t@gmail.com CreditAttribution: trzcinski.t@gmail.com as a volunteer commentedComment #14
DamienMcKenna@tom_ek: No worries, any help is be appreciated!
Comment #15
trzcinski.t@gmail.com CreditAttribution: trzcinski.t@gmail.com as a volunteer commentedRe-rolled.
I have removed the parts in metatag.module - this can be added later once there is an agreement on where we go and in the meantime it`ll prevent breaking with each code update :).
Comment #16
trzcinski.t@gmail.com CreditAttribution: trzcinski.t@gmail.com as a volunteer commentedComment #18
trzcinski.t@gmail.com CreditAttribution: trzcinski.t@gmail.com as a volunteer commentedRerolled the patch
Comment #19
chegor CreditAttribution: chegor as a volunteer commentedThe patch is sent for tests
Comment #21
DamienMcKennaAlso needs some tests to make sure the API works as intended.
Comment #22
KarenS CreditAttribution: KarenS at Lullabot commentedIf this was implemented, it could also be used to add JSON LD to a decoupled site. I created an issue for the Schema.org Metatag module at https://www.drupal.org/node/2881928.
I don't have a way to test this ATM I like this idea!
Comment #23
dunklea CreditAttribution: dunklea at Go Overseas commentedI'd also second the usefulness of this functionality. I'm trying to extend the Simple XML Sitemap module to not include content that's been noindex, either on the node or as a default setting.This patch would make this much easier.
Comment #24
drupalmonkey CreditAttribution: drupalmonkey commentedIt seems like this issue is no longer needed?
All of these functions now take an entity parameter:
metatag_get_default_tags()
metatag_get_tags_from_route()
metatag_generate_entity_metatags()
Comment #25
labboy0276 CreditAttribution: labboy0276 at Tandem commentedAs a FYI, the decoupled rendering seems to be solved in this issue: https://www.drupal.org/project/metatag/issues/2945817. I applied the patch in https://www.drupal.org/project/metatag/issues/2945817#comment-12932008 and all my tags are available via the metatag_normalized key in the attributes key.
There also seems to be a couple issues surrounding this same issue / request that could possibly be flagged as duplicates:
https://www.drupal.org/project/metatag/issues/2971192
https://www.drupal.org/project/metatag/issues/2636852
Comment #26
rcodina CreditAttribution: rcodina commentedThis issue can be closed, the
MetatagManager
class now has thetagsFromEntityWithDefaults
method.Comment #27
DamienMcKennaLet's make this a documentation task to provide additional details on how to obtain the meta tags for a given entity.