function semantic_fields_preprocess_field in Semantic Fields 7
Implements hook_preprocess_HOOK().
File
- ./
semantic_fields.module, line 149 - The functionality of the module
Code
function semantic_fields_preprocess_field(&$variables) {
$element = $variables['element'];
$field = field_info_field($element['#field_name']);
$instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
$display = isset($instance['display'][$element['#view_mode']]) ? $instance['display'][$element['#view_mode']] : $instance['display']['default'];
$use_defaults = FALSE;
if (!empty($display['settings']['semantic_field_format'])) {
$format = $display['settings']['semantic_field_format'];
}
elseif (!empty($instance['settings']['semantic_field_format'])) {
$format = $instance['settings']['semantic_field_format'];
}
else {
$use_defaults = TRUE;
}
if (isset($format) && $format === 'default') {
$use_defaults = TRUE;
}
// Load semantic field display format settings.
if (!$use_defaults) {
$preset = semantic_fields_preset_load($format);
}
if (empty($preset)) {
// Use defaults if settings could not be loaded.
$use_defaults = TRUE;
}
// Use default settings if the selected field format cannot be loaded, or if the Default field format is selected.
if ($use_defaults) {
$settings = array(
'field_element' => 'div',
'field_class' => '',
'field_prefix' => '',
'field_suffix' => '',
'label_element_above' => 'div',
'label_class_above' => '',
'label_suffix_above' => ':',
'label_element_inline' => 'div',
'label_class_inline' => '',
'label_suffix_inline' => ':',
'multiple_items_element' => 'div',
'multiple_items_class' => '',
'multiple_item_element' => 'div',
'multiple_item_class' => '',
'multiple_item_separator' => '',
'use_singlevalue_settings' => 0,
'single_item_element' => 'div',
'single_item_class' => '',
'last_every_nth' => 0,
'first_class' => '',
'last_class' => '',
'striping_classes' => 'odd even',
);
}
else {
$settings = $preset->data;
}
// Provide Semantic fields field template suggestions.
$variables['theme_hook_suggestions'][] = 'semantic_field';
$variables['theme_hook_suggestions'][] = 'semantic_field__' . $element['#field_type'];
$variables['theme_hook_suggestions'][] = 'semantic_field__' . $element['#field_name'];
$variables['theme_hook_suggestions'][] = 'semantic_field__' . $element['#bundle'];
$variables['theme_hook_suggestions'][] = 'semantic_field__' . $element['#field_name'] . '__' . $element['#bundle'];
// Field
$variables['field_element'] = check_plain($settings['field_element']);
$variables['field_prefix'] = !empty($settings['field_prefix']) ? check_plain($settings['field_prefix']) : '';
$variables['field_suffix'] = !empty($settings['field_suffix']) ? check_plain($settings['field_suffix']) : '';
if (!empty($settings['field_class'])) {
$variables['classes_array'][] = check_plain($settings['field_class']);
}
// Label
$variables['label_element'] = $element['#label_display'] == "above" ? check_plain($settings['label_element_above']) : check_plain($settings['label_element_inline']);
$variables['label_suffix'] = $element['#label_display'] == "above" ? check_plain($settings['label_suffix_above']) : check_plain($settings['label_suffix_inline']);
if (!isset($variables['label_classes_array'])) {
$variables['label_classes_array'] = array(
'field-label',
);
}
if ($element['#label_display'] == "above") {
if (!empty($settings['label_class_above'])) {
$variables['label_classes_array'][] = check_plain($settings['label_class_above']);
}
}
else {
if ($element['#label_display'] == "inline") {
if (!empty($settings['label_class_inline'])) {
$variables['label_classes_array'][] = check_plain($settings['label_class_inline']);
}
}
}
// Items
if (!isset($variables['content_classes_array'])) {
$variables['content_classes_array'] = array(
'field-items',
);
}
$num_items = count($variables['items']);
if ($num_items == 1) {
if (!$settings['use_singlevalue_settings']) {
$variables['content_element'] = check_plain($settings['multiple_items_element']);
if (!empty($settings['multiple_items_class'])) {
$variables['content_classes_array'][] = check_plain($settings['multiple_items_class']);
}
}
else {
$variables['content_element'] = '';
}
}
else {
$variables['content_element'] = check_plain($settings['multiple_items_element']);
if (!empty($settings['multiple_items_class'])) {
$variables['content_classes_array'][] = check_plain($settings['multiple_items_class']);
}
}
// Item
if ($num_items == 1 && $settings['use_singlevalue_settings']) {
$variables['item_element'] = check_plain($settings['single_item_element']);
$variables['item_separator'] = '';
}
else {
$variables['item_element'] = check_plain($settings['multiple_item_element']);
$variables['item_separator'] = $num_items > 1 && !empty($settings['multiple_item_separator']) ? check_plain($settings['multiple_item_separator']) : '';
}
$variables['item_classes'] = array();
$last_every_nth = !empty($settings['last_every_nth']) ? check_plain($settings['last_every_nth']) : 0;
foreach ($variables['items'] as $delta => $item) {
$item_classes = array(
'field-item',
);
if ($num_items == 1) {
if ($settings['use_singlevalue_settings'] && $settings['single_item_class']) {
$item_classes[] = check_plain($settings['single_item_class']);
}
elseif (!$settings['use_singlevalue_settings'] && $settings['multiple_item_class']) {
$item_classes[] = check_plain($settings['multiple_item_class']);
}
}
elseif ($num_items > 1) {
if ($settings['multiple_item_class']) {
$item_classes[] = check_plain($settings['multiple_item_class']);
}
}
// Striping
$striping_classes = array();
if (trim($settings['striping_classes'])) {
$striping_classes = explode(' ', trim($settings['striping_classes']));
$item_classes[] = $striping_classes[$delta % count($striping_classes)];
}
if ($settings['first_class']) {
if ($last_every_nth && $delta % $last_every_nth == 0 || !$last_every_nth && $delta == 0) {
$item_classes[] = check_plain($settings['first_class']);
}
}
if ($settings['last_class']) {
if ($last_every_nth && ($delta + 1) % $last_every_nth == 0 || !$last_every_nth && $delta + 1 == $num_items) {
$item_classes[] = check_plain($settings['last_class']);
}
}
$variables['item_classes'][$delta] = $item_classes;
}
}