Defining a new RDF mapping

Last updated on
21 December 2016

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

This document is a work in progress and is rapidly changing.

Before start mind this: The rdf mappings are stored in a database table called rdf_mapping as BLOB. Saying that rdf mappings are not entity fields and are only attached to the entities on the display.

Default mappings for entity types

Default RDF mappings can be defined for entity types. For instance, a default RDF mapping is defined for the node entity type.

function node_rdf_mapping() {
  return array(
    array(
      'type' => 'node',
      'bundle' => RDF_DEFAULT_BUNDLE,
      'mapping' => array(
        'rdftype' => array('sioc:Item', 'foaf:Document'),
        'title' => array(
          'predicates' => array('dc:title'),
        ),
...

When you install Drupal and the 'article' and 'page' bundles are created, they inherit the default mapping.

Bundle specific mappings

When you create a new bundle in a module, you can define an RDF mapping for that bundle by using hook_rdf_mapping(). This will override the entity type's default mapping.

For instance, you could create a FOAF file by creating a FOAF bundle, mapping its rdftype to foaf:Person, and mapping the title to foaf:name.

function foaf_rdf_mapping() {
    return array(
      array(
        'type' => 'node',
        'bundle' => 'foaf',
        'mapping' => array(
          'rdftype' => array('foaf:Person'),
          'title' => array(
            'predicates' => array('foaf:name'),
        ),
      ),
    ),
  );
}

Instances of a field that occur in different bundles can be mapped to different RDF properties. For example, when the title field appears in an article node, it is mapped to dc:title. When it appears in a foaf node, it is mapped to foaf:name.

Multiple types can be assigned to a bundle. For example, an article is both a sioc:Item and a foaf:Document.

Properties

These are all the properties that can be set in an RDF mapping. All properties except for rdftype are associated with a field instance (i.e. title, field_image).

  • rdftype
    The RDF type describes what kind of thing the resource is. RDF type terms always start with an uppercase letter (i.e. foaf:Person, sioc:User).
  • predicates
    The predicate describes the relationship between the entity and the field value. Multiple predicates can be used for a single field.
  • type
    There can be three types of predicate relationships: property, rel, and rev. The type defaults to property. A property is any literal value (i.e. string, integer).

    Rel should be used when the value of a field is a link or a node reference.

    Rev can be used to reverse the predicate relationship, so instead of being in the order of subject predicate object, the relationship is in the order of object predicate subject.

  • datatype
    In the case of properties, where the field value is a literal, you can assert the datatype. XML Schema datatypes are commonly used.
  • callback
    A callback can be used to convert a literal from one format to another. For example, the 'date_iso8601' callback is used to convert the creation date from the Unix timestamp that is stored in the Drupal database to the ISO 8601 format used in RDF.

Best Practices

Include hook_uninstall(). The uninstall hook can simply return, rdf_modules_uninstalled runs and deletes the mapping entry in {rdf_mapping}.

function foaf_uninstall() {
  return;
}

Help improve this page

Page status: No known problems

You can: