View source
<?php
namespace Drupal\user_email_verification\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Utility\Token;
use Drupal\user_email_verification\UserEmailVerificationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserEmailVerificationNotificationBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $userEmailVerification;
protected $currentUser;
protected $token;
public function __construct(array $configuration, $plugin_id, $plugin_definition, UserEmailVerificationInterface $user_email_verification_service, AccountProxyInterface $current_user, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->userEmailVerification = $user_email_verification_service;
$this->currentUser = $current_user;
$this->token = $token;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('user_email_verification.service'), $container
->get('current_user'), $container
->get('token'));
}
public function defaultConfiguration() {
return [
'notification' => [
'value' => $this
->t('Verify your Email by 3 simple steps:<br>1. Open your mail inbox,<br>2. Find Email verification message from [site:name],<br>3. Click by verification link.'),
'format' => NULL,
],
];
}
public function blockForm($form, FormStateInterface $form_state) {
$form['notification'] = [
'#title' => $this
->t('Notification message'),
'#description' => $this
->t('Paste here a message with instructions which will notify a user to verify Email.'),
'#type' => 'text_format',
'#required' => TRUE,
'#default_value' => $this->configuration['notification']['value'],
'#format' => $this->configuration['notification']['format'],
];
$form['token_tree'] = [
'#theme' => 'token_tree_link',
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this
->setConfigurationValue('notification', $form_state
->getValue('notification'));
}
public function build() {
$build = [];
if ($this->currentUser
->isAuthenticated() && $this->userEmailVerification
->isVerificationNeeded()) {
$build = [
'#attributes' => [
'class' => [
'email-verify-notification',
],
],
'notification' => [
'#type' => 'processed_text',
'#text' => $this->token
->replace($this->configuration['notification']['value']),
'#format' => $this->configuration['notification']['format'],
],
];
}
return $build;
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), [
'user:' . $this->currentUser
->id(),
]);
}
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), [
'user_email_verification_needed',
]);
}
}