You are here

function rdfx_get_rdf_model in RDF Extensions 7.2

Parameters

String $type: The entity type.

String $id: The entity instance ID.

Return value

ARC2 Object

5 calls to rdfx_get_rdf_model()
RdfxNodeSerializationTestCase::testRdfxNodeCreation in ./rdfx.test
Create a "Article" node and ensure it serialized properly.
RDFxRestWSFormatNTriples::viewResource in ./rdfx.restws.formats.inc
RDFxRestWSFormatRDFJSON::viewResource in ./rdfx.restws.formats.inc
RDFxRestWSFormatRDFXML::viewResource in ./rdfx.restws.formats.inc
RDFxRestWSFormatTurtle::viewResource in ./rdfx.restws.formats.inc

File

./rdfx.module, line 122
Extends the RDF API of Drupal core to support more RDF seralizations formats other RDF capabilities.

Code

function rdfx_get_rdf_model($type, $id) {

  // Loads entity and its metadata.
  $wrapper = entity_metadata_wrapper($type, $id);
  $entities = entity_load($type, array(
    $id,
  ));
  $entity = $entities[$id];
  $data = $wrapper
    ->value();
  $property_info = entity_get_property_info($type);
  $entity_uri = rdfx_resource_uri($type, $entity);

  // Instantiates node resource as ARC2 class and set base and namespaces.
  $res = ARC2::getResource();
  $res
    ->setUri($entity_uri);
  $res->base = url('', array(
    'absolute' => TRUE,
  ));
  $res->ns = rdf_get_namespaces();

  // Initializes ARC2 index.
  $index = array();

  // Adds the RDF types of the resource if a mapping exists.
  if (!empty($data->rdf_mapping['rdftype'])) {
    $index[$entity_uri]['rdf:type'] = $data->rdf_mapping['rdftype'];
  }
  foreach ($wrapper as $name => $property) {
    if ($property
      ->access('view')) {

      // Substitute the schema field if the key exists.
      if (!empty($property_info['properties'][$name]['schema field'])) {
        $name = $property_info['properties'][$name]['schema field'];
      }
      try {
        if ($property instanceof EntityDrupalWrapper || $property instanceof EntityValueWrapper) {
          rdfx_add_statement($index, $entity_uri, $property, $wrapper, $name);
        }
        elseif ($property instanceof EntityListWrapper) {

          // Iterates through the list and add each of them as statement.
          $li_filtered = rdfx_property_access_filter($property);
          foreach ($li_filtered as $li_name => $li_property) {
            if ($li_property instanceof EntityDrupalWrapper || $li_property instanceof EntityValueWrapper) {
              rdfx_add_statement($index, $entity_uri, $li_property, $wrapper, $name);
            }
          }
        }
        elseif ($property instanceof EntityStructureWrapper) {

          // Entity Structure Wrapper exposes structural elements, such as
          // text format for fields. We are not interested in this, and adding
          // the additional information about values means you have to handle
          // values as blank nodes.
          $li_filtered = rdfx_property_access_filter($property);
          foreach ($li_filtered as $li_name => $li_property) {
            if ($li_property instanceof EntityDrupalWrapper || $li_property instanceof EntityValueWrapper) {

              // This assumes that all literals have a 'value' array element
              // and that all resources have a 'uri' array element.
              if ($li_name == 'value' || $li_name == 'uri') {
                rdfx_add_statement($index, $entity_uri, $li_property, $wrapper, $name);
              }
            }
          }
        }
      } catch (EntityMetadataWrapperException $e) {

        // Property not supported. Fail silently.
      }
    }
  }

  // Attach the index to the ARC2 resource.
  $res->index = $index;

  // Allow other modules to alter the RDF data model before serialization.
  // For example, a module could add elements which are not supported by the
  // Entity API.
  // This should only be used in special cases. The data is not added to
  // Drupal's internal model, and is thus not accessible to other modules or
  // during Drupal's normal rendering stream. It will only show up in non-RDFa
  // serializations of the entity.
  drupal_alter('rdf_model', $res, $type, $id);

  // Expand all CURIEs.
  $res->index = $res
    ->expandPNames($res->index);
  return $res;
}