Skip to content

Instantly share code, notes, and snippets.

@keeganstreet
Created November 11, 2017 02:07
Show Gist options
  • Save keeganstreet/f554c40de0043939b6208c3417cc8a5e to your computer and use it in GitHub Desktop.
Save keeganstreet/f554c40de0043939b6208c3417cc8a5e to your computer and use it in GitHub Desktop.
Drupal theme hooks to add a template suggestion for labels based on the associated input type
<?php
/**
* Implements hook_preprocess_HOOK().
*/
function drupalsouth_preprocess_form_element(array &$variables) {
$variables['label']['#attributes']['data-element-type'] = $variables['element']['#type'];
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function drupalsouth_theme_suggestions_form_element_label_alter(array &$suggestions, array $variables) {
if (isset($variables['element']['#attributes']['data-element-type'])) {
$hook = str_replace('-', '_', $variables['element']['#attributes']['data-element-type']);
$suggestions[] = "form_element_label__{$hook}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment