View source
<?php
namespace Drupal\welcome_username\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WelcomeUserNameBlock extends BlockBase implements ContainerFactoryPluginInterface {
public function defaultConfiguration() {
return [
'welcome_username_welcome_string' => $this
->t('Welcome'),
'welcome_username_logout_string' => $this
->t('Logout'),
];
}
protected $formBuilder;
protected $currentUser;
protected $renderer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder, AccountProxy $currentUser, Renderer $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->formBuilder = $form_builder;
$this->currentUser = $currentUser;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('form_builder'), $container
->get('current_user'), $container
->get('renderer'));
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$form['welcome_username_welcome_string'] = [
'#type' => 'textfield',
'#title' => $this
->t('Welcome String'),
'#default_value' => $this->configuration['welcome_username_welcome_string'],
];
$form['welcome_username_logout_string'] = [
'#type' => 'textfield',
'#title' => $this
->t('Logout String'),
'#default_value' => $this->configuration['welcome_username_logout_string'],
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
parent::blockSubmit($form, $form_state);
$values = $form_state
->getValues();
$this->configuration['welcome_username_welcome_string'] = $values['welcome_username_welcome_string'];
$this->configuration['welcome_username_logout_string'] = $values['welcome_username_logout_string'];
}
public function build() {
if ($this->currentUser
->isAnonymous()) {
$form = $this->formBuilder
->getForm("Drupal\\user\\Form\\UserLoginForm");
$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();
$user_name = $this->currentUser
->getDisplayName();
$welcome_string = $config['welcome_username_welcome_string'];
$logout_string = $config['welcome_username_logout_string'];
$content['profile_link'] = Link::fromTextAndUrl(t($welcome_string) . " " . $user_name, Url::fromRoute('user.page'));
$content['logout_link'] = Link::fromTextAndUrl(t($logout_string), Url::fromRoute('user.logout'));
}
return [
'#theme' => 'welcome_username_login',
'#content' => $content,
];
}
}