Miguel Rodríguez does effective software development.

Solutions to implement Microdata + Schema.org in Drupal

This tutorial was tested only on Drupal 7
Drupal is one the the best CMS available right now, it has great capabilities by default, and if it lacks of something, you can improve its capabilities with a bunch of modules available, ranging from APIs to themes, however, although Drupal has been improved outrageously, there are still some things left, one of these things to improve has to do with the RDFa module, this module is used to define machine-readable data (often called rich snippets) but it has some problems with HTML5 themes, unfortunately, there is no replacement for this module, and there are no modules that can solve this problem totally, but there are other simple ways to define the rich snippets, have you heard about Microdata and Schema.org?, Microdata is a new specification promoted with HTML5.
 
There are two ways to use microdata + Schema.org to define the rich snippets as well as getting rid of RDFa module, the first one is using the Microdata module (Drupal), it has a way to implement Schema.org to the content types and fields, however, it still does not support some datatypes and it might be unstable yet (you better test it yourself), the second one is to add/modify the templates in order to set the rich snippets by yourself, it might be more work but you will have more control, additionally, you can do even more things like setting hidden data, using currently displayed information and much more, besides, you are not supposed to change or define rich snippets all the time, so why not doing it by templating.
 

First alternative: Microdata (module)

 
There is a module available for Drupal that can implement Microdata to the content types and its fields, however, it still does not support some datatypes, despite of this, it is a good way to define the rich snippets with Microdata. It is somewhat rudimentary, it works directly with the content types, so if you need to define something more, I fear you can not do it with this module, the datatypes are specified directly in a text input (no select box or anything else) which is not validated, so you might need some knowledge about Microdata and Schema.org, despite of its limitations, it is pretty simple to use (at least for me), just specify the datatype for a given content type, specify the items for each field and you are done.
Drupal - Microdata (module)
A simple way to implement Microdata+Schema.org is using a module like Microdata (module)
Attribution: 
 

Second alternative: Template override

 
Although Microdata (module) seems to be pretty interesting, it still could be somewhat unstable, not customizable and you can not define hidden data with it, however, if you override some templates you can do all that and more, besides, how many times do you need to define the rich snippets?, I would say just once, but even if someone needs to do that more than once, it would be just a few times, you are not supposed to change the rich snippets every day.
 
We are going to define some common rich snippets by template override, this would help to know exactly how to implement Microdata/Schema.org using this method, we are going to define some rich snippets such as; WebSite, BlogPosting, Author and articleBody.
 
In order to define the rich snippets with Schema.org, some key templates must be overridden such as the HTML template (html.tpl.php), some content types (node.tpl.php), and some fields (field.tpl.php, specially the body field), additionally, we are going to define a function to define every image with Schema.org.
 

An overview to the theme files

 
Before doing anything, we must check all the rich snippets that we can define according to the templates that we can override, so we will know the templates that we have to override exactly, this is an overview of the most basic templates that we can create.
Theme files overview
Attribution: 
Basically, we must have a list of all the rich snippets that want to define in order to find where the data is being displayed in the template and define it properly, for this you must be knowledgeable about the structure of Drupal and how to theme it, usually the HTML and body tag are defined along with the page content in the html.tpl.php file, the page content which includes many regions such as the content itself, the header, the footer and the sidebars, is defined in the page.tpl.php file, the node.tpl.php file prints the content of every node which may include its respective fields, this content is the real content in Drupal, the fields are defined in the field.tpl.php file (although it is not in the image above it exists and it is important, at least for me) including the body field which has the main content of the node, these kind of things have to be known in order to define the desired data.
Microdata and Schema.org let you define any data within a HTML element through a set of attributes, there are pretty basic datatype such as "Date" or "Number" which exists just to hold simple data as numbers, dates or pieces of text, there are global datatypes such as "WebSite" and "WebSiteElement" (and all their derivates) that are used to represent general entities enclosing data that has not to be associated as theirs. There are more specific datatypes such as "Person", "Organization" and "Places" which include several items with fully detailed data, once you know which data to define and where it is located, you simply have to add the proper attributes.
 

An overview to Microdata and Schema.org

 
Schema.org is a new set of datatypes for Microdata, Microdata is a new specification that allows to define machine-readable data in HTML documents, it is especially optimized for HTML5, and is pretty simple to use. Paraphrasing the use of Microdata/Schema.org, Microdata works basically with three attributes, "itemscope", "itemtype" and "itemprop", the "itemtype" attribute defines the datatype of a given item (HTML elements with Microdata attributes are going to be called items from now on), the URL of the datatype is supposed to be set here, all the datatypes of Schema.org can be found in the "The type hierarchy" section of its documentation, interestingly, all the URLs of each datatype shown in that page are the real URLs to be set in that attribute. Most of the items with a defined datatype contain a set of children items, not just simple data, for example, the items with "person" datatype may hold name, email, telephone and much more, in these cases the "itemscope" attribute should be set, the "itemscope" attribute indicates the items which have children items, it is very likely that the "itemscope" attribute is set along with the "itemtype" attribute. Every child item must have an "itemprop" attribute in order to indicate what item it is representing, for example, the item representing the name of another item must have the attribute "itemprop" with the value "name" in order to indicate it is a specific child of a parent item, if the child item has a datatype, it should have an "itemtype" attribute (and an "itemscope" attribute if it has children items as well), be aware of the items with an "itemscope" attribute, they must have at least one children item if not they will not be recognized, some datatypes will require specific items such as the "name" item (e.g.: "Person" datatype), it is encouraged that you define the "name" item when possible.
  1. <!-- a Person datatype item without any parent item -->
  2. <div id="element" class="element" itemscope itemtype="http://schema.org/Person">
  3.  
  4. ...
  5.  
  6. <!-- a paragraph with two children items (name and jobTitle) enclosed by span tags whose parent is the Person datatype item -->
  7. <p>Hello I am <span itemprop="name">Miguel Rodriguez</span>, a freelance <span itemprop="jobTitle">Developer</span>.</p>
  8.  
  9. ...
  10.  
  11. <!-- an Organization datatype item, it represents the 'worksFor' data of the Person datatype item -->
  12. <div itemprop="worksFor" itemscope itemtype="http://schema.org/Organization">
  13.  
  14. <!-- a child item, it is the 'name' data of the Organization datatype item -->
  15. <div itemprop="name">Independent Contractor</div>
  16.  
  17. </div>
  18.  
  19. </div>
This is an use example of Schema.org, there is an item with a "Person" datatype holding three children, the "name", "jobTitle" and "worksFor" items, all these children items are indicated by the "itemprop" attribute, the "worksFor" item has an "Organization" datatype holding a "name" item as well. Not writing the whole URL of the datatype is a common error (e.g.: you must write "http://schema.org/Person" not just "schema.org/Person"), you should also make sure you validate the rich snippets with an online validator such as the Google Structured Data Testing Tool
 

The HTML template: html.tpl.php

 
In the HTML template you can find the HTML & Body tag and the HTML elements enclosing the page content, although there is nothing more, this template is perfect to set a "WebSite" datatype item as well as any other global datatype items to enclose the content, the following is the possible structure of the HTML template.
  1. <!DOCTYPE html>
  2.  
  3. <html ... >
  4.  
  5. <head ... >
  6.  
  7. <?php print $head; ?>
  8. <title><?php print $head_title; ?></title>
  9.  
  10. ....
  11.  
  12. <?php print $styles; ?>
  13. <?php print $scripts; ?>
  14.  
  15. ....
  16.  
  17. </head>
  18. <!-- you can place a datatype in the Body tag -->
  19. <body ... itemscope itemtype="http://schema.org/WebPage" >
  20.  
  21. ...
  22.  
  23. <?php print $page_top; ?>
  24.  
  25. ...
  26.  
  27. <!-- or you can make a content wrapper -->
  28. <div class="wrapper" ... [itemprop=" ... "] itemscope itemtype="http://schema.org/WebPage" >
  29.  
  30. <?php print $page; ?>
  31.  
  32. <meta itemprop="name" content="WebPage Name">
  33. </div>
  34.  
  35. ...
  36.  
  37. <?php print $page_bottom; ?>
  38.  
  39. ...
  40.  
  41. </body>
  42. </html>
Let us say this is the structure of the html.tpl.php file (the html.tpl.php file will never look the same, whether you are using a theme built from scratch or a subtheme), as it was mentioned before, this template is perfect to place global datatype items to enclose all the content.
 

The Page template: page.tpl.php

 
Some website elements such as the header, the footer and the sidebars are defined in the Page template, the content is defined here as well (actually, it is just a wrapper but the content is inside anyway), in this template you can define all those elements as the "WPHeader", "WPFooter" and "WPSideBar" respectively, this would be an example of a Page template (remember that all the templates may change depending on the base theme you are using, if you are using a base theme of course).
  1. ...
  2.  
  3. <div id="page">
  4.  
  5. ...
  6.  
  7. <div id="header" ... itemscope itemtype="http://schema.org/WPHeader">
  8. ...
  9. <meta itemprop="name description" content="Header">
  10. </div>
  11.  
  12. ...
  13.  
  14. <div id="content" ... >
  15. ...
  16. <?php print render($page['content']); ?><!-- your content goes here -->
  17. ...
  18. </div>
  19.  
  20. ...
  21.  
  22. <div id="sidebar-first" ... itemscope itemtype="http://schema.org/WPSideBar">
  23. ...
  24. <meta itemprop="name description" content="Left Sidebar">
  25. </div>
  26.  
  27. ...
  28.  
  29. <div id="sidebar-second" ... itemscope itemtype="http://schema.org/WPSideBar">
  30. ...
  31. <meta itemprop="name description" content="Right Sidebar">
  32. </div>
  33.  
  34. ...
  35.  
  36. <div id="footer" itemscope itemtype="http://schema.org/WPFooter">
  37. ...
  38. <meta itemprop="name description" content="Footer">
  39. </div>
  40.  
  41. ...
  42.  
  43. </div>

The Node template: node.tpl.php

 
All the content in Drupal is displayed as Nodes, including every content type, data such as the title (if is there any), the author, the author picture and creation date can be found in this template, depending on the content, you can override the Node template to define any "BlogPosting" or "Article" item, although you can define it in the Page template as well, defining it in the Node template is encouraged.
Not all the data is shown, there are some data that you must define by using the variables that the template provides without showing them, in order to do this the data must be defined in HTML meta elements, these metas must be placed inside an item which is going to represent the "author" of the "BlogPosting" or "Article" item, according to the "Article" datatype (the "BlogPosting" datatype inherits all its properties) the "author" child item must be a "Person" datatype, therefore a "Person" datatype item is going to be created enclosing the user's picture (that would relate the picture with the author automatically, please check the section "adding the images" bellow) and all the hidden data is going to be defined after it (this article does not cover how to get the author's data). 
 
  1. <div id="node-<?php print $node->nid; ?>" ... itemscope itemtype="http://schema.org/BlogPosting" ... >
  2.  
  3. ...
  4.  
  5. <h2<?php print $title_attributes; ?>><a href="<?php print $node_url; ?>"><?php print $title; ?></a></h2>
  6.  
  7. ...
  8.  
  9. <div itemprop="author" itemscope itemtype="http://schema.org/Person">
  10.  
  11. <?php print $user_picture; ?>
  12.  
  13. <div class="submitted">
  14. <?php print $submitted; ?>
  15. </div>
  16.  
  17. <meta itemprop="name" content="author's name">
  18. <meta itemprop="url" content="author's URL">
  19. <meta itemprop="email" content="author's email">
  20.  
  21. </div>
  22.  
  23. ...
  24.  
  25. <div class="content clearfix"<?php print $content_attributes; ?>>
  26.  
  27. ...
  28.  
  29. <?php print render($content); ?>
  30.  
  31. ...
  32.  
  33. </div>
  34.  
  35. ...
  36.  
  37. <meta itemprop="name" content="<?php print $title; ?>">
  38. <meta itemprop="dateCreated" content="<?php print $date; ?>">
  39.  
  40. </div>
Although you can set a datatype to all the nodes, setting specific datatypes to the nodes according to the content type is encouraged, you can override the template for specific content types by naming the template file as this:
  1. node--[content type].tpl.php
For more information visit the Drupal documentation page about template (theme hook) suggestions.
 

