You are here

function microdata_mapping_to_attributes in Microdata 7

Converts a mapping array to an attibute array.

Parameters

array $mapping: The $mapping for the field or property.

string $entity_type: (optional) The entity type of the entity, used to generate tokens.

object $entity: (optional) The entity, used to generate tokens.

Return value

array A nested array of field and property attributes.

1 call to microdata_mapping_to_attributes()
microdata_entity_load in ./microdata.module
Implements hook_entity_load().

File

./microdata.module, line 901

Code

function microdata_mapping_to_attributes($mapping, $entity_type = NULL, $entity = NULL) {
  if (is_string($mapping)) {
    return $mapping;
  }

  // If the user has overriden the item handling, change the type to struct.
  if (isset($mapping['#is_item'])) {
    if ($mapping['#is_item'] === TRUE) {
      $mapping['#value_type'] = 'item';
    }
    else {
      $mapping['#value_type'] = 'struct';
    }
  }
  $return['#attributes'] = array();

  // If there is an itemprop mapping, add the attribute.
  if (!empty($mapping['#itemprop'])) {
    $return['#attributes']['itemprop'] = $mapping['#itemprop'];
  }

  // If the value for this property is an item, add itemscope.
  if (isset($mapping['#value_type']) && $mapping['#value_type'] === 'item') {
    $return['#attributes']['itemscope'] = '';

    // If an itemtype is defined, add that as well.
    if (!empty($mapping['#itemtype'])) {
      $return['#attributes']['itemtype'] = $mapping['#itemtype'];
    }

    // Get the appropriate token for the itemid.
    if (isset($mapping['#itemid_token'])) {

      // Because some token groups aren't named with the entity type, we have to
      // change the group.
      switch ($entity_type) {
        case 'taxonomy_term':
          $group = 'term';
          break;
        case 'taxonomy_vocabulary':
          $group = 'vocabulary';
          break;
        default:
          $group = $entity_type;
      }
      $return['#attributes']['itemid'] = token_replace($mapping['#itemid_token'], array(
        $group => $entity,
      ));
    }
  }
  foreach (array_keys($mapping) as $property) {
    if ($property[0] != '#') {
      $return[$property] = microdata_mapping_to_attributes($mapping[$property]);
    }
  }

  // Google's Rich Snippets requires (for some types) a 'url' itemprop with the
  // page's URI as the value.
  // @todo Remove this once Google recognizes itemid #1784580.
  if (isset($mapping['#use_schema_url'])) {
    $return['microdata_schema_url'] = _microdata_get_schema_url($entity_type, $entity);
  }

  // @todo Add a hook_microdata_attributes_attach to allow modules to insert
  // or change attributes. For example, field collection might want to allow
  // a field within the collection to be used as itemid.
  return $return;
}