You are here

class AnonymousSubscriptionAccessForm in Mailing List 8

Builds the form to request access to own subscriptions for anonymous users.

Hierarchy

Expanded class hierarchy of AnonymousSubscriptionAccessForm

File

src/Form/AnonymousSubscriptionAccessForm.php, line 16

Namespace

Drupal\mailing_list\Form
View source
class AnonymousSubscriptionAccessForm extends FormBase {

  /**
   * The mailing list subscription entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $subscriptionStorage;

  /**
   * The mail manager.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * Construct a new AnonymousSubscriptionAccessForm object.
   *
   * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
   *   The mail manager.
   * @param \Drupal\Core\Entity\EntityStorageInterface $subscription_storage
   *   The mailing list subscription entity storage.
   */
  public function __construct(EntityStorageInterface $subscription_storage, MailManagerInterface $mail_manager) {
    $this->subscriptionStorage = $subscription_storage;
    $this->mailManager = $mail_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager')
      ->getStorage('mailing_list_subscription'), $container
      ->get('plugin.manager.mail'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'mailing_list_anonymous_subscription_access_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = [
      '#title' => $this
        ->t('Manage your subscriptions'),
      'indications' => [
        '#type' => '#markup',
        '#markup' => $this
          ->t('Enter your email address to receive a message with a link to manage your subscriptions.'),
      ],
      'email' => [
        '#type' => 'email',
        '#title' => $this
          ->t('Your email'),
        '#required' => TRUE,
      ],
      'actions' => [
        'submit' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Send'),
          '#submit' => [
            '::submitForm',
          ],
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // No validations needed.
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Search for at least one active subscription for the given email address.
    $email = $form_state
      ->getValue('email');
    $subscription_result = $this->subscriptionStorage
      ->getQuery()
      ->condition('status', SubscriptionInterface::ACTIVE)
      ->condition('email', $email)
      ->condition('uid', 0)
      ->range(0, 1)
      ->execute();
    if (count($subscription_result)) {

      /** @var \Drupal\mailing_list\SubscriptionInterface $subscription */
      $subscription = $this->subscriptionStorage
        ->load(array_pop($subscription_result));
      $manage_url = Url::fromRoute('mailing_list.access_subscription', [
        'sid' => $subscription
          ->id(),
        'hash' => $subscription
          ->getAccessHash(),
        'rel' => 'manage',
      ]);
      if ($manage_url
        ->access()) {
        $this->mailManager
          ->mail('mailing_list', 'anonymous_subscription_access', $email, $subscription
          ->language(), [
          'manage_url' => $manage_url
            ->setAbsolute()
            ->toString(),
        ]);
      }
    }

    // Returns the same message to prevent subscribers email disclosure.
    drupal_set_message($this
      ->t('Your request has been successfully processed. You will receive a message with access instructions only if at least one active subscription was found for your email. If you do not receive any messages in short, you probably do not have any active subscription on this site.'));
    $form_state
      ->setRedirectUrl(Url::fromRoute('<front>'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnonymousSubscriptionAccessForm::$mailManager protected property The mail manager.
AnonymousSubscriptionAccessForm::$subscriptionStorage protected property The mailing list subscription entity storage.
AnonymousSubscriptionAccessForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
AnonymousSubscriptionAccessForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
AnonymousSubscriptionAccessForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AnonymousSubscriptionAccessForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
AnonymousSubscriptionAccessForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
AnonymousSubscriptionAccessForm::__construct public function Construct a new AnonymousSubscriptionAccessForm object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.