public function WelcomeUserNameBlock::build in Welcome Username 8
Builds and returns the renderable array for this block plugin.
If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).
Return value
array A renderable array representing the content of the block.
Overrides BlockPluginInterface::build
See also
\Drupal\block\BlockViewBuilder
File
- src/
Plugin/ Block/ WelcomeUserNameBlock.php, line 131
Class
- WelcomeUserNameBlock
- Provides a 'Welcome Username Login/Logout' Block.
Namespace
Drupal\welcome_username\Plugin\BlockCode
public function build() {
// For anonymous users load a simple login form.
if ($this->currentUser
->isAnonymous()) {
$form = $this->formBuilder
->getForm("Drupal\\user\\Form\\UserLoginForm");
// Placeholders.
$form['name']['#attributes']['placeholder'] = $form['name']['#description'];
unset($form['name']['#description']);
$form['pass']['#attributes']['placeholder'] = $form['pass']['#description'];
unset($form['pass']['#description']);
$content['login_form'] = $this->renderer
->render($form);
}
else {
$config = $this
->getConfiguration();
// Load user object.
$user_name = $this->currentUser
->getDisplayName();
// Load string from variable table if set.
$welcome_string = $config['welcome_username_welcome_string'];
$logout_string = $config['welcome_username_logout_string'];
// Create a link to the user profile page.
$content['profile_link'] = Link::fromTextAndUrl(t($welcome_string) . " " . $user_name, Url::fromRoute('user.page'));
// Create a logout link.
$content['logout_link'] = Link::fromTextAndUrl(t($logout_string), Url::fromRoute('user.logout'));
}
return [
'#theme' => 'welcome_username_login',
'#content' => $content,
];
}