Drupal: I need to display the username in the block

Is there a Drupal module for displaying a username in a block when it is logged in?

thank

+3
source share
2 answers
  • Create a new block.
  • Format: PHP code

Block body:

<? 
global $user;
print $user->name;
?>
+4
source

In Drupal 7, using a custom module called YOURMODULE:

/**
 * Implements hook_block_info().
 */
function YOURMODULE_block_info() {
  return array(
    'YOURMODULE_logged_in_as' => array(
       'info' => t('Login information ("Logged in as...").'),
       'cache' => DRUPAL_CACHE_PER_USER,
    ),
  );
}

/**
 * Implements hook_block_view().
 */
function YOURMODULE_block_view($delta = '') {
  if ($delta == 'YOURMODULE_logged_in_as') {
    global $user;
    return array(
      'subject' => NULL,
      'content' => t('Logged in as !name', array('!name' => theme('username', array('account' => $user)))),
    );
  }
}
+3
source

Source: https://habr.com/ru/post/1762595/


All Articles