You are here

function schemaorg_entity_view in Schema.org 7

Implements hook_entity_view().

File

./schemaorg.module, line 26
Enables administrators to annotate their structured content with vocabularies from schema.org.

Code

function schemaorg_entity_view($entity, $type, $view_mode, $langcode) {

  // Adds the schema.org url to the entity content as RDF metadata.
  if (!empty($entity->rdf_mapping['url']['predicates'])) {
    $attributes = rdf_rdfa_attributes($entity->rdf_mapping['url']);
    $uri = entity_uri($type, $entity);
    $attributes['resource'] = url($uri['path'], $uri['options']);
    $entity->content['schemaorg_url'] = array(
      '#markup' => theme('rdf_metadata', array(
        'metadata' => array(
          'span' => $attributes,
        ),
      )),
      '#weight' => 100,
    );
  }

  // It seems parsers are expecting to find the title/name of the entity within
  // the main content of the entity. Drupal displays the title outside the main
  // content on full entity display, so we assert it as hidden RDF metadata.
  foreach (array(
    'title',
    'subject',
    'name',
  ) as $field_name) {
    if (!empty($entity->{$field_name})) {
      $label_field = $field_name;
      break;
    }
  }
  if (isset($label_field) && !empty($entity->rdf_mapping[$label_field]['predicates'])) {
    $attributes = rdf_rdfa_attributes($entity->rdf_mapping[$label_field]);
    $attributes['content'] = $entity->{$label_field};
    $entity->content['schemaorg_name'] = array(
      '#markup' => theme('rdf_metadata', array(
        'metadata' => array(
          'span' => $attributes,
        ),
      )),
      '#weight' => 100,
    );
  }
}