Skip to content

Documentation for the Schema.org Metatag contrib module.

Notifications You must be signed in to change notification settings

digitalpolygon/schemaorg-metatag

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 

Repository files navigation

Schema.org Metatag

This project extends Drupal's Metatag module to display structured data as JSON-LD in the head of web pages. Either hard-code properties or identify patterns using token replacements. Using the override system in Metatag module you can define default structured data values for all content types, override the global content defaults for a particular content type, or even override everything else on an individual node to provide specific values for that node.

Read more about Schema.org, JSON-LD, and how this module works in an article on Lullabot.com: Create SEO Juice From JSON LD Structured Data in Drupal.

Since the Schema.org list is huge, and growing, this module only provides a small subset of those values, but it is designed to be extensible. Several types are included which can be copied to add new types (groups) with any number of their own properties.

The module creates the following Schema.org object types:

Many second-level properties are available to use on the above, including:

Using the override system in Metatag module you can define default structured data values for all content types, override the global content defaults for a particular content type, or even override everything else on an individual node to provide specific values for that node. Hard-code properties or identify patterns using token replacements.

Validation

Additional Resources

If you are new to structured data you should definitely read the first reference carefully.

For more information about the Metatag module and how to set it up, see Drupal Metatag Module Documentation.

Known Issues

  • To populate the image width and height properties, use the appropriate tokens. The core Token module provides image height and width token, for example, the token [node:field_image:0:width] will be replaced with the width of the first image in field_image on the current node.

Development Instructions

This module defines Metatag groups that map to Schema.org types, and metatag tags for Schema.org properties, then steps in before the values are rendered as metatags, pulls the Schema.org values out of the header created by Metatag, and instead renders them as JSON-LD when the page is displayed.

The module includes a base group class and several base tag classes that can be extended. Many properties are simple key/value pairs that require nothing more than extending the base class and giving them their own ids. Some are more complex, like Person and Organization, and BreadcrumbList, and they collect multiple values and serialize the results.

The development process for adding groups and properties:

  • Create groups at MODULE_NAME/src/Plugins/metatag/Group and properties at MODULE_NAME/src/Plugins/metatag/Tag. Each tag extends the appropriate base class.

In either case, you should be able to copy one of the existing modules as a starting point.

There is an included module, Schema.org Article Example, that shows how other modules can add more properties to types that are already defined.

Examples and Hints

Using this module, the code in the head might end up looking like this:

<script type="application/ld+json">{
    "@context": "https://schema.org",
    "@graph": [
        {
            "@type": "Article",
            "description": "Curabitur arcu erat, accumsan id imperdiet et.",
            "datePublished": "2009-11-30T13:04:01-0600",
            "dateModified": "2017-05-17T19:02:01-0500",
            "headline": "Curabitur arcu erat]",
            "author": {
                "@type": "Person",
                "name": "Minney Mouse",
                "sameAs": "https://example.com/user/2"
            },
            "publisher": {
                "@type": "Organization",
                "name": "Example.com",
                "sameAs": "https://example.com/",
                "logo": {
                    "@type": "ImageObject",
                    "url": "https://example.com/sites/default/files/logo.png",
                    "width": "600",
                    "height": "60"
                }
            },
            "mainEntityOfPage": {
                "@type": "WebPage",
                "@id": "https://example.com/story/example-story"
            },
        },
    ]
}</script>

Contributing

The composer.json file should be kept normalized using ergebnis/composer-normalize:

  • composer require --dev ergebnis/composer-normalize
  • composer normalize modules/contrib/schema_metatag/composer.json

Credits

The initial development was by Karen Stevenson.

About

Documentation for the Schema.org Metatag contrib module.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •