class ContactForm in Email Contact 8
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\email_contact\Form\ContactForm
Expanded class hierarchy of ContactForm
2 files declare their use of ContactForm
- ContactController.php in src/
Controller/ ContactController.php - EmailContactInlineFormatter.php in src/
Plugin/ Field/ FieldFormatter/ EmailContactInlineFormatter.php
File
- src/
Form/ ContactForm.php, line 10
Namespace
Drupal\email_contact\FormView source
class ContactForm extends FormBase {
private $entity_type;
private $entity_id;
private $field_name;
private $field_settings;
public function __construct($entity_type = NULL, $entity_id = NULL, $field_name = NULL, $field_settings = NULL) {
$this->entity_type = $entity_type;
$this->entity_id = $entity_id;
$this->field_name = $field_name;
$this->field_settings = $field_settings;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
$form_id = 'email_contact_mail';
if ($this->entity_id) {
$form_id .= '_' . $this->entity_id;
}
return $form_id . '_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// $this->entity_id = $id;
$user = \Drupal::currentUser();
$emails = email_contact_get_emails_from_field($this->entity_type, $this->entity_id, $this->field_name);
$form['emails'] = array(
'#type' => 'value',
'#value' => serialize($emails),
);
$form['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Your name'),
'#maxlength' => 255,
'#default_value' => $user
->id() ? $user
->getDisplayName() : '',
'#required' => TRUE,
];
$form['mail'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Your e-mail address'),
'#maxlength' => 255,
'#default_value' => $user
->id() ? $user
->getEmail() : '',
'#required' => TRUE,
);
$form['subject'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Subject'),
'#maxlength' => 255,
'#required' => TRUE,
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => $this
->t('Message'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Send e-mail'),
);
if (!$form_state
->get('settings')) {
$form_state
->set('settings', $this->field_settings);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!\Drupal::service('email.validator')
->isValid($form_state
->getValue('mail'))) {
$form_state
->setErrorByName('mail', $this
->t('You must enter a valid e-mail address.'));
}
if (preg_match("/\r|\n/", $form_state
->getValue('subject'))) {
$form_state
->setErrorByName('subject', $this
->t('The subject cannot contain linebreaks.'));
$msg = 'Email injection exploit attempted in email form subject: @subject';
$this
->logger('email_contact')
->notice($msg, [
'@subject' => $form_state['values']['subject'],
]);
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$emails = unserialize($form_state
->getValue('emails'));
// E-mail address of the sender: as the form field is a text field,
// all instances of \r and \n have been automatically stripped from it.
$reply_to = $form_state
->getValue('mail');
$settings = $form_state
->get('settings');
$params['subject'] = $form_state
->getValue('subject');
$params['name'] = $form_state
->getValue('name');
$params['default_message'] = $settings['default_message'];
$message = '';
if ($settings['include_values']) {
$message .= 'Name: ' . $params['name'] . '<br/>' . 'Email: ' . $reply_to . '<br/>';
}
$message .= '<br/>Message: ' . $form_state
->getValue('message');
$params['message'] = Markup::create($message);
// Send the e-mail to the recipients.
$mailManager = \Drupal::service('plugin.manager.mail');
$to = implode(', ', $emails);
$module = 'email_contact';
$key = 'contact';
$langcode = \Drupal::currentUser()
->getPreferredLangcode();
$send = true;
$result = $mailManager
->mail($module, $key, $to, $langcode, $params, $reply_to, $send);
if ($result['result'] !== true) {
$this
->messenger()
->addError($this
->t('There was a problem sending your message and it was not sent.'));
}
else {
$this
->messenger()
->addStatus($this
->t('Your message has been sent.'));
$msg = 'Email sent from: @replyto to: @to about: "@subject" containing: "@message"';
$this
->logger('email_contact')
->notice($msg, [
'@name' => $params['name'],
'@replyto' => $reply_to,
'@to' => $to,
'@subject' => $params['subject'],
'@message' => $params['message'],
]);
}
$redirect = '/';
if (!empty($settings['redirection_to'])) {
switch ($settings['redirection_to']) {
case 'current':
$redirect = NULL;
break;
case 'custom':
$redirect = $settings['custom_path'];
break;
default:
// TODO: $form_state['redirect'] = $path.
break;
}
}
if ($redirect) {
$url = Url::fromUserInput($redirect);
$form_state
->setRedirectUrl($url);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContactForm:: |
private | property | ||
ContactForm:: |
private | property | ||
ContactForm:: |
private | property | ||
ContactForm:: |
private | property | ||
ContactForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ContactForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ContactForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ContactForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
ContactForm:: |
public | function | ||
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:: |
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. | |
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. |