Skip to content

Instantly share code, notes, and snippets.

@alcroito
Created February 9, 2015 08:58
Show Gist options
  • Save alcroito/235b239008bebc37e267 to your computer and use it in GitHub Desktop.
Save alcroito/235b239008bebc37e267 to your computer and use it in GitHub Desktop.
Drupal theme_field overrides
function custom_preprocess_field(&$variables, $hook) {
$element = $variables['element'];
// Add suggestions for entity view mode as well.
if (isset($element['#view_mode'])) {
$view_mode = $element['#view_mode'];
$variables['theme_hook_suggestions'][] = 'field__' . $element['#field_name'] . '__' . $element['#bundle'] . '__' . $view_mode;
}
}
/**
* Displays a field with the field label set to h5, instead of a div.
*/
function icon($variables) {
$output = '';
// Render the label, if it's not hidden.
if (!$variables['label_hidden']) {
$prefix = '<h5>';
$suffix = '</h5>';
$label = $variables['label'];
if (isset($variables['#icon'])) {
$label = $variables['#icon'] . ' ' . $label;
}
$output .= $prefix . $label . $suffix;
}
// Render the items.
$output .= '<div class="field-items"' . $variables['content_attributes'] . '>';
foreach ($variables['items'] as $delta => $item) {
$classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');
$output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</div>';
}
$output .= '</div>';
// Render the top-level DIV.
$output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';
return $output;
}
function custom_field__field_info__bundle___custom_display($variables) {
$variables['#icon'] = '<i class="icon-info"></i>';
return custom_field_with_h5_label($variables);
}
function custom_field__field_description__bundle___custom_display($variables) {
$variables['#icon'] = '<i class="icon-music"></i>';
return custom_field_with_h5_label($variables);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment