Last active
April 27, 2016 20:21
-
-
Save reubenmoes/8c1c7da1286d143a13470ea8d59aa05b to your computer and use it in GitHub Desktop.
Drupal hook to get the Display Suite field group attributes input option to use tokens. NOTE: only works in template.php i believe, and not modules.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//@file template.php | |
/** | |
* Implements hook_field_group_pre_render_alter() | |
* Add token support for field group attributes | |
* | |
* NOTE: this is basically a copy/paste of field_group_pre_render_html_element() | |
* from the field_group module with some changes | |
* | |
* @param mixed $element | |
* @param object $group | |
* @param array $form | |
*/ | |
function template_field_group_pre_render_alter(&$element, $group, & $form) { | |
$entity_type = $form['#entity_type']; | |
//Fallback for field collections | |
$entity = isset($form['#'.$entity_type]) ? $form['#'.$entity_type] :$form['#entity']; | |
$html_element = isset($group->format_settings['instance_settings']['element']) ? $group->format_settings['instance_settings']['element'] : 'div'; | |
$show_label = isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : 0; | |
$label_element = isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : 'div'; | |
//NOTE: This is the line of code that is altered from original function | |
$attributes = isset($group->format_settings['instance_settings']['attributes']) ? ' ' . token_replace($group->format_settings['instance_settings']['attributes'], array($entity_type => $entity)) : ''; | |
$group->classes = trim($group->classes); | |
// This regex split the attributes string so that we can pass that | |
// later to drupal_attributes(). | |
preg_match_all('/([^\s=]+)="([^"]+)"/', $attributes, $matches); | |
$element_attributes = array(); | |
// Put the attribute and the value together. | |
foreach ($matches[1] as $key => $attribute) { | |
$element_attributes[$attribute] = $matches[2][$key]; | |
} | |
// Add the classes to the attributes array. | |
if (!isset($element_attributes['class']) && $group->classes) { | |
$element_attributes['class'] = $group->classes; | |
} | |
elseif (isset($element_attributes['class']) && $group->classes) { | |
$element_attributes['class'] .= ' ' . $group->classes; | |
} | |
$attributes = drupal_attributes($element_attributes); | |
$element['#prefix'] = '<' . $html_element . $attributes . '>'; | |
if ($show_label) { | |
$element['#prefix'] .= '<' . $label_element . '><span>' . check_plain(t($group->label)) . '</span></' . $label_element . '>'; | |
} | |
$element['#suffix'] = '</' . $html_element . '>'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment