You are here

public function TfaUserLoginBlock::build in Two-factor Authentication (TFA) 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 UserLoginBlock::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/TfaUserLoginBlock.php, line 86

Class

TfaUserLoginBlock
Provides a 'Tfa User login' block.

Namespace

Drupal\tfa\Plugin\Block

Code

public function build() {
  $form = \Drupal::formBuilder()
    ->getForm('Drupal\\tfa\\Form\\TfaLoginForm');
  unset($form['name']['#attributes']['autofocus']);

  // When unsetting field descriptions, also unset aria-describedby attributes
  // to avoid introducing an accessibility bug.
  // @todo Do this automatically in https://www.drupal.org/node/2547063.
  unset($form['name']['#description']);
  unset($form['name']['#attributes']['aria-describedby']);
  unset($form['pass']['#description']);
  unset($form['pass']['#attributes']['aria-describedby']);
  $form['name']['#size'] = 15;
  $form['pass']['#size'] = 15;
  $form['#action'] = Url::fromRoute('<current>', [], [
    'query' => $this
      ->getDestinationArray(),
    'external' => FALSE,
  ])
    ->toString();

  // Build action links.
  $items = [];
  if (\Drupal::config('user.settings')
    ->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
    $items['create_account'] = Link::fromTextAndUrl($this
      ->t('Create new account'), new Url('user.register', [], [
      'attributes' => [
        'title' => $this
          ->t('Create a new user account.'),
        'class' => [
          'create-account-link',
        ],
      ],
    ]));
  }
  $items['request_password'] = Link::fromTextAndUrl($this
    ->t('Reset your password'), new Url('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,
    ],
  ];
}