Created
May 5, 2017 10:11
-
-
Save NikLP/47f0c584f7a9d58fed77690fe9bbe95d to your computer and use it in GitHub Desktop.
Sacrilegious's way to get forms in templates for D8
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
/** | |
* 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