View source
<?php
namespace Drupal\user\Plugin\Block;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserLoginBlock extends BlockBase implements ContainerFactoryPluginInterface, TrustedCallbackInterface {
use RedirectDestinationTrait;
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_route_match'));
}
protected function blockAccess(AccountInterface $account) {
$route_name = $this->routeMatch
->getRouteName();
if ($account
->isAnonymous() && !in_array($route_name, [
'user.login',
'user.logout',
])) {
return AccessResult::allowed()
->addCacheContexts([
'route.name',
'user.roles:anonymous',
]);
}
return AccessResult::forbidden();
}
public function build() {
$form = \Drupal::formBuilder()
->getForm('Drupal\\user\\Form\\UserLoginForm');
unset($form['name']['#attributes']['autofocus']);
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;
$placeholder = 'form_action_p_4r8ITd22yaUvXM6SzwrSe9rnQWe48hz9k1Sxto3pBvE';
$form['#attached']['placeholders'][$placeholder] = [
'#lazy_builder' => [
'\\Drupal\\user\\Plugin\\Block\\UserLoginBlock::renderPlaceholderFormAction',
[],
],
];
$form['#action'] = $placeholder;
$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,
],
];
}
public static function renderPlaceholderFormAction() {
return [
'#type' => 'markup',
'#markup' => UrlHelper::filterBadProtocol(Url::fromRoute('<current>', [], [
'query' => \Drupal::destination()
->getAsArray(),
'external' => FALSE,
])
->toString()),
'#cache' => [
'contexts' => [
'url.path',
'url.query_args',
],
],
];
}
public static function trustedCallbacks() {
return [
'renderPlaceholderFormAction',
];
}
}