I need to add JSON-LD to our backdrop site. We are already using Metatag, so one approach would be to use the Schema Metatag module.

However, it looks like that module has not been ported yet(?)

Has any work been done so far to add JSON-LD to Backdrop?

I've never ported a module, so I'm not sure how much work it would be to port Schema Metatag, and I'm hesitant to jump into a difficult project. There is also a simple D8 module called JSON LD Schema API that would be fine as well since I like the code first approach.

Any ideas on the best way to approach this project?

Here are a few links for reference:

Most helpful answers

OK, I have provided a fix for the problem with metatag. Who knows when it will be merged, but you can patch metatag manually. If you don't know how to do that, let me know.

https://github.com/backdrop-contrib/metatag/issues/110

Schema.org Metatags is now ported, and has an alpha release. I've done limited testing with "Book". Please test other submodules, and post in the issue queue if you find problems:

https://github.com/backdrop-contrib/schema_metatag

Metatag tokens don't work in the main module. I will open another issue for that. Metatag is pretty complex, and it was pretty much abandoned for a long time. Fortunately, maintainers are now fixing things again, but there are some bugs still in that module.

Comments

I know nothing about JSON-LD, but looking at the module Schema Metatag: it looks very straightforward to port. It basically uses ctools plugins, which are replaced in Backdrop with a similar functionality by Plugin Manager. Additionally it loads a bunch of inc files with all the classes. Those need to be declared in hook_autoload_info() instead of loading them in the info file. 

Without having looked in depth, I believe the port can be done in less than 1 hour.

That's great to hear. Thank you for the speedy reply.

Is there a way for us to fund the port of this module and if so how should I proceed? I am happy to test as needed.

I've done an initial port, and done some very limited testing. The new tags provided by each of the Schema metatag modules are added into the "Enabled metatags" under admin/config/metadata/metatags/settings

Those can be enabled, and added to tags provided by Content or other entities. 

I put this in my personal repo. Once you test I can move this to the backdrop-contrib repo.

https://github.com/argiepiano/schema_metatag

The port was simpler than I thought, as there wasn't a need to use Plugin Manager. Metatag takes care of adding the new provided plugins.

Oh wow - thank you!

I will install the module now, do some testing tomorrow, and report back.

Thanks again for the super fast port.

OK I spoke too soon. I've played a bit with it and am not getting any traction. The code was apparently simple to port, but the functionality is much trickier to test. It's hard to tell if this is because of schema_metatag, or because of the main metatag module. Metatag is a module with problems in Backdrop. It's been fixed somewhat recently, but it still has some issues.

Anyway, if you want to try and test, go ahead. I'll move to Backdrop contrib just to allow issues there. Perhaps someone who uses metatag more than I do has the time to fix those.

FTR, a bit of debugging is showing that the tags are being attached and sent to theme_head_tag() in an incorrect format. So perhaps the solution lies in checking why this is happening. My suspicion is that the main metatag module is based on an earlier version of the D7 module, and schema_metatag needs the current version - which makes debugging tricky, since the problem may reside outside schema_metatag.

I've made some progress today - found some missing class autoload declarations which were affecting the functioning of the module. Haven't given up yet! I'll be trying a bit more in the next couple of days.

Do not bother testing the version that's there now. It doesn't work yet.

I appreciate the effort - thank you!

I gave it a quick try last night but ran into a "Using $this when not in object context" error on pages that were configured to use schema_metatag.

I will wait for now. If there is anything I can do to help, please let me know.

 

daveb, I made tons of progress. The main issue is with the main metatag module, which does things differently from D7. See an issue I opened there:

https://github.com/backdrop-contrib/metatag/issues/110

I will be providing a PR there. Once I do that, do you feel comfortable patching metatag with that PR, and then testing schema_metatag?

OK, I have provided a fix for the problem with metatag. Who knows when it will be merged, but you can patch metatag manually. If you don't know how to do that, let me know.

https://github.com/backdrop-contrib/metatag/issues/110

Schema.org Metatags is now ported, and has an alpha release. I've done limited testing with "Book". Please test other submodules, and post in the issue queue if you find problems:

https://github.com/backdrop-contrib/schema_metatag

Metatag tokens don't work in the main module. I will open another issue for that. Metatag is pretty complex, and it was pretty much abandoned for a long time. Fortunately, maintainers are now fixing things again, but there are some bugs still in that module.

argiepiano - yes, great progress!

Thanks for the all the updates and detailed info.

Yes, I can test tomorrow with a patched version of metatag. If you can let me know the best way to apply the patch, that would be great. I am using git for other stuff, so I should be able to figure it out.

> Metatag tokens don't work in the main module.

I notice when using the "Browse available tokens" feature when configuring metatags, you get a message about metatag tokens: "Generated by the Metatag module, may not be used to fill in other meta tags." (see screen below). I'm not sure where that language originates and what the expectation should be for using metatag tokens for schema metatag fields.

That language basically mean that you can't use [node:metatag:title] for example in a metatag field. That would create an infinite loop. So, you CAN use that token in other places, but not in any of the fields you get when configuring metatags here: admin/config/metadata/metatags/config/node and in other places.

If you tell me what you are looking to do, maybe I can offer some clues.

As for patching metatag in your site, download the patch file here:

https://patch-diff.githubusercontent.com/raw/backdrop-contrib/metatag/pu...

Place the file in modules/metatag, and then, within that folder, using shell, run patch -p1 <NAME_OF_THE_PATCH_FILE

Hi argiepiano,

On a local test site, I have metatag patched, and schema_metatag and schema_article enabled. 

I did a quick test, enabling a few article fields from schema_metatag on the article content type and I can see json-ld content appearing inside <script type="application/ld+json">. Cool!

I will do more testing soon. Thanks again for all the work you did to get this working!

Dave

I enabled quite a few more article fields, and configured images with width and height, date stamps with custom formats, and more. Have only tested the article type at this point. No problems or errors so far, seems to work great! 

Hi Alejandro,

I found time for a bit more testing of the 'article' schema type today and ran into a new error:

Warning: strtolower() expects parameter 1 to be string, array given in strtolower() (line 2237 of /var/www/html/web/modules/contrib/metatag/metatag.module). =

This happens because the $attr_value delivered to strtolower is an array for things like the 'organization' and 'ImageObject'. 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.

Any ideas for a simple fix?

Dave

OK, now I can't repro this error, so don't worry about investigating!

I will post again if I can figure out what causes the error.

Dave, can you please create an issue in the issue queue for the module? It's easier to address problems that way.  I can take a look later.

Yes, will do. Thanks, Alejandro!

Alejandro,

I just wanted to let you know that schema_metatag has been working great so far. Thanks again for your help in getting things ported and running!

Dave