class EmailExampleGetFormPage in Examples for Developers 8
Same name and namespace in other branches
- 3.x modules/email_example/src/Form/EmailExampleGetFormPage.php \Drupal\email_example\Form\EmailExampleGetFormPage
File test form class.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\email_example\Form\EmailExampleGetFormPage
Expanded class hierarchy of EmailExampleGetFormPage
Related topics
1 string reference to 'EmailExampleGetFormPage'
- email_example.routing.yml in email_example/
email_example.routing.yml - email_example/email_example.routing.yml
File
- email_example/
src/ Form/ EmailExampleGetFormPage.php, line 17
Namespace
Drupal\email_example\FormView source
class EmailExampleGetFormPage extends FormBase {
/**
* The mail manager.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* The email validator.
*
* @var \Drupal\Component\Utility\EmailValidator
*/
protected $emailValidator;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new EmailExampleGetFormPage.
*
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* The mail manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Component\Utility\EmailValidator $email_validator
* The email validator.
*/
public function __construct(MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager, EmailValidator $email_validator) {
$this->mailManager = $mail_manager;
$this->languageManager = $language_manager;
$this->emailValidator = $email_validator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$form = new static($container
->get('plugin.manager.mail'), $container
->get('language_manager'), $container
->get('email.validator'));
$form
->setMessenger($container
->get('messenger'));
$form
->setStringTranslation($container
->get('string_translation'));
return $form;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'email_example';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['intro'] = [
'#markup' => $this
->t('Use this form to send a message to an e-mail address. No spamming!'),
];
$form['email'] = [
'#type' => 'textfield',
'#title' => $this
->t('E-mail address'),
'#required' => TRUE,
];
$form['message'] = [
'#type' => 'textarea',
'#title' => $this
->t('Message'),
'#required' => TRUE,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!$this->emailValidator
->isValid($form_state
->getValue('email'))) {
$form_state
->setErrorByName('email', $this
->t('That e-mail address is not valid.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_values = $form_state
->getValues();
// All system mails need to specify the module and template key (mirrored
// from hook_mail()) that the message they want to send comes from.
$module = 'email_example';
$key = 'contact_message';
// Specify 'to' and 'from' addresses.
$to = $form_values['email'];
$from = $this
->config('system.site')
->get('mail');
// "params" loads in additional context for email content completion in
// hook_mail(). In this case, we want to pass in the values the user entered
// into the form, which include the message body in $form_values['message'].
$params = $form_values;
// The language of the e-mail. This will one of three values:
// - $account->getPreferredLangcode(): Used for sending mail to a particular
// website user, so that the mail appears in their preferred language.
// - \Drupal::currentUser()->getPreferredLangcode(): Used when sending a
// mail back to the user currently viewing the site. This will send it in
// the language they're currently using.
// - \Drupal::languageManager()->getDefaultLanguage()->getId: Used when
// sending mail to a pre-existing, 'neutral' address, such as the system
// e-mail address, or when you're unsure of the language preferences of
// the intended recipient.
//
// Since in our case, we are sending a message to a random e-mail address
// that is not necessarily tied to a user account, we will use the site's
// default language.
$language_code = $this->languageManager
->getDefaultLanguage()
->getId();
// Whether or not to automatically send the mail when we call mail() on the
// mail manager. This defaults to TRUE, and is normally what you want unless
// you need to do additional processing before the mail manager sends the
// message.
$send_now = TRUE;
// Send the mail, and check for success. Note that this does not guarantee
// message delivery; only that there were no PHP-related issues encountered
// while sending.
$result = $this->mailManager
->mail($module, $key, $to, $language_code, $params, $from, $send_now);
if ($result['result'] == TRUE) {
$this
->messenger()
->addMessage($this
->t('Your message has been sent.'));
}
else {
$this
->messenger()
->addMessage($this
->t('There was a problem sending your message and it was not sent.'), 'error');
}
}
}
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 | |
EmailExampleGetFormPage:: |
protected | property | The email validator. | |
EmailExampleGetFormPage:: |
protected | property | The language manager. | |
EmailExampleGetFormPage:: |
protected | property | The mail manager. | |
EmailExampleGetFormPage:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EmailExampleGetFormPage:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EmailExampleGetFormPage:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EmailExampleGetFormPage:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EmailExampleGetFormPage:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
EmailExampleGetFormPage:: |
public | function | Constructs a new EmailExampleGetFormPage. | |
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. |