class AnonymousSubscriptionAccessForm in Mailing List 8
Builds the form to request access to own subscriptions for anonymous users.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\mailing_list\Form\AnonymousSubscriptionAccessForm
Expanded class hierarchy of AnonymousSubscriptionAccessForm
File
- src/
Form/ AnonymousSubscriptionAccessForm.php, line 16
Namespace
Drupal\mailing_list\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AnonymousSubscriptionAccessForm:: |
protected | property | The mail manager. | |
AnonymousSubscriptionAccessForm:: |
protected | property | The mailing list subscription entity storage. | |
AnonymousSubscriptionAccessForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
AnonymousSubscriptionAccessForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
AnonymousSubscriptionAccessForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
AnonymousSubscriptionAccessForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
AnonymousSubscriptionAccessForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
AnonymousSubscriptionAccessForm:: |
public | function | Construct a new AnonymousSubscriptionAccessForm object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |