1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_user\Plugin\Block; |
4
|
|
|
|
5
|
|
|
use Drupal\user\Entity\User; |
6
|
|
|
use Drupal\Core\Block\BlockBase; |
7
|
|
|
use Drupal\Core\Cache\Cache; |
8
|
|
|
use Drupal\Core\Form\FormBuilder; |
9
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
10
|
|
|
use Drupal\Core\Session\AccountProxyInterface; |
11
|
|
|
use Drupal\Core\Url; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Provides the "User Dropdown" block. |
16
|
|
|
* |
17
|
|
|
* @Block( |
18
|
|
|
* id = "user_dropdown", |
19
|
|
|
* admin_label = @Translation("User Dropdown"), |
20
|
|
|
* category = @Translation("Menus") |
21
|
|
|
* ) |
22
|
|
|
*/ |
23
|
|
|
class UserDropdownBlock extends BlockBase implements ContainerFactoryPluginInterface { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The form builder. |
27
|
|
|
* |
28
|
|
|
* @var \Drupal\Core\Form\FormBuilder |
29
|
|
|
*/ |
30
|
|
|
protected $formBuilder; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The current user. |
34
|
|
|
* |
35
|
|
|
* @var \Drupal\Core\Session\AccountProxyInterface |
36
|
|
|
*/ |
37
|
|
|
protected $user; |
38
|
|
|
|
39
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilder $form_builder, AccountProxyInterface $user) { |
40
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
41
|
|
|
$this->formBuilder = $form_builder; |
42
|
|
|
$this->user = $user; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
49
|
|
|
return new static( |
50
|
|
|
$configuration, |
51
|
|
|
$plugin_id, |
52
|
|
|
$plugin_definition, |
53
|
|
|
$container->get('form_builder'), |
|
|
|
|
54
|
|
|
$container->get('current_user') |
|
|
|
|
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function build() { |
62
|
|
|
// Get the user login form. |
63
|
|
|
$form = $this->formBuilder->getForm('\Drupal\user\Form\UserLoginForm'); |
64
|
|
|
|
65
|
|
|
// Remove default form descriptions. |
66
|
|
|
unset($form['name']['#description']); |
67
|
|
|
unset($form['name']['#attributes']['aria-describedby']); |
68
|
|
|
unset($form['pass']['#description']); |
69
|
|
|
unset($form['pass']['#attributes']['aria-describedby']); |
70
|
|
|
|
71
|
|
|
$user_page_url = Url::fromRoute('user.page')->toString(); |
72
|
|
|
$user_logout_url = Url::fromRoute('user.logout')->toString(); |
73
|
|
|
|
74
|
|
|
$current_user = \Drupal::currentUser(); |
75
|
|
|
|
76
|
|
|
return [ |
77
|
|
|
'#theme' => 'user_dropdown', |
78
|
|
|
'#form' => $form, |
79
|
|
|
'#user_page_url' => $user_page_url, |
80
|
|
|
'#user_logout_url' => $user_logout_url, |
81
|
|
|
'#attached' => [ |
82
|
|
|
'library' => [ |
83
|
|
|
'df_tools_user/user-dropdown', |
84
|
|
|
], |
85
|
|
|
], |
86
|
|
|
'#cache' => [ |
87
|
|
|
'context' => ['user'], |
88
|
|
|
'tags' => User::load($current_user->id())->getCacheTags(), |
89
|
|
|
], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|