3 namespace Drupal\user\Plugin\Block;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\Core\Routing\RedirectDestinationTrait;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\Core\Routing\UrlGeneratorTrait;
11 use Drupal\Core\Session\AccountInterface;
12 use Drupal\Core\Block\BlockBase;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Provides a 'User login' block.
19 * id = "user_login_block",
20 * admin_label = @Translation("User login"),
21 * category = @Translation("Forms")
24 class UserLoginBlock extends BlockBase implements ContainerFactoryPluginInterface {
26 use UrlGeneratorTrait;
27 use RedirectDestinationTrait;
32 * @var \Drupal\Core\Routing\RouteMatchInterface
34 protected $routeMatch;
37 * Constructs a new UserLoginBlock instance.
39 * @param array $configuration
40 * The plugin configuration, i.e. an array with configuration values keyed
41 * by configuration option name. The special key 'context' may be used to
42 * initialize the defined contexts by setting it to an array of context
43 * values keyed by context names.
44 * @param string $plugin_id
45 * The plugin_id for the plugin instance.
46 * @param mixed $plugin_definition
47 * The plugin implementation definition.
48 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
51 public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
52 parent::__construct($configuration, $plugin_id, $plugin_definition);
54 $this->routeMatch = $route_match;
60 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
65 $container->get('current_route_match')
73 protected function blockAccess(AccountInterface $account) {
74 $route_name = $this->routeMatch->getRouteName();
75 if ($account->isAnonymous() && !in_array($route_name, ['user.login', 'user.logout'])) {
76 return AccessResult::allowed()
77 ->addCacheContexts(['route.name', 'user.roles:anonymous']);
79 return AccessResult::forbidden();
85 public function build() {
86 $form = \Drupal::formBuilder()->getForm('Drupal\user\Form\UserLoginForm');
87 unset($form['name']['#attributes']['autofocus']);
88 // When unsetting field descriptions, also unset aria-describedby attributes
89 // to avoid introducing an accessibility bug.
90 // @todo Do this automatically in https://www.drupal.org/node/2547063.
91 unset($form['name']['#description']);
92 unset($form['name']['#attributes']['aria-describedby']);
93 unset($form['pass']['#description']);
94 unset($form['pass']['#attributes']['aria-describedby']);
95 $form['name']['#size'] = 15;
96 $form['pass']['#size'] = 15;
97 $form['#action'] = $this->url('<current>', [], ['query' => $this->getDestinationArray(), 'external' => FALSE]);
98 // Build action links.
100 if (\Drupal::config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
101 $items['create_account'] = [
103 '#title' => $this->t('Create new account'),
104 '#url' => Url::fromRoute('user.register', [], [
106 'title' => $this->t('Create a new user account.'),
107 'class' => ['create-account-link'],
112 $items['request_password'] = [
114 '#title' => $this->t('Reset your password'),
115 '#url' => Url::fromRoute('user.pass', [], [
117 'title' => $this->t('Send password reset instructions via email.'),
118 'class' => ['request-password-link'],
123 'user_login_form' => $form,
125 '#theme' => 'item_list',