Skip to content

Instantly share code, notes, and snippets.

@opi
Created June 5, 2013 08:03
Show Gist options
  • Save opi/5712322 to your computer and use it in GitHub Desktop.
Save opi/5712322 to your computer and use it in GitHub Desktop.
Reduce div soup in drupal theme. (like fences or field_wrappers modules)
<?php
/**
* @file field.tpl.php
* See http://api.drupal.org/api/drupal/modules!field!theme!field.tpl.php/7
*
* Theme suggestions:
* - field.tpl.php
* - field--TYPE.tpl.php
* - field--NAME.tpl.php
* - field--CONTENT_TYPE.tpl.php
* - field--NAME--CONTENT_TYPE.tpl.php
*
* See http://drupal.org/node/1089656#field-suggestion
*/
?>
<?php if (!$label_hidden): ?>
<div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
<?php endif; ?>
<?php foreach ($items as $delta => $item): ?>
<?php print render($item); ?>
<?php endforeach; ?>
<?php
/**
* @file field.tpl.php
* See http://api.drupal.org/api/drupal/modules!field!theme!field.tpl.php/7
*
* Theme suggestions:
* - field.tpl.php
* - field--TYPE.tpl.php
* - field--NAME.tpl.php
* - field--CONTENT_TYPE.tpl.php
* - field--NAME--CONTENT_TYPE.tpl.php
*
* See http://drupal.org/node/1089656#field-suggestion
*/
?>
<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
<?php if (!$label_hidden): ?>
<div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
<?php endif; ?>
<?php foreach ($items as $delta => $item): ?>
<?php print render($item); ?>
<?php endforeach; ?>
</div>
<?php
/**
* Implements template_preprocess_field().
*/
function mytheme_preprocess_field(&$vars) {
$element = $vars['element'];
// Custom theme_hook_suggestions (no_markup & wrapper_only)
$wrapper_only_fields = array(
'field_block',
'body',
'field_description',
);
if (in_array($element['#field_name'], $wrapper_only_fields)) {
$vars['theme_hook_suggestions'][] = 'field__wrapper_only';
}
$no_markup_fields = array(
'field_subtitle',
'field_link',
);
if (in_array($element['#field_name'], $no_markup_fields)) {
$vars['theme_hook_suggestions'][] = 'no_markup';
}
} // mytheme_preprocess_field
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment