updated core and modules
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Drupal\contact;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Entity\EntityForm;
|
||||
@@ -9,6 +10,8 @@ use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Form\ConfigFormBaseTrait;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Egulias\EmailValidator\EmailValidator;
|
||||
use Drupal\Core\Path\PathValidatorInterface;
|
||||
use Drupal\Core\Render\Element\PathElement;
|
||||
|
||||
/**
|
||||
* Base form for contact form edit forms.
|
||||
@@ -23,14 +26,24 @@ class ContactFormEditForm extends EntityForm implements ContainerInjectionInterf
|
||||
*/
|
||||
protected $emailValidator;
|
||||
|
||||
/**
|
||||
* The path validator.
|
||||
*
|
||||
* @var \Drupal\Core\Path\PathValidatorInterface
|
||||
*/
|
||||
protected $pathValidator;
|
||||
|
||||
/**
|
||||
* Constructs a new ContactFormEditForm.
|
||||
*
|
||||
* @param \Egulias\EmailValidator\EmailValidator $email_validator
|
||||
* The email validator.
|
||||
* @param \Drupal\Core\Path\PathValidatorInterface $path_validator
|
||||
* The path validator service.
|
||||
*/
|
||||
public function __construct(EmailValidator $email_validator) {
|
||||
$this->emailValidator = $email_validator;
|
||||
public function __construct(EmailValidator $email_validator, PathValidatorInterface $path_validator) {
|
||||
$this->emailValidator = $email_validator;
|
||||
$this->pathValidator = $path_validator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,7 +51,8 @@ class ContactFormEditForm extends EntityForm implements ContainerInjectionInterf
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('email.validator')
|
||||
$container->get('email.validator'),
|
||||
$container->get('path.validator')
|
||||
);
|
||||
}
|
||||
|
||||
@@ -58,47 +72,60 @@ class ContactFormEditForm extends EntityForm implements ContainerInjectionInterf
|
||||
$contact_form = $this->entity;
|
||||
$default_form = $this->config('contact.settings')->get('default_form');
|
||||
|
||||
$form['label'] = array(
|
||||
$form['label'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Label'),
|
||||
'#maxlength' => 255,
|
||||
'#default_value' => $contact_form->label(),
|
||||
'#description' => $this->t("Example: 'website feedback' or 'product information'."),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
$form['id'] = array(
|
||||
];
|
||||
$form['id'] = [
|
||||
'#type' => 'machine_name',
|
||||
'#default_value' => $contact_form->id(),
|
||||
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
|
||||
'#machine_name' => array(
|
||||
'#machine_name' => [
|
||||
'exists' => '\Drupal\contact\Entity\ContactForm::load',
|
||||
),
|
||||
],
|
||||
'#disabled' => !$contact_form->isNew(),
|
||||
);
|
||||
$form['recipients'] = array(
|
||||
];
|
||||
$form['recipients'] = [
|
||||
'#type' => 'textarea',
|
||||
'#title' => $this->t('Recipients'),
|
||||
'#default_value' => implode(', ', $contact_form->getRecipients()),
|
||||
'#description' => $this->t("Example: 'webmaster@example.com' or 'sales@example.com,support@example.com' . To specify multiple recipients, separate each email address with a comma."),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
$form['reply'] = array(
|
||||
];
|
||||
$form['message'] = [
|
||||
'#type' => 'textarea',
|
||||
'#title' => $this->t('Message'),
|
||||
'#default_value' => $contact_form->getMessage(),
|
||||
'#description' => $this->t('The message to display to the user after submission of this form. Leave blank for no message.'),
|
||||
];
|
||||
$form['redirect'] = [
|
||||
'#type' => 'path',
|
||||
'#title' => $this->t('Redirect path'),
|
||||
'#convert_path' => PathElement::CONVERT_NONE,
|
||||
'#default_value' => $contact_form->getRedirectPath(),
|
||||
'#description' => $this->t('Path to redirect the user to after submission of this form. For example, type "/about" to redirect to that page. Use a relative path with a slash in front.'),
|
||||
];
|
||||
$form['reply'] = [
|
||||
'#type' => 'textarea',
|
||||
'#title' => $this->t('Auto-reply'),
|
||||
'#default_value' => $contact_form->getReply(),
|
||||
'#description' => $this->t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'),
|
||||
);
|
||||
$form['weight'] = array(
|
||||
];
|
||||
$form['weight'] = [
|
||||
'#type' => 'weight',
|
||||
'#title' => $this->t('Weight'),
|
||||
'#default_value' => $contact_form->getWeight(),
|
||||
'#description' => $this->t('When listing forms, those with lighter (smaller) weights get listed before forms with heavier (larger) weights. Forms with equal weights are sorted alphabetically.'),
|
||||
);
|
||||
$form['selected'] = array(
|
||||
];
|
||||
$form['selected'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Make this the default form'),
|
||||
'#default_value' => $default_form === $contact_form->id(),
|
||||
);
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
@@ -115,10 +142,16 @@ class ContactFormEditForm extends EntityForm implements ContainerInjectionInterf
|
||||
foreach ($recipients as &$recipient) {
|
||||
$recipient = trim($recipient);
|
||||
if (!$this->emailValidator->isValid($recipient)) {
|
||||
$form_state->setErrorByName('recipients', $this->t('%recipient is an invalid email address.', array('%recipient' => $recipient)));
|
||||
$form_state->setErrorByName('recipients', $this->t('%recipient is an invalid email address.', ['%recipient' => $recipient]));
|
||||
}
|
||||
}
|
||||
$form_state->setValue('recipients', $recipients);
|
||||
$redirect_url = $form_state->getValue('redirect');
|
||||
if ($redirect_url && $this->pathValidator->isValid($redirect_url)) {
|
||||
if (Unicode::substr($redirect_url, 0, 1) !== '/') {
|
||||
$form_state->setErrorByName('redirect', $this->t('The path should start with /.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,13 +163,14 @@ class ContactFormEditForm extends EntityForm implements ContainerInjectionInterf
|
||||
$contact_settings = $this->config('contact.settings');
|
||||
|
||||
$edit_link = $this->entity->link($this->t('Edit'));
|
||||
$view_link = $contact_form->link($contact_form->label(), 'canonical');
|
||||
if ($status == SAVED_UPDATED) {
|
||||
drupal_set_message($this->t('Contact form %label has been updated.', array('%label' => $contact_form->label())));
|
||||
$this->logger('contact')->notice('Contact form %label has been updated.', array('%label' => $contact_form->label(), 'link' => $edit_link));
|
||||
drupal_set_message($this->t('Contact form %label has been updated.', ['%label' => $view_link]));
|
||||
$this->logger('contact')->notice('Contact form %label has been updated.', ['%label' => $contact_form->label(), 'link' => $edit_link]);
|
||||
}
|
||||
else {
|
||||
drupal_set_message($this->t('Contact form %label has been added.', array('%label' => $contact_form->label())));
|
||||
$this->logger('contact')->notice('Contact form %label has been added.', array('%label' => $contact_form->label(), 'link' => $edit_link));
|
||||
drupal_set_message($this->t('Contact form %label has been added.', ['%label' => $view_link]));
|
||||
$this->logger('contact')->notice('Contact form %label has been added.', ['%label' => $contact_form->label(), 'link' => $edit_link]);
|
||||
}
|
||||
|
||||
// Update the default form.
|
||||
|
||||
@@ -9,6 +9,14 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
||||
*/
|
||||
interface ContactFormInterface extends ConfigEntityInterface {
|
||||
|
||||
/**
|
||||
* Returns the message to be displayed to user.
|
||||
*
|
||||
* @return string
|
||||
* A user message.
|
||||
*/
|
||||
public function getMessage();
|
||||
|
||||
/**
|
||||
* Returns list of recipient email addresses.
|
||||
*
|
||||
@@ -17,6 +25,24 @@ interface ContactFormInterface extends ConfigEntityInterface {
|
||||
*/
|
||||
public function getRecipients();
|
||||
|
||||
/**
|
||||
* Returns the path for redirect.
|
||||
*
|
||||
* @return string
|
||||
* The redirect path.
|
||||
*/
|
||||
public function getRedirectPath();
|
||||
|
||||
/**
|
||||
* Returns the url object for redirect path.
|
||||
*
|
||||
* Empty redirect property results a url object of front page.
|
||||
*
|
||||
* @return \Drupal\core\Url
|
||||
* The redirect url object.
|
||||
*/
|
||||
public function getRedirectUrl();
|
||||
|
||||
/**
|
||||
* Returns an auto-reply message to send to the message author.
|
||||
*
|
||||
@@ -33,6 +59,16 @@ interface ContactFormInterface extends ConfigEntityInterface {
|
||||
*/
|
||||
public function getWeight();
|
||||
|
||||
/**
|
||||
* Sets the message to be displayed to the user.
|
||||
*
|
||||
* @param string $message
|
||||
* The message to display after form is submitted.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setMessage($message);
|
||||
|
||||
/**
|
||||
* Sets list of recipient email addresses.
|
||||
*
|
||||
@@ -43,6 +79,16 @@ interface ContactFormInterface extends ConfigEntityInterface {
|
||||
*/
|
||||
public function setRecipients($recipients);
|
||||
|
||||
/**
|
||||
* Sets the redirect path.
|
||||
*
|
||||
* @param string $redirect
|
||||
* The desired path.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRedirectPath($redirect);
|
||||
|
||||
/**
|
||||
* Sets an auto-reply message to send to the message author.
|
||||
*
|
||||
|
||||
@@ -64,9 +64,9 @@ class ContactController extends ControllerBase {
|
||||
// If there are no forms, do not display the form.
|
||||
if (empty($contact_form)) {
|
||||
if ($this->currentUser()->hasPermission('administer contact forms')) {
|
||||
drupal_set_message($this->t('The contact form has not been configured. <a href=":add">Add one or more forms</a> .', array(
|
||||
':add' => $this->url('contact.form_add'))), 'error');
|
||||
return array();
|
||||
drupal_set_message($this->t('The contact form has not been configured. <a href=":add">Add one or more forms</a> .', [
|
||||
':add' => $this->url('contact.form_add')]), 'error');
|
||||
return [];
|
||||
}
|
||||
else {
|
||||
throw new NotFoundHttpException();
|
||||
@@ -76,9 +76,9 @@ class ContactController extends ControllerBase {
|
||||
|
||||
$message = $this->entityManager()
|
||||
->getStorage('contact_message')
|
||||
->create(array(
|
||||
->create([
|
||||
'contact_form' => $contact_form->id(),
|
||||
));
|
||||
]);
|
||||
|
||||
$form = $this->entityFormBuilder()->getForm($message);
|
||||
$form['#title'] = $contact_form->label();
|
||||
@@ -106,13 +106,13 @@ class ContactController extends ControllerBase {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
$message = $this->entityManager()->getStorage('contact_message')->create(array(
|
||||
$message = $this->entityManager()->getStorage('contact_message')->create([
|
||||
'contact_form' => 'personal',
|
||||
'recipient' => $user->id(),
|
||||
));
|
||||
]);
|
||||
|
||||
$form = $this->entityFormBuilder()->getForm($message);
|
||||
$form['#title'] = $this->t('Contact @username', array('@username' => $user->getDisplayName()));
|
||||
$form['#title'] = $this->t('Contact @username', ['@username' => $user->getDisplayName()]);
|
||||
$form['#cache']['contexts'][] = 'user.permissions';
|
||||
return $form;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Drupal\contact\Entity;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
|
||||
use Drupal\contact\ContactFormInterface;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Defines the contact form entity.
|
||||
@@ -39,6 +40,8 @@ use Drupal\contact\ContactFormInterface;
|
||||
* "recipients",
|
||||
* "reply",
|
||||
* "weight",
|
||||
* "message",
|
||||
* "redirect",
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
@@ -58,12 +61,26 @@ class ContactForm extends ConfigEntityBundleBase implements ContactFormInterface
|
||||
*/
|
||||
protected $label;
|
||||
|
||||
/**
|
||||
* The message displayed to user on form submission.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $message;
|
||||
|
||||
/**
|
||||
* List of recipient email addresses.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $recipients = array();
|
||||
protected $recipients = [];
|
||||
|
||||
/**
|
||||
* The path to redirect to on form submission.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirect;
|
||||
|
||||
/**
|
||||
* An auto-reply message.
|
||||
@@ -79,6 +96,21 @@ class ContactForm extends ConfigEntityBundleBase implements ContactFormInterface
|
||||
*/
|
||||
protected $weight = 0;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMessage() {
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setMessage($message) {
|
||||
$this->message = $message;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -94,6 +126,34 @@ class ContactForm extends ConfigEntityBundleBase implements ContactFormInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRedirectPath() {
|
||||
return $this->redirect;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRedirectUrl() {
|
||||
if ($this->redirect) {
|
||||
$url = Url::fromUserInput($this->redirect);
|
||||
}
|
||||
else {
|
||||
$url = Url::fromRoute('<front>');
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setRedirectPath($redirect) {
|
||||
$this->redirect = $redirect;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
||||
@@ -130,24 +130,15 @@ class Message extends ContentEntityBase implements MessageInterface {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
$fields['contact_form'] = BaseFieldDefinition::create('entity_reference')
|
||||
->setLabel(t('Form ID'))
|
||||
->setDescription(t('The ID of the associated form.'))
|
||||
->setSetting('target_type', 'contact_form')
|
||||
->setRequired(TRUE);
|
||||
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
|
||||
$fields['uuid'] = BaseFieldDefinition::create('uuid')
|
||||
->setLabel(t('UUID'))
|
||||
->setDescription(t('The message UUID.'))
|
||||
->setReadOnly(TRUE);
|
||||
$fields['contact_form']->setLabel(t('Form ID'))
|
||||
->setDescription(t('The ID of the associated form.'));
|
||||
|
||||
$fields['langcode'] = BaseFieldDefinition::create('language')
|
||||
->setLabel(t('Language'))
|
||||
->setDescription(t('The message language code.'))
|
||||
->setDisplayOptions('form', array(
|
||||
'type' => 'language_select',
|
||||
'weight' => 2,
|
||||
));
|
||||
$fields['uuid']->setDescription(t('The message UUID.'));
|
||||
|
||||
$fields['langcode']->setDescription(t('The message language code.'));
|
||||
|
||||
$fields['name'] = BaseFieldDefinition::create('string')
|
||||
->setLabel(t("The sender's name"))
|
||||
@@ -162,29 +153,29 @@ class Message extends ContentEntityBase implements MessageInterface {
|
||||
->setLabel(t('Subject'))
|
||||
->setRequired(TRUE)
|
||||
->setSetting('max_length', 100)
|
||||
->setDisplayOptions('form', array(
|
||||
->setDisplayOptions('form', [
|
||||
'type' => 'string_textfield',
|
||||
'weight' => -10,
|
||||
))
|
||||
])
|
||||
->setDisplayConfigurable('form', TRUE);
|
||||
|
||||
// The text of the contact message.
|
||||
$fields['message'] = BaseFieldDefinition::create('string_long')
|
||||
->setLabel(t('Message'))
|
||||
->setRequired(TRUE)
|
||||
->setDisplayOptions('form', array(
|
||||
->setDisplayOptions('form', [
|
||||
'type' => 'string_textarea',
|
||||
'weight' => 0,
|
||||
'settings' => array(
|
||||
'settings' => [
|
||||
'rows' => 12,
|
||||
),
|
||||
))
|
||||
],
|
||||
])
|
||||
->setDisplayConfigurable('form', TRUE)
|
||||
->setDisplayOptions('view', array(
|
||||
->setDisplayOptions('view', [
|
||||
'type' => 'string',
|
||||
'weight' => 0,
|
||||
'label' => 'above',
|
||||
))
|
||||
])
|
||||
->setDisplayConfigurable('view', TRUE);
|
||||
|
||||
$fields['copy'] = BaseFieldDefinition::create('boolean')
|
||||
|
||||
@@ -73,7 +73,7 @@ class MailHandler implements MailHandlerInterface {
|
||||
public function sendMailMessages(MessageInterface $message, AccountInterface $sender) {
|
||||
// Clone the sender, as we make changes to mail and name properties.
|
||||
$sender_cloned = clone $this->userStorage->load($sender->id());
|
||||
$params = array();
|
||||
$params = [];
|
||||
$current_langcode = $this->languageManager->getCurrentLanguage()->getId();
|
||||
$recipient_langcode = $this->languageManager->getDefaultLanguage()->getId();
|
||||
$contact_form = $message->getContactForm();
|
||||
@@ -86,7 +86,7 @@ class MailHandler implements MailHandlerInterface {
|
||||
|
||||
// For the email message, clarify that the sender name is not verified; it
|
||||
// could potentially clash with a username on this site.
|
||||
$sender_cloned->name = $this->t('@name (not verified)', array('@name' => $message->getSenderName()));
|
||||
$sender_cloned->name = $this->t('@name (not verified)', ['@name' => $message->getSenderName()]);
|
||||
}
|
||||
|
||||
// Build email parameters.
|
||||
@@ -122,22 +122,29 @@ class MailHandler implements MailHandlerInterface {
|
||||
if (!$message->isPersonal() && $contact_form->getReply()) {
|
||||
// User contact forms do not support an auto-reply message, so this
|
||||
// message always originates from the site.
|
||||
$this->mailManager->mail('contact', 'page_autoreply', $sender_cloned->getEmail(), $current_langcode, $params);
|
||||
if (!$sender_cloned->getEmail()) {
|
||||
$this->logger->error('Error sending auto-reply, missing sender e-mail address in %contact_form', [
|
||||
'%contact_form' => $contact_form->label(),
|
||||
]);
|
||||
}
|
||||
else {
|
||||
$this->mailManager->mail('contact', 'page_autoreply', $sender_cloned->getEmail(), $current_langcode, $params);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$message->isPersonal()) {
|
||||
$this->logger->notice('%sender-name (@sender-from) sent an email regarding %contact_form.', array(
|
||||
$this->logger->notice('%sender-name (@sender-from) sent an email regarding %contact_form.', [
|
||||
'%sender-name' => $sender_cloned->getUsername(),
|
||||
'@sender-from' => $sender_cloned->getEmail(),
|
||||
'%contact_form' => $contact_form->label(),
|
||||
));
|
||||
]);
|
||||
}
|
||||
else {
|
||||
$this->logger->notice('%sender-name (@sender-from) sent %recipient-name an email.', array(
|
||||
$this->logger->notice('%sender-name (@sender-from) sent %recipient-name an email.', [
|
||||
'%sender-name' => $sender_cloned->getUsername(),
|
||||
'@sender-from' => $sender_cloned->getEmail(),
|
||||
'%recipient-name' => $message->getPersonalRecipient()->getUsername(),
|
||||
));
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace Drupal\contact;
|
||||
|
||||
use Drupal\Component\Datetime\TimeInterface;
|
||||
use Drupal\Core\Datetime\DateFormatterInterface;
|
||||
use Drupal\Core\Entity\ContentEntityForm;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
|
||||
use Drupal\Core\Flood\FloodInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Language\LanguageManagerInterface;
|
||||
@@ -63,9 +65,13 @@ class MessageForm extends ContentEntityForm {
|
||||
* The contact mail handler service.
|
||||
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
|
||||
* The date service.
|
||||
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
|
||||
* The entity type bundle service.
|
||||
* @param \Drupal\Component\Datetime\TimeInterface $time
|
||||
* The time service.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entity_manager, FloodInterface $flood, LanguageManagerInterface $language_manager, MailHandlerInterface $mail_handler, DateFormatterInterface $date_formatter) {
|
||||
parent::__construct($entity_manager);
|
||||
public function __construct(EntityManagerInterface $entity_manager, FloodInterface $flood, LanguageManagerInterface $language_manager, MailHandlerInterface $mail_handler, DateFormatterInterface $date_formatter, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
|
||||
parent::__construct($entity_manager, $entity_type_bundle_info, $time);
|
||||
$this->flood = $flood;
|
||||
$this->languageManager = $language_manager;
|
||||
$this->mailHandler = $mail_handler;
|
||||
@@ -81,7 +87,9 @@ class MessageForm extends ContentEntityForm {
|
||||
$container->get('flood'),
|
||||
$container->get('language_manager'),
|
||||
$container->get('contact.mail_handler'),
|
||||
$container->get('date.formatter')
|
||||
$container->get('date.formatter'),
|
||||
$container->get('entity_type.bundle.info'),
|
||||
$container->get('datetime.time')
|
||||
);
|
||||
}
|
||||
|
||||
@@ -95,24 +103,24 @@ class MessageForm extends ContentEntityForm {
|
||||
$form['#attributes']['class'][] = 'contact-form';
|
||||
|
||||
if (!empty($message->preview)) {
|
||||
$form['preview'] = array(
|
||||
'#theme_wrappers' => array('container__preview'),
|
||||
'#attributes' => array('class' => array('preview')),
|
||||
);
|
||||
$form['preview'] = [
|
||||
'#theme_wrappers' => ['container__preview'],
|
||||
'#attributes' => ['class' => ['preview']],
|
||||
];
|
||||
$form['preview']['message'] = $this->entityManager->getViewBuilder('contact_message')->view($message, 'full');
|
||||
}
|
||||
|
||||
$form['name'] = array(
|
||||
$form['name'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Your name'),
|
||||
'#maxlength' => 255,
|
||||
'#required' => TRUE,
|
||||
);
|
||||
$form['mail'] = array(
|
||||
];
|
||||
$form['mail'] = [
|
||||
'#type' => 'email',
|
||||
'#title' => $this->t('Your email address'),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
];
|
||||
if ($user->isAnonymous()) {
|
||||
$form['#attached']['library'][] = 'core/drupal.form';
|
||||
$form['#attributes']['data-user-info-from-browser'] = TRUE;
|
||||
@@ -121,9 +129,9 @@ class MessageForm extends ContentEntityForm {
|
||||
// prevent the impersonation of other users.
|
||||
else {
|
||||
$form['name']['#type'] = 'item';
|
||||
$form['name']['#value'] = $user->getUsername();
|
||||
$form['name']['#value'] = $user->getDisplayName();
|
||||
$form['name']['#required'] = FALSE;
|
||||
$form['name']['#plain_text'] = $user->getUsername();
|
||||
$form['name']['#plain_text'] = $user->getDisplayName();
|
||||
|
||||
$form['mail']['#type'] = 'item';
|
||||
$form['mail']['#value'] = $user->getEmail();
|
||||
@@ -133,24 +141,24 @@ class MessageForm extends ContentEntityForm {
|
||||
|
||||
// The user contact form has a preset recipient.
|
||||
if ($message->isPersonal()) {
|
||||
$form['recipient'] = array(
|
||||
$form['recipient'] = [
|
||||
'#type' => 'item',
|
||||
'#title' => $this->t('To'),
|
||||
'#value' => $message->getPersonalRecipient()->id(),
|
||||
'name' => array(
|
||||
'name' => [
|
||||
'#theme' => 'username',
|
||||
'#account' => $message->getPersonalRecipient(),
|
||||
),
|
||||
);
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$form['copy'] = array(
|
||||
$form['copy'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Send yourself a copy'),
|
||||
// Do not allow anonymous users to send themselves a copy, because it can
|
||||
// be abused to spam people.
|
||||
'#access' => $user->isAuthenticated(),
|
||||
);
|
||||
];
|
||||
return $form;
|
||||
}
|
||||
|
||||
@@ -160,11 +168,11 @@ class MessageForm extends ContentEntityForm {
|
||||
public function actions(array $form, FormStateInterface $form_state) {
|
||||
$elements = parent::actions($form, $form_state);
|
||||
$elements['submit']['#value'] = $this->t('Send message');
|
||||
$elements['preview'] = array(
|
||||
$elements['preview'] = [
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Preview'),
|
||||
'#submit' => array('::submitForm', '::preview'),
|
||||
);
|
||||
'#submit' => ['::submitForm', '::preview'],
|
||||
];
|
||||
return $elements;
|
||||
}
|
||||
|
||||
@@ -189,10 +197,10 @@ class MessageForm extends ContentEntityForm {
|
||||
$interval = $this->config('contact.settings')->get('flood.interval');
|
||||
|
||||
if (!$this->flood->isAllowed('contact', $limit, $interval)) {
|
||||
$form_state->setErrorByName('', $this->t('You cannot send more than %limit messages in @interval. Try again later.', array(
|
||||
$form_state->setErrorByName('', $this->t('You cannot send more than %limit messages in @interval. Try again later.', [
|
||||
'%limit' => $limit,
|
||||
'@interval' => $this->dateFormatter->formatInterval($interval),
|
||||
)));
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,10 +213,17 @@ class MessageForm extends ContentEntityForm {
|
||||
public function save(array $form, FormStateInterface $form_state) {
|
||||
$message = $this->entity;
|
||||
$user = $this->currentUser();
|
||||
// Save the message. In core this is a no-op but should contrib wish to
|
||||
// implement message storage, this will make the task of swapping in a real
|
||||
// storage controller straight-forward.
|
||||
$message->save();
|
||||
$this->mailHandler->sendMailMessages($message, $user);
|
||||
$contact_form = $message->getContactForm();
|
||||
|
||||
$this->flood->register('contact', $this->config('contact.settings')->get('flood.interval'));
|
||||
drupal_set_message($this->t('Your message has been sent.'));
|
||||
if ($submission_message = $contact_form->getMessage()) {
|
||||
drupal_set_message($submission_message);
|
||||
}
|
||||
|
||||
// To avoid false error messages caused by flood control, redirect away from
|
||||
// the contact form; either to the contacted user account or the front page.
|
||||
@@ -216,12 +231,8 @@ class MessageForm extends ContentEntityForm {
|
||||
$form_state->setRedirectUrl($message->getPersonalRecipient()->urlInfo());
|
||||
}
|
||||
else {
|
||||
$form_state->setRedirect('<front>');
|
||||
$form_state->setRedirectUrl($contact_form->getRedirectUrl());
|
||||
}
|
||||
// Save the message. In core this is a no-op but should contrib wish to
|
||||
// implement message storage, this will make the task of swapping in a real
|
||||
// storage controller straight-forward.
|
||||
$message->save();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,25 +23,6 @@ class MessageViewBuilder extends EntityViewBuilder {
|
||||
return $build;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
|
||||
parent::buildComponents($build, $entities, $displays, $view_mode);
|
||||
|
||||
foreach ($entities as $id => $entity) {
|
||||
// Add the message extra field, if enabled.
|
||||
$display = $displays[$entity->bundle()];
|
||||
if ($entity->getMessage() && $display->getComponent('message')) {
|
||||
$build[$id]['message'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Message'),
|
||||
'#plain_text' => $entity->getMessage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -54,7 +35,7 @@ class MessageViewBuilder extends EntityViewBuilder {
|
||||
// convert DIVs correctly.
|
||||
foreach (Element::children($build) as $key) {
|
||||
if (isset($build[$key]['#label_display']) && $build[$key]['#label_display'] == 'above') {
|
||||
$build[$key] += array('#prefix' => '');
|
||||
$build[$key] += ['#prefix' => ''];
|
||||
$build[$key]['#prefix'] = $build[$key]['#title'] . ":\n";
|
||||
$build[$key]['#label_display'] = 'hidden';
|
||||
}
|
||||
|
||||
@@ -20,14 +20,14 @@ class ContactCategory extends DrupalSqlBase {
|
||||
*/
|
||||
public function query() {
|
||||
$query = $this->select('contact', 'c')
|
||||
->fields('c', array(
|
||||
->fields('c', [
|
||||
'cid',
|
||||
'category',
|
||||
'recipients',
|
||||
'reply',
|
||||
'weight',
|
||||
'selected',
|
||||
)
|
||||
]
|
||||
);
|
||||
$query->orderBy('c.cid');
|
||||
return $query;
|
||||
@@ -45,14 +45,14 @@ class ContactCategory extends DrupalSqlBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function fields() {
|
||||
return array(
|
||||
return [
|
||||
'cid' => $this->t('Primary Key: Unique category ID.'),
|
||||
'category' => $this->t('Category name.'),
|
||||
'recipients' => $this->t('Comma-separated list of recipient email addresses.'),
|
||||
'reply' => $this->t('Text of the auto-reply message.'),
|
||||
'weight' => $this->t("The category's weight."),
|
||||
'selected' => $this->t('Flag to indicate whether or not category is selected by default. (1 = Yes, 0 = No)'),
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,8 +42,8 @@ class ContactLink extends LinkBase {
|
||||
$this->options['alter']['make_link'] = TRUE;
|
||||
$this->options['alter']['url'] = $this->getUrlInfo($row);
|
||||
|
||||
$title = $this->t('Contact %user', array('%user' => $entity->label()));
|
||||
$this->options['alter']['attributes'] = array('title' => $title);
|
||||
$title = $this->t('Contact %user', ['%user' => $entity->label()]);
|
||||
$this->options['alter']['attributes'] = ['title' => $title];
|
||||
|
||||
if (!empty($this->options['text'])) {
|
||||
return $this->options['text'];
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\contact\Tests\Update;
|
||||
|
||||
use Drupal\system\Tests\Update\UpdatePathTestBase;
|
||||
|
||||
/**
|
||||
* Tests contact update path.
|
||||
*
|
||||
* @group contact
|
||||
*/
|
||||
class ContactUpdateTest extends UpdatePathTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setDatabaseDumpFiles() {
|
||||
$this->databaseDumpFiles = [
|
||||
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests contact_form updates.
|
||||
*
|
||||
* @see contact_post_update_add_message_redirect_field_to_contact_form()
|
||||
*/
|
||||
public function testPostUpdateContactFormFields() {
|
||||
// Check that contact_form does not have fields redirect and message.
|
||||
$config_factory = \Drupal::configFactory();
|
||||
// Check that contact_form entities are more than zero.
|
||||
$contact_forms = $config_factory->listAll('contact.form.');
|
||||
$this->assertTrue(count($contact_forms), 'There are contact forms to update.');
|
||||
foreach ($contact_forms as $contact_config_name) {
|
||||
$contact_form_data = $config_factory->get($contact_config_name)->get();
|
||||
$this->assertFalse(isset($contact_form_data['message']), 'Prior to running the update the "message" key does not exist.');
|
||||
$this->assertFalse(isset($contact_form_data['redirect']), 'Prior to running the update the "redirect" key does not exist.');
|
||||
}
|
||||
|
||||
// Run updates.
|
||||
$this->runUpdates();
|
||||
|
||||
// Check that the contact_form entities have been updated.
|
||||
foreach ($contact_forms as $contact_config_name) {
|
||||
$contact_form_data = $config_factory->get($contact_config_name)->get();
|
||||
$this->assertTrue(isset($contact_form_data['message']), 'After running the update the "message" key exists.');
|
||||
$this->assertEqual('Your message has been sent.', $contact_form_data['message']);
|
||||
$this->assertTrue(isset($contact_form_data['redirect']), 'After running the update the "redirect" key exists.');
|
||||
$this->assertEqual('', $contact_form_data['redirect']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user