Creating mappings for taxonomies

Last updated on
13 October 2016

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

There are two ways to map taxonomy to RDF

  1. Each term can be handled as a resource with its own URI.

    For instance, on the standard Article content type, the tags are handled as resources with their own URIs. If you tag your content with the term 'drupalcon', the RDF statements produced would be the following:

    <http://example.com/node/1> dc:subject <http://example.com/taxonomy/term/1>
     <http://example.com/taxonomy/term/1> rdfs:label "drupalcon"
    
  2. In English, this says one of the subjects of node 1 is taxonomy term 1 and the name of taxonomy term 1 is "drupalcon".

  3. Each term can be handled as a string.

    For example, to use Google's Rich Snippets recipe vocabulary, you use v:recipeType which requires that the value be a string.

    <http://example.com/node/1> v:recipeType "brunch"
    

    In English, this says that node 1 is tagged with "brunch" as it's recipe type. There is no relationship defined between the node URI and the taxonomy term URI.

Code for handling taxonomy terms as resources

function example_rdf_mapping() {
  return array(
    array(
      'type' => 'node',
      'bundle' => 'example_node',
      'mapping' => array(
        'example_tags_field' => array(
          'predicates' => array('dc:subject'),
          'type' => 'rel',
        ),
      ),
    ),
    array(
      'type' => 'taxonomy_term',
      'bundle' => example_taxonomy_vocab,
      'mapping' => array(
        'rdftype' => array('skos:Concept'),
        'name'   => array(
          'predicates' => array('rdfs:label'),
        ),
      ),
    ),
  );
}

Code for handling taxonomy terms as strings

To use the taxonomy terms as strings, we don't create a mapping on the field, but just on the vocabulary. The relationship between the node and the vocabulary is added as a predicate on the term, rather than on the field.

function richsnippets_recipe_rdf_mapping() {
  return array(
    // recipe_types is the machine name of the vocabulary.
    'recipe_types' => array(
      'type' => 'taxonomy_term',
      'bundle' => 'recipe_types',
      'mapping' => array(
        // Because this term is not a resource, we do not set an RDF type.
        'rdftype' => array(),
        'name'    => array(
          // This is the relationship between the node and the tag string.
          'predicates' => array('v:recipeType'),
        ),
      ),
    ),
  );
}

Help improve this page

Page status: No known problems

You can: