function UserLoginBlock::build

Same name and namespace in other branches
  1. 9 core/modules/user/src/Plugin/Block/UserLoginBlock.php \Drupal\user\Plugin\Block\UserLoginBlock::build()
  2. 8.9.x core/modules/user/src/Plugin/Block/UserLoginBlock.php \Drupal\user\Plugin\Block\UserLoginBlock::build()
  3. 10 core/modules/user/src/Plugin/Block/UserLoginBlock.php \Drupal\user\Plugin\Block\UserLoginBlock::build()

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

File

core/modules/user/src/Plugin/Block/UserLoginBlock.php, line 97

Class

UserLoginBlock
Provides a 'User login' block.

Namespace

Drupal\user\Plugin\Block

Code

public function build() {
  $form = $this->formBuilder
    ->getForm(UserLoginForm::class);
  unset($form['name']['#attributes']['autofocus']);
  $form['name']['#size'] = 15;
  $form['pass']['#size'] = 15;
  // Instead of setting an actual action URL, we set the placeholder, which
  // will be replaced at the very last moment. This ensures forms with
  // dynamically generated action URLs don't have poor cacheability.
  // Use the proper API to generate the placeholder, when we have one. See
  // https://www.drupal.org/node/2562341. The placeholder uses a fixed string
  // that is
  // Crypt::hashBase64('\Drupal\user\Plugin\Block\UserLoginBlock::build');
  // This is based on the implementation in
  // \Drupal\Core\Form\FormBuilder::prepareForm(), but the user login block
  // requires different behavior for the destination query argument.
  // cspell:disable-next-line
  $placeholder = 'form_action_p_4r8ITd22yaUvXM6SzwrSe9rnQWe48hz9k1Sxto3pBvE';
  $form['#attached']['placeholders'][$placeholder] = [
    '#lazy_builder' => [
      '\\Drupal\\user\\Plugin\\Block\\UserLoginBlock::renderPlaceholderFormAction',
      [],
    ],
  ];
  $form['#action'] = $placeholder;
  // Build action links.
  $items = [];
  if (\Drupal::config('user.settings')->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
    $items['create_account'] = [
      '#type' => 'link',
      '#title' => $this->t('Create new account'),
      '#url' => Url::fromRoute('user.register', [], [
        'attributes' => [
          'title' => $this->t('Create a new user account.'),
          'class' => [
            'create-account-link',
          ],
        ],
      ]),
    ];
  }
  $items['request_password'] = [
    '#type' => 'link',
    '#title' => $this->t('Reset your password'),
    '#url' => Url::fromRoute('user.pass', [], [
      'attributes' => [
        'title' => $this->t('Send password reset instructions via email.'),
        'class' => [
          'request-password-link',
        ],
      ],
    ]),
  ];
  return [
    'user_login_form' => $form,
    'user_links' => [
      '#theme' => 'item_list',
      '#items' => $items,
    ],
  ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.