I've been able to successfully add the parent form ID to all form elements, based on these two questions: How can we find the id of the parent form in preprocess_HOOK() or HOOK_alter() theme functions? and Add the Form name to the theme suggestions input field.
However, the two elements I really need template suggestions for are inputs that have containers... therefore the parent form ID is added to the containers, not the inputs, and when I add the template suggestions the variable is blank (obviously).
I see there are modules like Formdazzle! which makes this easy, but I feel like I'm close - however I'm struggling to 'reach' the children (the inputs) of the form elements (the containers).
This is my current code:
* Implements hook_form_alter().
*/
function HOOK_form_alter(&$form, FormStateInterface $form_state, $form_id) {
foreach (\Drupal\Core\Render\Element::children($form) as $key) {
if ($form[$key]['#parents']) {
$form[$key]['#attributes']['data-parent-form'] = $form['#id'];
} else {
// help!
}
}
}
/**
* Implements hook_theme_suggestions_input_alter().
*/
function HOOK_theme_suggestions_input_alter(&$suggestions, array $variables) {
$element = $variables['element'];
$suggestions[] = 'input__' . $element['#type'] . '__' . $element['#attributes']['data-parent-form'];
}