You are here

public function LogintobogganLoginBlock::build in LoginToboggan 8

Implements \Drupal\block\BlockBase::blockBuild().

Overrides BlockPluginInterface::build

File

src/Plugin/Block/LogintobogganLoginBlock.php, line 107

Class

LogintobogganLoginBlock
Provides a 'LoginToboggan login' block.

Namespace

Drupal\logintoboggan\Plugin\Block

Code

public function build() {
  $config = $this
    ->getConfiguration();

  // Default type to zero.
  $block_type = $config['login_display_type'] ?? '0';

  // Build a login form. This copies mostly from core UserLoginBlock.
  if ($block_type == '0' || $block_type == '2') {
    $login_form = $this->formBuilder
      ->getForm('Drupal\\user\\Form\\UserLoginForm');
    unset($login_form['name']['#attributes']['autofocus']);

    // When unsetting field descriptions, unset aria-describedby attributes
    // to avoid introducing an accessibility bug.
    unset($login_form['name']['#description']);
    unset($login_form['name']['#attributes']['aria-describedby']);
    unset($login_form['pass']['#description']);
    unset($login_form['pass']['#attributes']['aria-describedby']);
    $login_form['name']['#size'] = 15;
    $login_form['pass']['#size'] = 15;
    $placeholder = 'form_action_p_4r8ITd22yaUvXM6SzwrF6huIvE248hz9k1Sxto3pBvE';
    $login_form['#attached']['placeholders'][$placeholder] = [
      '#lazy_builder' => [
        '\\Drupal\\logintoboggan\\Plugin\\Block\\LogintobogganLoginBlock::renderPlaceholderFormAction',
        [],
      ],
    ];
    $login_form['#action'] = $placeholder;
  }
  $link = [
    '#title' => $this
      ->t('Login in / register'),
    '#type' => 'link',
    '#url' => Url::fromRoute('user.login', [
      'query' => [
        'destination' => '/node/100',
      ],
    ]),
    '#attributes' => [
      'id' => 'toboggan-login-link',
    ],
  ];
  if ($block_type == '1') {
    $login_form = $link;
  }
  $message = !empty($config['login_block_message']) ? $config['login_block_message'] : '';
  $build = [];
  $build['#cache']['max-age'] = 0;
  $build['#theme'] = 'lt_login_block';
  $build['content'] = [
    'user_login_form' => $login_form,
    'message' => $message,
    'login_link' => $link,
    'block_type' => $block_type,
  ];
  return $build;
}