- 146 views
Place this HOOK in your .theme file
There are many hooks you can use, but mainly HOOK_preprocess() and HOOK_preprocess_html() works nicely with this solution.
function HOOK_preprocess(&$vars, $hook)
{
// get current user ID
$current_user_id = \Drupal::currentUser()->id();
$vars['current_user_id'] = $current_user_id;
}
Use your newly created variable anywhere in TWIG file
Now it is easy, as you can see in the the example above, you can not use {{ current_user_id
}} which will print current user ID anywhere you want. You can pull any data from anywhere and store it in any variable this way and print out it in any TWIG file anywhere in Drupal.