ed0e2eaf1a8a7e12dac4ab85fe28ff52f50d6609
[yaffs-website] / web / core / modules / user / src / Plugin / Block / UserLoginBlock.php
1 <?php
2
3 namespace Drupal\user\Plugin\Block;
4
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;
10 use Drupal\Core\Url;
11 use Drupal\Core\Session\AccountInterface;
12 use Drupal\Core\Block\BlockBase;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Provides a 'User login' block.
17  *
18  * @Block(
19  *   id = "user_login_block",
20  *   admin_label = @Translation("User login"),
21  *   category = @Translation("Forms")
22  * )
23  */
24 class UserLoginBlock extends BlockBase implements ContainerFactoryPluginInterface {
25
26   use UrlGeneratorTrait;
27   use RedirectDestinationTrait;
28
29   /**
30    * The route match.
31    *
32    * @var \Drupal\Core\Routing\RouteMatchInterface
33    */
34   protected $routeMatch;
35
36   /**
37    * Constructs a new UserLoginBlock instance.
38    *
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
49    *   The route match.
50    */
51   public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
52     parent::__construct($configuration, $plugin_id, $plugin_definition);
53
54     $this->routeMatch = $route_match;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
61     return new static(
62       $configuration,
63       $plugin_id,
64       $plugin_definition,
65       $container->get('current_route_match')
66     );
67   }
68
69
70   /**
71    * {@inheritdoc}
72    */
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']);
78     }
79     return AccessResult::forbidden();
80   }
81
82   /**
83    * {@inheritdoc}
84    */
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.
99     $items = [];
100     if (\Drupal::config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
101       $items['create_account'] = [
102         '#type' => 'link',
103         '#title' => $this->t('Create new account'),
104         '#url' => Url::fromRoute('user.register', [], [
105           'attributes' => [
106             'title' => $this->t('Create a new user account.'),
107             'class' => ['create-account-link'],
108           ],
109         ]),
110       ];
111     }
112     $items['request_password'] = [
113       '#type' => 'link',
114       '#title' => $this->t('Reset your password'),
115       '#url' => Url::fromRoute('user.pass', [], [
116         'attributes' => [
117           'title' => $this->t('Send password reset instructions via email.'),
118           'class' => ['request-password-link'],
119         ],
120       ]),
121     ];
122     return [
123       'user_login_form' => $form,
124       'user_links' => [
125         '#theme' => 'item_list',
126         '#items' => $items,
127       ],
128     ];
129   }
130
131 }