Skip to content

Instantly share code, notes, and snippets.

@NikLP
Created May 5, 2017 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikLP/47f0c584f7a9d58fed77690fe9bbe95d to your computer and use it in GitHub Desktop.
Save NikLP/47f0c584f7a9d58fed77690fe9bbe95d to your computer and use it in GitHub Desktop.
Sacrilegious's way to get forms in templates for D8
/**
* Provides suggestions for different parts, so that blocks are properly loaded
*
* @see hook_theme_suggestions_alter()
*
* @param array $suggestions
* @param array $variables
* @param string $hook
*/
function hook_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
switch ($hook) {
case 'form':
if (isset($variables['element']['#form_id'])) {
$suggestions[] = $hook . '__' . $variables['element']['#form_id'];
}
break;
}
}
/**
* @implements template_preprocess_HOOK()
*
* @param array $variables
*/
function hook_preprocess_form(&$variables) {
if (isset($variables['element'])) {
foreach (\Drupal\Core\Render\Element::children($variables['element']) as $key) {
$variables['form'][$key] = $variables['element'][$key];
unset($variables['form'][$key]['#markup']);
$variables['form'][$key]['#printed'] = FALSE;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment