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:

  1. Make the metatag_get_default_tags() accept argument (say $entity) which would default to NULL 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().

  2. 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?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tom_ek created an issue. See original summary.

trzcinski.t@gmail.com’s picture

trzcinski.t@gmail.com’s picture

I 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.

DamienMcKenna’s picture

Thanks 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.

Status: Needs review » Needs work

The last submitted patch, 3: metatag-move_functions_inside_metatag_manager-2774471-2.patch, failed testing.

trzcinski.t@gmail.com’s picture

Thanks 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.

The last submitted patch, 3: metatag-move_functions_inside_metatag_manager-2774471-2.patch, failed testing.

trzcinski.t@gmail.com’s picture

Adding a fix for the test.

Status: Needs review » Needs work

The last submitted patch, 8: metatag-move_functions_inside_metatag_manager-2774471-4.patch, failed testing.

DamienMcKenna’s picture

@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.

trzcinski.t@gmail.com’s picture

Rerolled 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.

trzcinski.t@gmail.com’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
DamienMcKenna’s picture

Priority: Minor » Normal

@tom_ek: No worries, any help is be appreciated!

trzcinski.t@gmail.com’s picture

Re-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 :).

trzcinski.t@gmail.com’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
trzcinski.t@gmail.com’s picture

chegor’s picture

Status: Needs work » Needs review

The patch is sent for tests

Status: Needs review » Needs work
DamienMcKenna’s picture

Issue tags: +Needs tests

Also needs some tests to make sure the API works as intended.

KarenS’s picture

If 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!

dunklea’s picture

I'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.

drupalmonkey’s picture

It 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()

labboy0276’s picture

As 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

rcodina’s picture

This issue can be closed, the MetatagManager class now has the tagsFromEntityWithDefaults method.

DamienMcKenna’s picture

Title: Get all metatags for a given entity » Document more ways to get all meta tags for a given entity
Component: Code » Documentation
Issue tags: -Needs tests

Let's make this a documentation task to provide additional details on how to obtain the meta tags for a given entity.