Am new to Drupal so this is a very fundemental question.

How do I display the current username ? In a custom Block? I beleive is the $user->name session variable.

Do I create a Custom Block Type? If so how do I reference the username?

Do I edit files on the file system? If so which files?

Do I create new files? If so where?

In other web frameworks the templating engine allows server script tags... ex.. like

<%= username %>

Ive tried putting

$user->name and {{app.user}}

into the custom block editor but nope, I've learned it's a WYSISYG with no way to render

All I want to do is display a user name in a block I can add a label too.

Any help appriciated.

  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Clive
    Commented Feb 22, 2019 at 17:13

3 Answers 3


You can do this in sevaral ways, one of this could be use a preprocess function. In your custom module in the .module file or your custom theme in the .theme file (recommended) write this:

 * Implements hook_preprocess_HOOK().
function [my_module_or_theme]_preprocess_block(&$variables) {
  $variables['username'] = \Drupal::currentUser()->getUsername();

and then in your template suggestions of block.twig.html print the variable {{ username }}

  • This pointer looks very helpful, thankyou. It points out how to do the username lookup programmatically which as a beginner is an important step to really understanding Drupal. I'll come back to accept once I'm up successfully and running with a custom module / theme.
    – Kickaha
    Commented Feb 25, 2019 at 15:12
  • Not exactly the answer I needed, but close enough to have helped. Thanks.
    – Kickaha
    Commented Mar 7, 2019 at 18:07

To answer my question I created a custom module with a block.

That needed a folder for the module, a .info.yml file and a file for the block.

1) A folder for the module modules/custom/my_show_user

2) The file modules/custom/my_show_user/my_show_user.info.yml

name: Show User Module
description: Displays the logged in user name in a block.
package: Custom

type: module
core: 8.x

  - drupal:block

php: 5.6

3) The file modules/custom/my_show_user/src/Plugin/Block/UserNameBlock.php


namespace Drupal\my_show_user\Plugin\Block;

use Drupal\Core\Block\BlockBase;

 * Provides a 'User Name' Block.
 * @Block(
 *   id = "my_show_user",
 *   admin_label = @Translation("Show User"),
 *   category = @Translation("Show User"),
 * )

class UserNameBlock extends BlockBase {

   * {@inheritdoc}
  public function build() {
    return array(
      '#markup' => $this->t(\Drupal::currentUser()->getUsername()),


That's it done. .

fyi, any time a change is made clear the cache to see it
Manage>Configuration>Performance>Clear All Caches

In Structure > Place Block a block called UserNameBlock can now be selected and it displays the current logged in user.

Adding the dependency - drupal:block and keeping the commented annotations @Block and @inheritdoc was key.

the line

'#markup' => $this->t('any html string can go here ' . \Drupal::currentUser()->getUsername() . ' concatenated with data in the template, in this case username' ),

is the entry point for your 'custom template'.


public function build() { return [ '#markup' => $this->t(\Drupal::currentUser()->getUsername()), ]; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.