Adding structured data to Drupal 7 nodes

All snippets are outputted using ld+json since this is the prefered method of Google.

Specifying Your Organization's Logo, contacts and social profiles

This one is quite easy to implement, just add a snippet into html.tpl.php, if the file is not in your theme make a copy and add it to your theme.

Add the end of the file you can add the following snippet, make sure to change all URLs, phone number and languages. You can add multiple contact points, more info can be found at https://developers.google.com/structured-data/customize/contact-points.

The snippet also adds links to social profiles connected to the company.



Include Your Site Name in Search Results

Adding the site name can be done in a similar way, add the following snippet into html.tpl.php at the bottom.



Enabling Rich Snippets for blog items

I added snippets for our blog post, this can be done in multiple ways, since we were already using displau suite, we created a custom field, inside the callback function we added the following code. We first check the node type and the use entity_metadata_wrapper to easily fetch the values we need.

All data is added to an array, which will be converted to a json object. Since image is a mandatory field, we reset the variable if no image is present. You can add multiple images if you want to, but no idea what google is going to do with them.


  $output = '';
  $node = $ds_field['entity'];

  if ($node->type == 'blog') {
    $wrapped = entity_metadata_wrapper('node', $node);

    $data = array(
      '@context' => 'http://schema.org',
      '@type' => 'CollectionPage',
      'headline' => $wrapped->title->value(),
      'datePublished' => date('c', $wrapped->created->value()),
    );

    $intro = $wrapped->field_intro->value();
    if (isset($intro['safe_value'])) {
      $data['description'] = $intro['safe_value'];
    }

    $image = $wrapped->field_images[0]->value();
    if (isset($image['fid'])) {
      $data['image'] = array(image_style_url('blog_800', $image['uri']));
    }
    else {
      $data = '';
    }

    if ($data) {
      $output .= '';
    }
  }

  return $output;

Breadcrumbs

You can add custom breadcrumbs, since our site doesn't display any breadcrumbs we decided to add them using ld+json.

Make sure to use t() so all labels are translatable when using a multilingual sites.


  $output = '';
  $node = $ds_field['entity'];

  if ($node->type == 'blog') {
    $wrapped = entity_metadata_wrapper('node', $node);

    $breadcrumb = array(
      '@context' => 'http://schema.org',
      '@type' => 'BreadcrumbList',
      'itemListElement' => array(
        array(
          '@type' => 'ListItem',
          'position' => 1,
          'item' => array(
            '@id' => url('', array(
              'absolute' => TRUE,
              'https' => TRUE,
            )),
            'name' => 'Attiks',
          ),
        ),
        array(
          '@type' => 'ListItem',
          'position' => 2,
          'item' => array(
            '@id' => url('blog', array(
              'absolute' => TRUE,
              'https' => TRUE,
            )),
            'name' => t('Blog'),
          ),
        ),
      ),
    );

    $output .= '';
  }

  retun $output;

Validate your snippets

Make sure all snippets are valid, you can use Google's Structured Data Testing Tool

References

Drupal module

Zoltan Kisgyorgy is working on a Drupal module, Structured data module.

Comments

Hi there! If interested in making multilingual Drupal 7 sites, I recommend trying a collaborative translation management platform like https://poeditor.com/. It is an online service that can help users simplify their workflow, reducing a lot of time.