class EmailConfirmerResponseForm in Email confirmer 8
Email confirmation response form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\Core\Entity\EntityConfirmFormBase implements ConfirmFormInterface
- class \Drupal\email_confirmer\Form\EmailConfirmerResponseForm
- class \Drupal\Core\Entity\EntityConfirmFormBase implements ConfirmFormInterface
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of EmailConfirmerResponseForm
File
- src/
Form/ EmailConfirmerResponseForm.php, line 14
Namespace
Drupal\email_confirmer\FormView source
class EmailConfirmerResponseForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this
->config('email_confirmer.settings')
->get('confirmation_response.questions.' . $this->entity
->getStatus());
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
/** @var \Drupal\email_confirmer\EmailConfirmationInterface $confirmation */
$confirmation = $this
->getEntity();
return $confirmation
->isPending() ? $this
->t('Send') : $this
->t('OK');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return Url::fromRoute('<front>');
}
/**
* {@inheritdoc}
*/
public function getFormName() {
return $this
->getFormId();
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'email_confirmer_response';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Skip the response form and positively confirm the confirmation according
// to the settings.
if ($this
->config('email_confirmer.settings')
->get('confirmation_response.skip_confirmation_form')) {
return $this
->skipConfirmationForm($form_state);
}
// Build the response form.
$form = parent::buildForm($form, $form_state);
unset($form['#process']);
unset($form['#after_build']);
// No cancel option needed.
unset($form['actions']['cancel']);
if ($this
->getEntity()
->isPending()) {
$form['cancel'] = [
'#type' => 'radios',
'#default_value' => 0,
'#options' => [
0 => $this
->t('Confirm'),
1 => $this
->t('Cancel'),
],
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
/** @var \Drupal\email_confirmer\EmailConfirmationInterface $confirmation */
$confirmation = $this
->getEntity();
$question = '';
switch ($confirmation
->getStatus()) {
case 'pending':
// @todo obfuscate email address if no administer permission, user is anonymous or not owner
$question = $this
->t('Confirm %email', [
'%email' => $this->entity
->label(),
]);
break;
case 'expired':
if ($confirmation
->isConfirmed() || $confirmation
->isCancelled()) {
$question = $this
->t('Already processed');
}
else {
$question = $this
->t('Confirmation expired');
}
break;
case 'cancelled':
$question = $this
->t('Confirmation cancelled');
break;
case 'confirmed':
$question = $this
->t('Confirmation done');
break;
}
return $question;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Do nothing if it is not a real response.
if (!$this
->getEntity()
->isPending()) {
$form_state
->setRedirectUrl($this
->getRedirectUrl('error'));
return;
}
try {
if ($form_state
->getValue('cancel')) {
// Explicitly cancelled.
$this
->cancelConfirmation($form_state);
}
else {
// Confirmed.
$this
->confirmConfirmation($form_state);
}
} catch (InvalidConfirmationStateException $exception) {
$this
->confirmationError($form_state);
}
}
/**
* Cancels the email confirmation.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @throws \Drupal\email_confirmer\InvalidConfirmationStateException
* If confirmation is expired, confirmed or already cancelled.
*/
protected function cancelConfirmation(FormStateInterface $form_state) {
/** @var \Drupal\email_confirmer\EmailConfirmationInterface $confirmation */
$confirmation = $this
->getEntity();
$confirmation
->cancel();
$this
->messenger()
->addStatus($this
->t('Email confirmation cancelled.'));
$confirmation
->save();
$form_state
->setRedirectUrl($this
->getRedirectUrl('cancel'));
}
/**
* Confirms the email confirmation.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @throws \Drupal\email_confirmer\InvalidConfirmationStateException
* If confirmation is cancelled, expired or already confirmed.
*/
protected function confirmConfirmation(FormStateInterface $form_state) {
/** @var \Drupal\email_confirmer\EmailConfirmationInterface $confirmation */
$confirmation = $this
->getEntity();
if (!$confirmation
->confirm($this
->getRouteMatch()
->getParameter('hash'))) {
$this
->confirmationError($form_state);
return;
}
$this
->messenger()
->addStatus($this
->t('Email confirmation confirmed.'));
$confirmation
->save();
$form_state
->setRedirectUrl($this
->getRedirectUrl('confirm'));
}
/**
* Common behaviour when there ware errors in confirmation.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
protected function confirmationError(FormStateInterface $form_state) {
$this
->messenger()
->addError($this
->t('There was an error processing your email confirmation.'), 'error');
$form_state
->setRedirectUrl($this
->getRedirectUrl('error'));
}
/**
* Returns the URL to redirect to after the form is completed or skipped.
*
* @param string $operation
* Possible values:
* - confirm
* - cancel
* - error
* Defines the page that will be redirected to.
*
* @return \Drupal\Core\Url
* The url to redirect to.
*/
protected function getRedirectUrl($operation) {
/** @var \Drupal\email_confirmer\EmailConfirmationInterface $confirmation */
$confirmation = $this
->getEntity();
// Go to confirmation response URL, response path from settings or front.
if (!($url = $confirmation
->getResponseUrl($operation))) {
$path = $this
->config('email_confirmer.settings')
->get('confirmation_response.url.' . $operation) ?: '<front>';
$url = Url::fromUri('internal:/' . $path);
}
return $url;
}
/**
* Skip the response form and positively confirm the confirmation.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Redirection response according to the confirmation result.
*/
protected function skipConfirmationForm(FormStateInterface $form_state) {
try {
$this
->confirmConfirmation($form_state);
} catch (InvalidConfirmationStateException $exception) {
$this
->confirmationError($form_state);
}
// Use RedirectResponse because redirecting with form state is not working
// inside buildForm.
$redirect_state = $form_state
->getRedirect() ?: $this
->getRedirectUrl('error');
if ($redirect_state instanceof RedirectResponse) {
$redirect_response = $redirect_state;
}
else {
$redirect_response = new RedirectResponse($redirect_state instanceof Url ? $redirect_state
->setAbsolute()
->toString() : $redirect_state);
}
return $redirect_response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | |
EmailConfirmerResponseForm:: |
public | function |
Form constructor. Overrides EntityConfirmFormBase:: |
|
EmailConfirmerResponseForm:: |
protected | function | Cancels the email confirmation. | |
EmailConfirmerResponseForm:: |
protected | function | Common behaviour when there ware errors in confirmation. | |
EmailConfirmerResponseForm:: |
protected | function | Confirms the email confirmation. | |
EmailConfirmerResponseForm:: |
public | function |
Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface:: |
|
EmailConfirmerResponseForm:: |
public | function |
Returns a caption for the button that confirms the action. Overrides EntityConfirmFormBase:: |
|
EmailConfirmerResponseForm:: |
public | function |
Returns additional text to display as a description. Overrides EntityConfirmFormBase:: |
|
EmailConfirmerResponseForm:: |
public | function |
Returns a unique string identifying the form. Overrides EntityForm:: |
|
EmailConfirmerResponseForm:: |
public | function |
Returns the internal name used to refer to the confirmation item. Overrides EntityConfirmFormBase:: |
|
EmailConfirmerResponseForm:: |
public | function |
Returns the question to ask the user. Overrides ConfirmFormInterface:: |
|
EmailConfirmerResponseForm:: |
protected | function | Returns the URL to redirect to after the form is completed or skipped. | |
EmailConfirmerResponseForm:: |
protected | function | Skip the response form and positively confirm the confirmation. | |
EmailConfirmerResponseForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
|
EntityConfirmFormBase:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
1 |
EntityConfirmFormBase:: |
public | function | The delete() method is not used in EntityConfirmFormBase. This overrides the default implementation that redirects to the delete-form confirmation form. | |
EntityConfirmFormBase:: |
public | function |
Returns a string identifying the base form. Overrides EntityForm:: |
|
EntityConfirmFormBase:: |
public | function |
Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface:: |
|
EntityConfirmFormBase:: |
public | function |
The save() method is not used in EntityConfirmFormBase. This overrides the
default implementation that saves the entity. Overrides EntityForm:: |
|
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function | Gets the actual form array to be built. | 30 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
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:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
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. |