CommentNotifyUnsubscribe.php in Comment Notify 8
File
src/Form/CommentNotifyUnsubscribe.php
View source
<?php
namespace Drupal\comment_notify\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CommentNotifyUnsubscribe extends FormBase {
protected $messenger;
public static function create(ContainerInterface $container) {
return new static($container
->get('messenger'));
}
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
public function getFormId() {
return 'comment_notify_unsubscribe';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['comment_notify_unsubscribe'] = [];
$form['comment_notify_unsubscribe']['email'] = [
'#type' => 'textfield',
'#title' => t('Email to unsubscribe'),
'#description' => $this
->t('All comment notification requests associated with this email will be revoked.'),
'#required' => TRUE,
];
$form['comment_notify_unsubscribe']['submit'] = [
'#type' => 'submit',
'#value' => t('Unsubscribe this e-mail'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
module_load_include('inc', 'comment_notify', 'comment_notify');
$email = trim($form_state
->getValue([
'email',
]));
$comments = comment_notify_unsubscribe_by_email($email);
if ($comments == 0) {
$this->messenger
->addWarning($this
->t("There were no active comment notifications for that email."));
}
else {
$this->messenger
->addStatus($this
->t("Email unsubscribed from all the comment notifications."));
}
}
}