By editor, 2 December, 2015 Question How to get the current user, user ID, user data on drupal 8 ? How To check if the current page is the homepage of Drupal 8 ? Example: //use Drupal\user\Entity\User; $userCurrent = \Drupal::currentUser(); $user = \Drupal\user\Entity\User::load($userCurrent->id()); $name = $user->getUsername(); How To check if the current page is the homepage in Drupal 8 As drupal_is_front_page() on drupal 7, you can use path.matcher service of drupal 8. Example: $is_front_page = \Drupal::service('path.matcher')->isFrontPage(); Tags Drupal 8 User Code Add new comment Comments3 You must have JavaScript enabled to use this form. Your name Subject Comment About text formats Plain text No HTML tags allowed. Lines and paragraphs break automatically. Web page addresses and email addresses turn into links automatically. Display this in a twig template How can i able to add this in my twig template file.?? Reply Use custom module to show variable or alter your template For that, use a custom module like here Example : (Custom page) return array( '#theme' => 'tour_custon_theme', '#user' => $user, ); //OR with inline template return array( '#type' => 'inline_template', '#template' => '{{ user }}', '#context' => array( 'user' => $user, ), ); If you can't do this, try to use the hook template_preprocess like: function template_preprocess_YOURTEMPLATE(&$variables) { } Reply $userCurrent = \Drupal:… $userCurrent = \Drupal::currentUser(); $user = \Drupal\user\Entity\User::load($userCurrent->id()); $name = $user->getUsername(); Reply
Use custom module to show variable or alter your template For that, use a custom module like here Example : (Custom page) return array( '#theme' => 'tour_custon_theme', '#user' => $user, ); //OR with inline template return array( '#type' => 'inline_template', '#template' => '{{ user }}', '#context' => array( 'user' => $user, ), ); If you can't do this, try to use the hook template_preprocess like: function template_preprocess_YOURTEMPLATE(&$variables) { } Reply
$userCurrent = \Drupal:… $userCurrent = \Drupal::currentUser(); $user = \Drupal\user\Entity\User::load($userCurrent->id()); $name = $user->getUsername(); Reply
Comments3
Display this in a twig template
How can i able to add this in my twig template file.??
Use custom module to show variable or alter your template
For that, use a custom module like here
Example : (Custom page)
return array(
'#theme' => 'tour_custon_theme',
'#user' => $user,
);
//OR with inline template
return array(
'#type' => 'inline_template',
'#template' => '{{ user }}',
'#context' => array(
'user' => $user,
),
);
If you can't do this, try to use the hook template_preprocess like:
function template_preprocess_YOURTEMPLATE(&$variables) {
}
$userCurrent = \Drupal:…
$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$name = $user->getUsername();