Skip to content

Instantly share code, notes, and snippets.

@balsama
Last active August 29, 2015 14:04
Show Gist options
  • Save balsama/a4d0deb13786f1e7b05f to your computer and use it in GitHub Desktop.
Save balsama/a4d0deb13786f1e7b05f to your computer and use it in GitHub Desktop.
Drupal custom formatter for fields. Easily wrap a field in a div or heading tag with customizable classes. (use this as a ctools import)
$formatter = new stdClass();
$formatter->disabled = FALSE; /* Edit this to true to make a default formatter disabled initially */
$formatter->api_version = 2;
$formatter->name = 'html_element';
$formatter->label = 'HTML Element';
$formatter->description = 'Allows you to wrap fields in HTML element with custom classes.';
$formatter->mode = 'php';
$formatter->field_types = 'text_long, text';
$formatter->code = '$html_element = $variables[\'#display\'][\'settings\'][\'html_element\'];
$classes = $variables[\'#display\'][\'settings\'][\'additional_css_classes\'];
if ($classes != \'\') {
$prefix = \'<\' . $html_element . \' class="\' . $classes . \'">\';
}
else {
$prefix = \'<\' . $html_element . \'>\';
}
foreach ($variables[\'#items\'] as $delta => $item) {
$element[$delta] = array(
\'#type\' => \'markup\',
\'#prefix\' => $prefix,
\'#markup\' => $item[\'value\'],
\'#suffix\' => \'</\' . $html_element . \'>\',
);
}
return $element;';
$formatter->fapi = '$form = array();
$form[\'html_element\'] = array(
\'#multiple_toggle\' => \'1\',
\'#title\' => t(\'HTML Element\'),
\'#default_value\' => \'div\',
\'#key_type_toggled\' => \'1\',
\'#multiple\' => \'0\',
\'#options\' => array(
\'div\' => t(\'div\'),
\'h2\' => t(\'h2\'),
\'h3\' => t(\'h3\'),
\'h4\' => t(\'h4\'),
\'h5\' => t(\'h5\'),
\'h6\' => t(\'h6\'),
),
\'#type\' => \'select\',
\'#weight\' => \'0\',
\'#required\' => \'0\',
);
$form[\'additional_css_classes\'] = array(
\'#title\' => t(\'Additional CSS Classes\'),
\'#type\' => \'textfield\',
\'#required\' => \'0\',
\'#weight\' => \'1\',
);
';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment