function microdata_preprocess_field in Microdata 7
Implements MODULE_preprocess_HOOK().
Adds Microdata markup to the field wrapper.
File
- ./
microdata.module, line 412
Code
function microdata_preprocess_field(&$variables) {
// Skip preprocess for node Preview.
if (isset($variables['element']['#object']->in_preview)) {
return;
}
$element = $variables['element'];
$field_name = $element['#field_name'];
$mapping = microdata_get_mapping($element['#entity_type'], $element['#bundle']);
// Make sure there is a mapping with a proper value type and formatter for
// this field, otherwise skip this whole function and return to avoid errors.
if (!isset($mapping[$field_name]) || empty($mapping[$field_name]['#value_type']) || !isset($element['#formatter']) || empty($element['#object']->microdata)) {
return;
}
$field_mapping = $mapping[$field_name];
$microdata = $element['#object']->microdata;
// If this is a field with string values, then the $element will have a
// single HTML id to wrap around the whole field.
if (isset($element['#microdata_field_id'])) {
$variables['content_attributes_array']['id'] = $element['#microdata_field_id'];
}
foreach ($variables['items'] as $delta => &$item) {
// If this is a datetime or URL element, the field formatter must place the
// item's property attributes. If this is an item, the itemprop is placed
// on the item's meta tag. Otherwise, the itemprop can be placed on the
// wrapping div using the item_attributes_array.
if (isset($field_mapping['#value_type'])) {
$is_datetime = $field_mapping['#value_type'] === 'datetime' ? TRUE : FALSE;
$is_url = $field_mapping['#value_type'] === 'url' ? TRUE : FALSE;
$is_item_reference = $field_mapping['#value_type'] === 'item_reference' ? microdata_is_item($element['#entity_type'], $element['#object'], $field_name, $delta) : FALSE;
$place_microdata_wrapper = !($is_datetime || $is_url || $is_item_reference);
}
else {
$place_microdata_wrapper = TRUE;
}
if ($place_microdata_wrapper) {
$attributes = isset($variables['item_attributes_array'][$delta]) ? $variables['item_attributes_array'][$delta] : array();
if (isset($microdata[$field_name]['#attributes'])) {
$variables['item_attributes_array'][$delta] = array_merge($attributes, $microdata[$field_name]['#attributes']);
}
else {
$variables['item_attributes_array'][$delta] = $attributes;
}
}
// If this is a reference field, it will have a field ID for each field
// item.
if (isset($item['#microdata_field_id'])) {
$variables['item_attributes_array'][$delta]['id'] = $item['#microdata_field_id'];
}
// Because core field formatters cannot be changed until D8, we use a
// theme hack to place the itemprop (and itemtype for taxonomy).
switch ($element['#formatter']) {
case 'image':
$item['#item']['attributes']['itemprop'] = $field_mapping['#itemprop'];
break;
case 'text_default':
if ($field_mapping['#value_type'] != 'text') {
$attributes = isset($variables['item_attributes_array'][$delta]) ? $variables['item_attributes_array'][$delta] : array();
$variables['item_attributes_array'][$delta] = array_merge($attributes, $microdata[$field_name]['value']['#attributes']);
}
break;
case 'text_trimmed':
case 'text_summary_or_trimmed':
$attributes = isset($variables['item_attributes_array'][$delta]) ? $variables['item_attributes_array'][$delta] : array();
if (is_array($microdata[$field_name]['#attributes']) && isset($microdata[$field_name]['summary']['#attributes'])) {
$variables['item_attributes_array'][$delta] = array_merge($attributes, $microdata[$field_name]['summary']['#attributes']);
}
break;
}
}
}