UserLoginBlock.php 3.45 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\user\Plugin\Block\UserLoginBlock.
6
7
 */

8
namespace Drupal\user\Plugin\Block;
9
10

use Drupal\block\BlockBase;
11
use Drupal\Component\Annotation\Plugin;
12
use Drupal\Core\Annotation\Translation;
13
14
15
16
17
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\user\Form\UserLoginForm;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
18
19
20
21
22
23

/**
 * Provides a 'User login' block.
 *
 * @Plugin(
 *   id = "user_login_block",
24
 *   admin_label = @Translation("User login"),
25
26
27
 *   module = "user"
 * )
 */
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class UserLoginBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The DI Container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * The request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * Constructs a new UserLoginBlock.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param array $plugin_definition
   *   The plugin implementation definition.
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The DI Container.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, ContainerInterface $container, Request $request) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->container = $container;
    $this->request = $request;
  }

  /**
66
   * {@inheritdoc}
67
68
69
70
71
72
73
74
75
76
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container,
      $container->get('request')
    );
  }
77
78

  /**
79
   * Overrides \Drupal\block\BlockBase::access().
80
   */
81
  public function access() {
82
83
84
85
    return (!$GLOBALS['user']->uid && !(arg(0) == 'user' && !is_numeric(arg(1))));
  }

  /**
86
   * {@inheritdoc}
87
   */
88
  public function build() {
89
    $form = drupal_get_form(UserLoginForm::create($this->container), $this->request);
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    unset($form['name']['#attributes']['autofocus']);
    unset($form['name']['#description']);
    unset($form['pass']['#description']);
    $form['name']['#size'] = 15;
    $form['pass']['#size'] = 15;
    $form['#action'] = url(current_path(), array('query' => drupal_get_destination(), 'external' => FALSE));
    // Build action links.
    $items = array();
    if (config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
      $items['create_account'] = l(t('Create new account'), 'user/register', array(
        'attributes' => array(
          'title' => t('Create a new user account.'),
          'class' => array('create-account-link'),
        ),
      ));
    }
    $items['request_password'] = l(t('Request new password'), 'user/password', array(
      'attributes' => array(
        'title' => t('Request new password via e-mail.'),
        'class' => array('request-password-link'),
      ),
    ));
    return array(
      'user_login_form' => $form,
      'user_links' => array(
        '#theme' => 'item_list',
        '#items' => $items,
      ),
    );
  }

}