-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Process '#attached' key in metatag data before adding tags to the head #110
Comments
…ocess it correctly Fixes backdrop-contrib#110
I've been testing a patched version of Metatag (#110) with Metatag schema, with the 'article' schema type. In general, it's working well and I see schema tags in json-ld format appear in the of pages configured to use the article schema type. However, while testing yesterday, I ran into a new error:
This happens because the $attr_value delivered to strtolower can be an array for things like the 'organization' and 'ImageObject' that are part of the 'article' spec. I'm not sure why I didn't see this before, but I noticed after clearing caches, the error doesn't always appear right away. If I hack the code to check for a string:
The error goes away. Let me know if you need more details or if I should open a separate issue. |
@argiepiano Thanks for the PR! I'll be taking a look at this shortly. Can either you or @dgbruns provide some steps to reproduce the problem? |
@quicksketch - just sent over some info and json config. The gist is that I am testing the 'Article' type only at the 'Content' level in Metag with the patched version of Metatag that uses backdrop_render. It seems that the error above happens when pieces of the json-ld array (the sub-arrays for persons and organizations) hit strtolower. |
Thanks @argiepiano and @dgbruns. I've reproduced the problem with the @dgbruns I think your little fix is also totally okay. I'd like to have automated test coverage for the variety of situations we're putting forward, but it seems like the Metatag module tests need some work. I'll put up a combined PR that includes #111 plus @dgbruns's fix, and if possible, some test coverage. |
@argiepiano I see your point about needing to support |
I checked metatag's handling of So in summary to fix this issue, I think we need to do both of these things:
|
OK. I'll close my PR for this issue then. |
Perfect, and I see you merged backdrop-contrib/schema_metatag#1. I've merged #117 as well and I'll make a new release shortly. |
Backdrop's version of metatag is different from D7's version in one important way: B's version directly adds the head tags in
metatag_preprocess_page()
. This creates serious problems for metatag data that make use of the#attached
key.One example of that is the data provided by "Canonical URL" metatag in class
BackdropLinkMetaTag
. The data returned bygetElement
in this class looks a bit like this:This data fails to have any effect when it's run without any preprocessing through
backdrop_add_html_head($data, $tag);
inmetatag_preprocess_page()
. The result of doing it this way (i.e. ADDING the data to the head BEFORE running the#attached
function) is that, in this case, the Canonical URLLink
header is never sent to the browser, as reported in #109.There are TONS of other metatags, provided by a module I'm porting called
schema_metatag
, that make use of the[#attached]
key. Because of the current approach taken by Backdrop (i.e. using a "shortcut" and adding the tags directly inmetatag_preprocess_page()
, instead of doing it the way D7 does it), those tags fail to be added, and in fact crash the rest of the tags added later, because they result in malformed<meta>
tags being added.There is a solution that avoids completely rewriting
metatag_preprocess_page()
. The solution is to check if the metatag data contains[#attached]
as the top-most element. If it does, instead of directly adding the "raw" data, you callbackdrop_render()
for that data. Backdrop is smart enough to call the attached function with the arguments provided in the data, AND adds the tag to the head without the need of manually usingbackdrop_add_html_head()
.This solution solves not only the Canonical URL problem, but also all the
schema_metatag
malfunctioning tags.I'll provide the PR later tonight. It's a one-liner.
The text was updated successfully, but these errors were encountered: