If there's multiple entities involved in viewed page, metatags are not correctly rendered. This is because $i_will_say_this_only_once is set to TRUE too early in metatag_entity_view, indeed when TRUE this variable forbids the metatags to be set even if metatags have not been set yet.
For instance, if I put a dsm($entity) at the beginning of this function, on a user page I get :
- Object Profile (Profile2 object)
- Object StdClass (my user, where the metas are stored)
- Object EntityForm Type (another object that may contain metatags)
So, if I set $i_will_say_this_only_once = TRUE; just after the first entity, there's no chance that I get my user metas being displayed.
The solution would be to put $i_will_say_this_only_once = TRUE; at the end of the function, so that we are at least sure that metas have been set (but we are not sure that this are the metas we want, if there are concurrent metas provided by multiple entities).
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | metatag-n2162397-18.patch | 8.41 KB | damienmckenna |
| #17 | metatag-n2162397-17.patch | 7.4 KB | damienmckenna |
| #14 | metatag-n2162397-14.patch | 359 bytes | damienmckenna |
| #6 | metatags-multiple_entities-2162397-6.patch | 1.01 KB | roderik |
Comments
Comment #1
damienmckennaThat's part of the problem - if there are multiple entities loading on the one page, how do you tell which one is authoritative? The _metatag_entity_is_page() function should verify that the current page is only appropriate for a specific entity, so unless there's some strange way to have different entities both have the same internal URL, I don't see how this is possible.
If you can provide more details, and better identify a scenario this might actually happen, then I can reexamine it. Otherwise I think this works as it should.
Thanks for taking the time to report it.
Comment #2
hideaway commentedI have similar problem here. I have a content type which is a collection of nodes (article_collection) which contains articles (article). My urls are like /parent_nid/child_nid. Problem is, that I need to show metatags of active child node, and not parent node. How can I do this? I am fighting over a day in metatag_metatags_view_alter hook but even though I am able to load child node without any problem, I cannot find a way to override metatags (to use child node metatags instead of parent node) :(
Comment #3
hideaway commentedOk I solved this with following code (not sure, if this is kosher). There is at least some duplicated code, since you cannot call something like metatag_override_tags($metatags).
Comment #4
pedrosmoker commentedI have a similar problem, where should i put this code?
Comment #5
damienmckenna@pedrosmoker: You need to create a custom module to put the code in.
Comment #6
roderikI hope I can reopen this issue because it's not got any code attached to it yet - and I _think_ it directly addresses the original reported issue:
Suggested extra rule: if an entity is not configured to have metatags in the first place, then never regard it as authoritative. This probably does not solve all possible exotic problems, but it does solve the profile vs. user problem. (In theory) it also offers more consistent behavior when a pair of entities with metatags enabled & disabled, are somehow entity_view()ed in a different order.
And I think/hope that this behavior change does not have an adverse effect on other situations.
At the moment, this happens on my site:
$i_will_say_this_only_once = TRUEis set.Attached patch fixes that. There are a dozen small things that could be done differently (like the location of metatag_entity_supports_metatags(), the fact that that is now called twice, code inside an if{} vs a return statement inside an if{}... but this is the cleanest patch I could produce without knowing all code details/preferences.
PS if you agree and want a test, feel free to tell me. My 'drupal volunteer time' is sparse atm but I'll get around to it sometime.
Comment #7
damienmckenna@roderik: Nice patch! I'll give it a more detailed test shortly, but this seems like a great improvement.
I think it'd be useful to add some tests to confirm the logic works correctly.
Comment #8
damienmckennaComment #9
damienmckennaI'm trying to work out what could cause this error to happen in the first place - would you happen to steps on how to configure Profile2 so it triggers the bug? Thanks. I'm trying to write some tests to cover the scenario.
Comment #10
damienmckennaHoping to finish this for the next release.
Comment #11
damienmckennaChanging this to Needs Work as we (I) need to write tests to make sure it works correctly.
Comment #12
roderikThanks for your ping via message; it's unfortunately necessary these days.
I'm taking the short route and not trying to write/test a test myself; just describing what I think should work, from what I was seeing on the live site I was tingering with.
Steps to reproduce:
* Enable metatags for user entities (if that isn't on by default - hey, I'm clueless.)
* Create a profile2 entity type/bundle. (if there isn't a default one after enabling the module. Again, clueless.) (Not sure if it should have a field or can work without. No metatag configuration here; metatags will/should be disallowed for this profile2 type.)
* Create/save a user, with an explicit (e.g.) description metatag value.
* Create/save a profile for this user.
* View the user page; assert that the <meta name="description" content=whatever> is present.
Assertion is expected to fail without the patch (because, as far as I can see, the code is set up such that the profile2 entity is always entity_view()ed before the user entity) -- and to succeed with the patch.
I just confirmed this on my site after upgrading it to 1.16 stable.
Thanks for the confirmation my fix is probably the right thing to do.
Comment #13
damienmckenna@roderik: Thanks for the info, I thought there might have been more to it ;-) I'm working on some tests, will have something shortly.
Comment #14
damienmckennaStep 1: a patch to add the Profile2 dependency.
Comment #16
damienmckennaPatch #14 has been committed so that we can then add tests.
Comment #17
damienmckennaHere's a test to confirm that the problem exists.
Comment #18
damienmckennaAnd now with the original patch to confirm the bug is fixed.
Comment #20
damienmckennaThis is where I run around screaming because it all works correctly. And then eat ice-cream.
Comment #22
damienmckennaCommitted. Thanks roderick!