You are here

function microdata_item in Microdata 7

Maintains a list of microdata item meta tags.

HTML data formats usually rely on the fact that properties are nested within the HTML tags of items in order to express the relationship between an item and its properties. However, if a site uses tools like Views or Panels to move fields around independently, microdata can't depend on that nesting.

In order to support Views and Panels, we place items as meta tags at the bottom of the page and use the itemref feature of microdata to set the relationships.

Parameters

$item_type: Usually the entity type when working with an entity or a reference field. However, this could be a custom property type, such as addressfield, in the case of compound fields.

$item_id: Usually the entity id, this id will be used to join attributes from multiple points in the system and should be an ID that can be used in conjunction with the item_type to identify the property.

$attributes: The HTML attributes that should be added to this meta tag. These should usually be taken directly from the variable provided in $entity->microdata[$field_name]['#attributes']. Can include:

  • itemscope: Required to declare this as an item.
  • itemtype: The type of item this is.
  • itemprop: If this is a property of another item, an array of properties that relates the two.
  • itemid: The globally unique ID for the item.
  • itemref: The HTML ids of any fields related to this item. This is usually set within the microdata_preprocess_field implementation.

Return value

Array or NULL If no parameters were provided, return the array of all items. If parameters were provided, the function is simply adding a new item, so return NULL.

Related topics

2 calls to microdata_item()
microdata_field_attach_view_alter in ./microdata.module
Implements hook_field_attach_view_alter().
_microdata_process_page in ./microdata.module
Inserts the meta elements for items which have been added.

File

./microdata.module, line 747

Code

function microdata_item($item_type = NULL, $item_id = NULL, $input_attributes = array()) {
  static $microdata_item;

  // If this is the first item to be added, initalize the array.
  if (empty($microdata_item)) {
    $microdata_item = array();
  }

  // If no arguments have been passed in, return the complete array. Rather
  // than performing an array_unique check whenever an item is added, remove
  // duplicates before returning.
  if (empty($item_type)) {
    foreach ($microdata_item as $entity_type => &$items) {
      foreach ($items as &$item) {
        foreach ($item as &$attributes) {
          foreach ($attributes as &$attribute) {
            if (is_array($attribute)) {
              $attribute = array_unique($attribute);
            }
          }
        }
      }
    }
    return $microdata_item;
  }

  // If the attributes array doesn't exist yet, initalize it.
  if (!isset($microdata_item[$item_type][$item_id]['#attributes'])) {
    $microdata_item[$item_type][$item_id]['#attributes'] = array();
  }
  $attributes =& $microdata_item[$item_type][$item_id]['#attributes'];
  $attributes = array_merge_recursive($attributes, $input_attributes);
}