node.html.twigで、現在アクセスしているユーザのid取得方法について

Events happening in the community are now at Drupal community events on www.drupal.org.
gg45's picture

いつもお世話になります。

Drupal 8です。
node.html.twigで、現在アクセスしているユーザの(ログインしているユーザ)idを取得したいのですが
方法わかる方いますでしょうか?
{{ logged_in }} だとログインしているか否かはわかるのですが、idまでは撮れませんでした。

ご存知の方教えていただけると嬉しいです。

Comments

Add below code into

macshutd0wn's picture

Add below code into YOUR_THEME.theme file:

/**
* Implements hook_preprocess_HOOK() for node.html.twig.
*/
function YOUR_THEME_NAME_preprocess_node(&$variables) {
$account = \Drupal::currentUser();
$variables['uid'] = $account->id();
}

Then print uid value in node.html.twig by {{ uid }}.

gg45's picture

ありがとうございます!対応できました。
Thank you MacShutD0wn

日本 コミュニティ: Drupal Japan User Group

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: