|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_user\Plugin\Block; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Block\BlockBase; |
|
6
|
|
|
use Drupal\Core\Cache\Cache; |
|
7
|
|
|
use Drupal\Core\Form\FormBuilder; |
|
8
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
9
|
|
|
use Drupal\Core\Session\AccountProxyInterface; |
|
10
|
|
|
use Drupal\Core\Url; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Provides the "User Dropdown" block. |
|
15
|
|
|
* |
|
16
|
|
|
* @Block( |
|
17
|
|
|
* id = "user_dropdown", |
|
18
|
|
|
* admin_label = @Translation("User Dropdown"), |
|
19
|
|
|
* category = @Translation("Menus") |
|
20
|
|
|
* ) |
|
21
|
|
|
*/ |
|
22
|
|
|
class UserDropdownBlock extends BlockBase implements ContainerFactoryPluginInterface { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The form builder. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Drupal\Core\Form\FormBuilder |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $formBuilder; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The current user. |
|
33
|
|
|
* |
|
34
|
|
|
* @var \Drupal\Core\Session\AccountProxyInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $user; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilder $form_builder, AccountProxyInterface $user) { |
|
39
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
|
40
|
|
|
$this->formBuilder = $form_builder; |
|
41
|
|
|
$this->user = $user; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
48
|
|
|
return new static( |
|
49
|
|
|
$configuration, |
|
50
|
|
|
$plugin_id, |
|
51
|
|
|
$plugin_definition, |
|
52
|
|
|
$container->get('form_builder'), |
|
|
|
|
|
|
53
|
|
|
$container->get('current_user') |
|
|
|
|
|
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function build() { |
|
61
|
|
|
$block = []; |
|
62
|
|
|
$links_cache_contexts = []; |
|
63
|
|
|
|
|
64
|
|
|
// Display different links to depending on whether the user is logged in. |
|
65
|
|
|
if ($this->user->isAnonymous()) { |
|
66
|
|
|
// Create a 'person' icon that toggles a login form. |
|
67
|
|
|
$markup = ''; |
|
68
|
|
|
$markup .= '<a class="login-dropdown-button standard-icon meta-icon-size left" data-toggle="user-login-wrapper" aria-controls="user-login-wrapper" aria-expanded="false">'; |
|
69
|
|
|
$markup .= '<i class="icon ion-ios-person"><span>Login</span></i>'; |
|
70
|
|
|
$markup .= '</a>'; |
|
71
|
|
|
|
|
72
|
|
|
$block['login'] = [ |
|
73
|
|
|
'#markup'=> $markup, |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
// Wrap the login form with some required foundation classes/attributes |
|
77
|
|
|
$login_wrapper = [ |
|
78
|
|
|
'#type' => 'container', |
|
79
|
|
|
'#attributes' =>[ |
|
80
|
|
|
'class' => ['dropdown-pane', 'display-none'], |
|
81
|
|
|
'id' => 'user-login-wrapper', |
|
82
|
|
|
'data-dropdown' => '', |
|
83
|
|
|
'aria-hidden' => 'true', |
|
84
|
|
|
'aria-autoclose' => 'true', |
|
85
|
|
|
'data-auto-focus' => 'true', |
|
86
|
|
|
'tabindex' => '-1' |
|
87
|
|
|
], |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
// Get the user login form. |
|
91
|
|
|
$form = $this->formBuilder->getForm('\Drupal\user\Form\UserLoginForm'); |
|
92
|
|
|
|
|
93
|
|
|
// Remove default form descriptions. |
|
94
|
|
|
unset($form['name']['#description']); |
|
95
|
|
|
unset($form['name']['#attributes']['aria-describedby']); |
|
96
|
|
|
unset($form['pass']['#description']); |
|
97
|
|
|
unset($form['pass']['#attributes']['aria-describedby']); |
|
98
|
|
|
|
|
99
|
|
|
$login_wrapper['form'] = $form; |
|
100
|
|
|
|
|
101
|
|
|
$block['wrapper'] = $login_wrapper; |
|
102
|
|
|
} |
|
103
|
|
|
else { |
|
104
|
|
|
$username = $this->user->getDisplayName(); |
|
105
|
|
|
$user_page_url = Url::fromRoute('user.page')->toString(); |
|
106
|
|
|
$user_logout_url = Url::fromRoute('user.logout')->toString(); |
|
107
|
|
|
|
|
108
|
|
|
// Create a 'person' icon that toggles user profile and log out links. |
|
109
|
|
|
$markup = ''; |
|
110
|
|
|
$markup .= '<a class="login-dropdown-button standard-icon meta-icon-size left" data-toggle="user-logout-wrapper" aria-controls="user-logout-wrapper" aria-expanded="false">'; |
|
111
|
|
|
$markup .= '<i class="icon ion-ios-person"></i>'; |
|
112
|
|
|
$markup .= '<span> '. $username . '</span>'; |
|
113
|
|
|
$markup .= '</a>'; |
|
114
|
|
|
|
|
115
|
|
|
$markup .= '<div id="user-logout-wrapper" class="dropdown-pane f-dropdown" data-dropdown aria-hidden="true" aria-autoclose="false" data-auto-focus="false">'; |
|
116
|
|
|
$markup .= '<div class="user-links">'; |
|
117
|
|
|
$markup .= '<a class="" href="' . $user_page_url . '"><i class="icon ion-ios-person"></i> ' . $username . '</a>'; |
|
|
|
|
|
|
118
|
|
|
$markup .= '<a class="logout-button" href="' . $user_logout_url . '"><i class="icon ion-log-out"></i> ' . t('Log Out') . '</a>'; |
|
|
|
|
|
|
119
|
|
|
$markup .= '</div>'; |
|
120
|
|
|
$markup .= '</div>'; |
|
121
|
|
|
|
|
122
|
|
|
$block['login'] = [ |
|
123
|
|
|
'#markup'=> $markup, |
|
124
|
|
|
]; |
|
125
|
|
|
|
|
126
|
|
|
// The "Edit user account" link is per-user. |
|
127
|
|
|
$links_cache_contexts[] = 'user'; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Cacheable per "authenticated or not", because the links to |
|
131
|
|
|
// display depend on that. |
|
132
|
|
|
$block['#cache']['contexts'] = Cache::mergeContexts(['user.roles:authenticated'], $links_cache_contexts); |
|
133
|
|
|
|
|
134
|
|
|
return $block; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|