The Fields template: field.tpl.php

 
Every single field in Drupal can be customized with this template, there are some fields that come by default such as the body field, generally, the main content comes in this field, this makes it perfect to define items such as the "articleBody" or other item that comes inside the "Article" item.
  1. <div itemprop="articleBody" class="<?php print $classes; ?>"<?php print $attributes; ?>>
  2.  
  3. ...
  4.  
  5. <?php if (!$label_hidden): ?>
  6. <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  7. <?php endif; ?>
  8.  
  9. ...
  10.  
  11. <div class="field-items"<?php print $content_attributes; ?>>
  12.  
  13. <?php foreach ($items as $delta => $item): ?>
  14. <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
  15. <?php endforeach; ?>
  16.  
  17. </div>
  18.  
  19. ...
  20. </div>
The same situation happens with the fields, Although you can define an item to all the fields, setting specific item to the fields according to the content type is encouraged, you can override the template for specific content types as this:
  1. field--[field name]--[content type].tpl.php
For more information visit template (theme hook) suggestions.
 

Defining more information

 
Not all the information can be added just by templating, there are some other things to do in order to add the rest of the information such as the images and the ads, for example, some information may be contained in the blocks, some other information, as the images, can not be accessed that easy.
 

Adding the images

 
The information of the website and its articles have been added, however, the images are not, they must be added and associated with the webpage, as you may know, this is not something that can be done merely by overriding a template, in order to do this we must add a function which is going to add the microdata attributes automatically.
  1. function THEME_preprocess_image(&$vars)
  2. {
  3. $vars["attributes"]["itemprop"] = "image";
  4. }
All the images would be associated with their respective parent item automatically, so, if is there an image inside the "WebPage" item, that image will be associated with it, if is there an image inside the "BlogPosting" item, that image will be associated with it (not with the "WebPage" item), and so on.
 

Adding the Ads

 
You can also define the ads, in order to do this, they should have been added in a block with "full html" filtering without any text editor (wysiwyg), you should not modify the ads but you can add the ads in a container that you can create with the proper attributes.
  1. <div itemscope itemtype="http://schema.org/WPAdBlock">
  2.  
  3. <script type="text/javascript" ... > ... </script>
  4.  
  5. ...
  6.  
  7. </div>
By doing this, all the ads are going to be defined with Microdata + Schema.org, it is important to define as much data as possible, even if it means adding all the ads.
 

Adding information in the fields (wysiwyg)

 
This is somewhat cumbersome because there are no text editors which allow adding microdata information comfortably, but is still possible as long as you know HTML and that you can insert full HTML inputs, basically you just have to remember how Microdata and Schema.org work, just select a piece of information and enclose it in span tags, if it is an item which is going to have more items add an "itemscope" attribute, if it is a child of a parent item add an "itemprop" attribute with the name of the item it is representing, if it has a specific datatype (either required by its parent item or not) add an "itemtype" attribute with the proper datatype.
 

Benefits and Limitations

 
Both possibilities have benefits and limitations that you might consider;
 

Using Microdata (module);

Well, using this module would be the best for people who does not know how to code or people who like things easy, you can define a lot of data without hassle, besides, this might be ideal if you need to add/change data often with Microdata and Schema.org (this is hypothetical, I still do not know why would you do this), although it has some limitations is still a great alternative.
 
Benefits
 
  • It is a straightforward process.
  • No coding skill required (although you should know how Microdata and Schema.org work).
  • Many data types can be used.
 
Limitations
 
  • It can only specify data between content types and its fields.
  • Although it supports many datatypes, there might be some datatypes that it can not be used.
  • Hidden data can not be specified.
  • There are several data outside the content types that can not be defined.
 

Through theming;

In my personal opinion this is more a theming solution than a module solution, this is something that must be planned and not "on the go", if you need to define the data thoroughly (all the items that you can including hidden data) perhaps doing this is the best solution.
 
Benefits
 
  • The data can be thoroughly specified.
  • Any datatype can be used.
  • hidden data can be defined.
  • You can define other items such as the header, footer, sidebar, ads and many other things.
 
Limitations
 
  • It requires coding skills.
  • It might be hard and complex.

About me

About the author
Miguel Rodríguez is a freelance developer from Caracas - Venezuela. He is a persistent and goal-oriented person who tries to make the best use of the resources. He is positive, proactive and motivated enough to set a goal with the intention to fulfill it. Find more about him .