|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_nemid\Plugin\Block; |
| 4 | + |
| 5 | +use Drupal\Core\Block\BlockBase; |
| 6 | +use Drupal\Core\Form\FormStateInterface; |
| 7 | + |
| 8 | +/** |
| 9 | + * Provides a 'OS2Forms Nemid Login/out link' block. |
| 10 | + * |
| 11 | + * @Block( |
| 12 | + * id = "os2forms_nemid_login_logout_link", |
| 13 | + * admin_label = @Translation("OS2Forms Nemid Login/out link") |
| 14 | + * ) |
| 15 | + */ |
| 16 | +class NemidLoginLogoutLink extends BlockBase { |
| 17 | + |
| 18 | + /** |
| 19 | + * {@inheritdoc} |
| 20 | + */ |
| 21 | + public function defaultConfiguration() { |
| 22 | + return [ |
| 23 | + 'link_login_text' => $this->t('Login'), |
| 24 | + 'link_logout_text' => $this->t('Logout'), |
| 25 | + 'hide_login_button' => FALSE, |
| 26 | + ]; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * {@inheritdoc} |
| 31 | + */ |
| 32 | + public function blockForm($form, FormStateInterface $form_state) { |
| 33 | + $form['hide_login_button'] = [ |
| 34 | + '#type' => 'checkbox', |
| 35 | + '#title' => $this->t('Hide login button'), |
| 36 | + '#default_value' => $this->configuration['hide_login_button'], |
| 37 | + ]; |
| 38 | + $form['link_login_text'] = [ |
| 39 | + '#type' => 'textfield', |
| 40 | + '#title' => $this->t('Login text'), |
| 41 | + '#description' => $this->t('A text on login link.'), |
| 42 | + '#default_value' => $this->configuration['link_login_text'], |
| 43 | + '#states' => [ |
| 44 | + 'visible' => [ |
| 45 | + 'input[name="settings[hide_login_button]"]' => ['checked' => FALSE], |
| 46 | + ], |
| 47 | + ], |
| 48 | + ]; |
| 49 | + $form['link_logout_text'] = [ |
| 50 | + '#type' => 'textfield', |
| 51 | + '#title' => $this->t('Logout text'), |
| 52 | + '#description' => $this->t('A text on logout link.'), |
| 53 | + '#default_value' => $this->configuration['link_logout_text'], |
| 54 | + ]; |
| 55 | + return $form; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + public function blockSubmit($form, FormStateInterface $form_state) { |
| 62 | + $this->configuration['hide_login_button'] = $form_state->getValue('hide_login_button'); |
| 63 | + $this->configuration['link_login_text'] = $form_state->getValue('link_login_text'); |
| 64 | + $this->configuration['link_logout_text'] = $form_state->getValue('link_logout_text'); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function build() { |
| 71 | + /** @var \Drupal\os2web_nemlogin\Service\AuthProviderService $authProviderService */ |
| 72 | + $authProviderService = \Drupal::service('os2web_nemlogin.auth_provider'); |
| 73 | + $plugin = $authProviderService->getActivePlugin(); |
| 74 | + |
| 75 | + // Do nothing if there is no auth plugin. |
| 76 | + if (empty($plugin)) { |
| 77 | + return []; |
| 78 | + } |
| 79 | + |
| 80 | + if (!$plugin->isAuthenticated() && $this->configuration['hide_login_button']) { |
| 81 | + return []; |
| 82 | + } |
| 83 | + |
| 84 | + $link = $authProviderService->generateLink($this->configuration['link_login_text'], $this->configuration['link_logout_text']); |
| 85 | + $element['#title'] = $link->getText(); |
| 86 | + $element['#url'] = $link->getUrl(); |
| 87 | + $build['login_logout_link'] = [ |
| 88 | + '#title' => $link->getText(), |
| 89 | + '#type' => 'link', |
| 90 | + '#url' => $link->getUrl(), |
| 91 | + ]; |
| 92 | + |
| 93 | + return $build; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * {@inheritdoc} |
| 98 | + */ |
| 99 | + public function getCacheMaxAge() { |
| 100 | + return 0; |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments