View source
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\mailing_list\SubscriptionInterface;
use Drupal\email_confirmer\EmailConfirmationInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Url;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
const MAILING_LIST_CONFIRM_NOT_CONFIRMED = 0;
const MAILING_LIST_CONFIRM_TO_BE_CONFIRMED = 1;
const MAILING_LIST_CONFIRM_CONFIRMED = 2;
const MAILING_LIST_CONFIRM_CANCELLED = 3;
const MAILING_LIST_CONFIRM_SKIPPED = -1;
function mailing_list_confirm_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type
->id() == 'mailing_list_subscription') {
$fields = [];
$values = [
MAILING_LIST_CONFIRM_NOT_CONFIRMED => t('Not confirmed'),
MAILING_LIST_CONFIRM_TO_BE_CONFIRMED => t('To be confirmed'),
MAILING_LIST_CONFIRM_CONFIRMED => t('Confirmed'),
MAILING_LIST_CONFIRM_CANCELLED => t('Cancelled'),
MAILING_LIST_CONFIRM_SKIPPED => t('Skipped'),
];
$fields['email_confirmation_status'] = BaseFieldDefinition::create('list_integer')
->setName('email_confirmation_status')
->setLabel(t('Email confirmation status'))
->setRequired(TRUE)
->setDefaultValue(MAILING_LIST_CONFIRM_NOT_CONFIRMED)
->setSetting('allowed_values', $values)
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 5,
'hidden' => TRUE,
])
->setDisplayConfigurable('form', TRUE);
return $fields;
}
}
function mailing_list_confirm_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
if ($field_definition
->getName() == 'email_confirmation_status' && $operation == 'edit') {
return AccessResult::forbiddenIf(!$account
->hasPermission('administer mailing list subscriptions'));
}
return AccessResult::neutral();
}
function mailing_list_confirm_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (strpos($form_id, 'mailing_list_subscription_') === 0 && isset($form['email_confirmation_status'])) {
$form['email_confirmation_status']['#group'] = 'subscription_status';
}
}
function mailing_list_confirm_mailing_list_insert(EntityInterface $entity) {
if ($block_display_mode = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('mailing_list_subscription.' . $entity
->id() . '.block')) {
$block_display_mode
->removeComponent('email_confirmation_status');
$block_display_mode
->save();
}
}
function mailing_list_confirm_mailing_list_subscription_presave(EntityInterface $entity) {
if (!$entity
->isActive() || $entity
->getOwner()
->hasPermission('bypass ' . $entity
->getListId() . ' mailing list subscription confirm')) {
return;
}
$status_field = $entity
->get('email_confirmation_status');
switch ($status_field->value) {
case MAILING_LIST_CONFIRM_CONFIRMED:
if (!isset($entity->original) || $entity->original
->getEmail() == $entity
->getEmail()) {
break;
}
case MAILING_LIST_CONFIRM_NOT_CONFIRMED:
$status_field
->setValue(MAILING_LIST_CONFIRM_TO_BE_CONFIRMED);
case MAILING_LIST_CONFIRM_TO_BE_CONFIRMED:
if ($entity
->getOwner()
->isAuthenticated() && ($confirmation = \Drupal::service('email_confirmer')
->getConfirmation($entity
->getEmail(), 'confirmed')) && $confirmation
->get('uid')->target_id == $entity
->getOwnerId()) {
$status_field
->setValue(MAILING_LIST_CONFIRM_CONFIRMED);
}
else {
$entity
->setStatus(SubscriptionInterface::INACTIVE);
}
break;
}
}
function mailing_list_confirm_mailing_list_subscription_insert(EntityInterface $entity) {
mailing_list_confirm_mailing_list_subscription_update($entity);
}
function mailing_list_confirm_mailing_list_subscription_update(EntityInterface $entity) {
$status_field = $entity
->get('email_confirmation_status');
if ($entity
->isActive() || $status_field->value != MAILING_LIST_CONFIRM_TO_BE_CONFIRMED || $entity
->getOwner()
->hasPermission('bypass ' . $entity
->getListId() . ' mailing list subscription confirm')) {
return;
}
$email = $entity
->getEmail();
$email_confirmer = \Drupal::service('email_confirmer');
$realm = 'mailing_list_confirm_' . $entity
->getListId();
if (!($confirmation = $email_confirmer
->getConfirmation($email, 'pending', $realm)) || $confirmation
->getProperty('mailing_list_sid') != $entity
->id()) {
$response_url = $entity
->getOwnerId() > 0 ? $entity
->toUrl('form-destination') : Url::fromRoute('mailing_list.access_subscription', [
'sid' => $entity
->id(),
'hash' => $entity
->getAccessHash(),
'rel' => 'form-destination',
]);
$confirmation = $email_confirmer
->createConfirmation($email)
->setProperty('mailing_list_sid', $entity
->id())
->setRealm($realm)
->setResponseUrl($response_url, 'confirm');
}
$confirmation
->sendRequest();
$confirmation
->save();
}
function mailing_list_confirm_email_confirmer($op, EmailConfirmationInterface $confirmation) {
if ($sid = $confirmation
->getProperty('mailing_list_sid')) {
$subscription = \Drupal::entityTypeManager()
->getStorage('mailing_list_subscription')
->load($sid);
if (!$subscription || $subscription
->getEmail() != $confirmation
->getEmail() || $subscription
->get('email_confirmation_status')->value != MAILING_LIST_CONFIRM_TO_BE_CONFIRMED) {
return;
}
switch ($op) {
case 'confirm':
$subscription
->get('email_confirmation_status')
->setValue(MAILING_LIST_CONFIRM_CONFIRMED);
$subscription
->setStatus(SubscriptionInterface::ACTIVE);
$subscription
->save();
\Drupal::service('mailing_list.manager')
->grantSessionAccess($subscription);
break;
case 'cancel':
$subscription
->get('email_confirmation_status')
->setValue(MAILING_LIST_CONFIRM_CANCELLED);
$subscription
->setStatus(SubscriptionInterface::INACTIVE);
$subscription
->save();
break;
}
}
}
function mailing_list_confirm_mail_alter(&$message) {
if ($message['id'] == 'email_confirmer_confirmation_request') {
$params = $message['params'];
$confirmation = $params['context']['email_confirmer_confirmation'];
if ($sid = $confirmation
->getProperty('mailing_list_sid')) {
$subscription = \Drupal::entityTypeManager()
->getStorage('mailing_list_subscription')
->load($sid);
$list = $subscription
->getList();
$message['subject'] = t('Confirm your subscription to @list on @site', [
'@list' => $list
->label(),
'@site' => \Drupal::config('system.site')
->get('name'),
]);
$message['body'] = [
t("We have received a request to subscribe @email to the @list mailing list on @site website at @site_url. Please, reply to this request using the link below.", [
'@email' => $subscription
->getEmail(),
'@list' => $list
->label(),
'@site' => \Drupal::config('system.site')
->get('name'),
'@site_url' => Url::fromRoute('<front>')
->setAbsolute()
->toString(),
]),
Url::fromRoute('entity.email_confirmer_confirmation.response_form', [
'email_confirmer_confirmation' => $confirmation
->uuid(),
'hash' => $confirmation
->getHash(),
])
->setAbsolute()
->toString(),
];
}
}
}