Commit 40fbf6b4 authored by alexpott's avatar alexpott

Issue #2072897 by jayeshanandani, marcingy, herom, er.pushpinderrana, inket:...

Issue #2072897 by jayeshanandani, marcingy, herom, er.pushpinderrana, inket: Modernize contact.module forms.
parent fc939d4e
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/** /**
* @file * @file
* Definition of Drupal\contact\CategoryForm. * Contains \Drupal\contact\CategoryForm.
*/ */
namespace Drupal\contact; namespace Drupal\contact;
...@@ -16,20 +16,20 @@ ...@@ -16,20 +16,20 @@
class CategoryForm extends EntityForm { class CategoryForm extends EntityForm {
/** /**
* Overrides Drupal\Core\Entity\EntityForm::form(). * {@inheritdoc}
*/ */
public function form(array $form, array &$form_state) { public function form(array $form, array &$form_state) {
$form = parent::form($form, $form_state); $form = parent::form($form, $form_state);
$category = $this->entity; $category = $this->entity;
$default_category = \Drupal::config('contact.settings')->get('default_category'); $default_category = $this->config('contact.settings')->get('default_category');
$form['label'] = array( $form['label'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Label'), '#title' => $this->t('Label'),
'#maxlength' => 255, '#maxlength' => 255,
'#default_value' => $category->label(), '#default_value' => $category->label(),
'#description' => t("Example: 'website feedback' or 'product information'."), '#description' => $this->t("Example: 'website feedback' or 'product information'."),
'#required' => TRUE, '#required' => TRUE,
); );
$form['id'] = array( $form['id'] = array(
...@@ -43,26 +43,26 @@ public function form(array $form, array &$form_state) { ...@@ -43,26 +43,26 @@ public function form(array $form, array &$form_state) {
); );
$form['recipients'] = array( $form['recipients'] = array(
'#type' => 'textarea', '#type' => 'textarea',
'#title' => t('Recipients'), '#title' => $this->t('Recipients'),
'#default_value' => implode(', ', $category->recipients), '#default_value' => implode(', ', $category->recipients),
'#description' => t("Example: 'webmaster@example.com' or 'sales@example.com,support@example.com' . To specify multiple recipients, separate each email address with a comma."), '#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, '#required' => TRUE,
); );
$form['reply'] = array( $form['reply'] = array(
'#type' => 'textarea', '#type' => 'textarea',
'#title' => t('Auto-reply'), '#title' => $this->t('Auto-reply'),
'#default_value' => $category->reply, '#default_value' => $category->reply,
'#description' => t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'), '#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'] = array(
'#type' => 'weight', '#type' => 'weight',
'#title' => t('Weight'), '#title' => $this->t('Weight'),
'#default_value' => $category->weight, '#default_value' => $category->weight,
'#description' => t('When listing categories, those with lighter (smaller) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.'), '#description' => $this->t('When listing categories, those with lighter (smaller) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.'),
); );
$form['selected'] = array( $form['selected'] = array(
'#type' => 'checkbox', '#type' => 'checkbox',
'#title' => t('Make this the default category.'), '#title' => $this->t('Make this the default category.'),
'#default_value' => $default_category === $category->id(), '#default_value' => $default_category === $category->id(),
); );
...@@ -70,7 +70,7 @@ public function form(array $form, array &$form_state) { ...@@ -70,7 +70,7 @@ public function form(array $form, array &$form_state) {
} }
/** /**
* Overrides Drupal\Core\Entity\EntityForm::validate(). * {@inheritdoc}
*/ */
public function validate(array $form, array &$form_state) { public function validate(array $form, array &$form_state) {
parent::validate($form, $form_state); parent::validate($form, $form_state);
...@@ -88,33 +88,33 @@ public function validate(array $form, array &$form_state) { ...@@ -88,33 +88,33 @@ public function validate(array $form, array &$form_state) {
} }
/** /**
* Overrides Drupal\Core\Entity\EntityForm::save(). * {@inheritdoc}
*/ */
public function save(array $form, array &$form_state) { public function save(array $form, array &$form_state) {
$category = $this->entity; $category = $this->entity;
$status = $category->save(); $status = $category->save();
$contact_settings = $this->config('contact.settings');
$edit_link = \Drupal::linkGenerator()->generateFromUrl($this->t('Edit'), $this->entity->urlInfo()); $edit_link = \Drupal::linkGenerator()->generateFromUrl($this->t('Edit'), $this->entity->urlInfo());
if ($status == SAVED_UPDATED) { if ($status == SAVED_UPDATED) {
drupal_set_message(t('Category %label has been updated.', array('%label' => $category->label()))); drupal_set_message($this->t('Category %label has been updated.', array('%label' => $category->label())));
watchdog('contact', 'Category %label has been updated.', array('%label' => $category->label()), WATCHDOG_NOTICE, $edit_link); watchdog('contact', 'Category %label has been updated.', array('%label' => $category->label()), WATCHDOG_NOTICE, $edit_link);
} }
else { else {
drupal_set_message(t('Category %label has been added.', array('%label' => $category->label()))); drupal_set_message($this->t('Category %label has been added.', array('%label' => $category->label())));
watchdog('contact', 'Category %label has been added.', array('%label' => $category->label()), WATCHDOG_NOTICE, $edit_link); watchdog('contact', 'Category %label has been added.', array('%label' => $category->label()), WATCHDOG_NOTICE, $edit_link);
} }
// Update the default category. // Update the default category.
$contact_config = \Drupal::config('contact.settings');
if ($form_state['values']['selected']) { if ($form_state['values']['selected']) {
$contact_config $contact_settings
->set('default_category', $category->id()) ->set('default_category', $category->id())
->save(); ->save();
} }
// If it was the default category, empty out the setting. // If it was the default category, empty out the setting.
elseif ($contact_config->get('default_category') == $category->id()) { elseif ($contact_settings->get('default_category') == $category->id()) {
$contact_config $contact_settings
->set('default_category', NULL) ->set('default_category', NULL)
->save(); ->save();
} }
......
...@@ -19,7 +19,7 @@ class CategoryDeleteForm extends EntityConfirmFormBase { ...@@ -19,7 +19,7 @@ class CategoryDeleteForm extends EntityConfirmFormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getQuestion() { public function getQuestion() {
return t('Are you sure you want to delete %name?', array('%name' => $this->entity->label())); return $this->t('Are you sure you want to delete %name?', array('%name' => $this->entity->label()));
} }
/** /**
...@@ -33,7 +33,7 @@ public function getCancelRoute() { ...@@ -33,7 +33,7 @@ public function getCancelRoute() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getConfirmText() { public function getConfirmText() {
return t('Delete'); return $this->t('Delete');
} }
/** /**
...@@ -41,7 +41,7 @@ public function getConfirmText() { ...@@ -41,7 +41,7 @@ public function getConfirmText() {
*/ */
public function submit(array $form, array &$form_state) { public function submit(array $form, array &$form_state) {
$this->entity->delete(); $this->entity->delete();
drupal_set_message(t('Category %label has been deleted.', array('%label' => $this->entity->label()))); drupal_set_message($this->t('Category %label has been deleted.', array('%label' => $this->entity->label())));
watchdog('contact', 'Category %label has been deleted.', array('%label' => $this->entity->label()), WATCHDOG_NOTICE); watchdog('contact', 'Category %label has been deleted.', array('%label' => $this->entity->label()), WATCHDOG_NOTICE);
$form_state['redirect_route'] = $this->getCancelRoute(); $form_state['redirect_route'] = $this->getCancelRoute();
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/** /**
* @file * @file
* Definition of Drupal\contact\MessageForm. * Contains \Drupal\contact\MessageForm.
*/ */
namespace Drupal\contact; namespace Drupal\contact;
...@@ -70,7 +70,7 @@ public static function create(ContainerInterface $container) { ...@@ -70,7 +70,7 @@ public static function create(ContainerInterface $container) {
} }
/** /**
* Overrides Drupal\Core\Entity\EntityForm::form(). * {@inheritdoc}
*/ */
public function form(array $form, array &$form_state) { public function form(array $form, array &$form_state) {
$user = $this->currentUser(); $user = $this->currentUser();
...@@ -88,7 +88,7 @@ public function form(array $form, array &$form_state) { ...@@ -88,7 +88,7 @@ public function form(array $form, array &$form_state) {
$language_configuration = $this->moduleHandler->invoke('language', 'get_default_configuration', array('contact_message', $message->getCategory()->id())); $language_configuration = $this->moduleHandler->invoke('language', 'get_default_configuration', array('contact_message', $message->getCategory()->id()));
$form['langcode'] = array( $form['langcode'] = array(
'#title' => t('Language'), '#title' => $this->t('Language'),
'#type' => 'language_select', '#type' => 'language_select',
'#default_value' => $message->getUntranslated()->language()->id, '#default_value' => $message->getUntranslated()->language()->id,
'#languages' => Language::STATE_ALL, '#languages' => Language::STATE_ALL,
...@@ -97,13 +97,13 @@ public function form(array $form, array &$form_state) { ...@@ -97,13 +97,13 @@ public function form(array $form, array &$form_state) {
$form['name'] = array( $form['name'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Your name'), '#title' => $this->t('Your name'),
'#maxlength' => 255, '#maxlength' => 255,
'#required' => TRUE, '#required' => TRUE,
); );
$form['mail'] = array( $form['mail'] = array(
'#type' => 'email', '#type' => 'email',
'#title' => t('Your email address'), '#title' => $this->t('Your email address'),
'#required' => TRUE, '#required' => TRUE,
); );
if ($user->isAnonymous()) { if ($user->isAnonymous()) {
...@@ -128,7 +128,7 @@ public function form(array $form, array &$form_state) { ...@@ -128,7 +128,7 @@ public function form(array $form, array &$form_state) {
if ($message->isPersonal()) { if ($message->isPersonal()) {
$form['recipient'] = array( $form['recipient'] = array(
'#type' => 'item', '#type' => 'item',
'#title' => t('To'), '#title' => $this->t('To'),
'#value' => $message->getPersonalRecipient()->id(), '#value' => $message->getPersonalRecipient()->id(),
'name' => array( 'name' => array(
'#theme' => 'username', '#theme' => 'username',
...@@ -139,7 +139,7 @@ public function form(array $form, array &$form_state) { ...@@ -139,7 +139,7 @@ public function form(array $form, array &$form_state) {
$form['copy'] = array( $form['copy'] = array(
'#type' => 'checkbox', '#type' => 'checkbox',
'#title' => t('Send yourself a copy.'), '#title' => $this->t('Send yourself a copy.'),
// Do not allow anonymous users to send themselves a copy, because it can // Do not allow anonymous users to send themselves a copy, because it can
// be abused to spam people. // be abused to spam people.
'#access' => $user->isAuthenticated(), '#access' => $user->isAuthenticated(),
...@@ -148,13 +148,13 @@ public function form(array $form, array &$form_state) { ...@@ -148,13 +148,13 @@ public function form(array $form, array &$form_state) {
} }
/** /**
* Overrides Drupal\Core\Entity\EntityForm::actions(). * {@inheritdoc}
*/ */
public function actions(array $form, array &$form_state) { public function actions(array $form, array &$form_state) {
$elements = parent::actions($form, $form_state); $elements = parent::actions($form, $form_state);
$elements['submit']['#value'] = t('Send message'); $elements['submit']['#value'] = $this->t('Send message');
$elements['preview'] = array( $elements['preview'] = array(
'#value' => t('Preview'), '#value' => $this->t('Preview'),
'#validate' => array( '#validate' => array(
array($this, 'validate'), array($this, 'validate'),
), ),
...@@ -176,7 +176,7 @@ public function preview(array $form, array &$form_state) { ...@@ -176,7 +176,7 @@ public function preview(array $form, array &$form_state) {
} }
/** /**
* Overrides Drupal\Core\Entity\EntityForm::save(). * {@inheritdoc}
*/ */
public function save(array $form, array &$form_state) { public function save(array $form, array &$form_state) {
$user = $this->currentUser(); $user = $this->currentUser();
...@@ -195,7 +195,7 @@ public function save(array $form, array &$form_state) { ...@@ -195,7 +195,7 @@ public function save(array $form, array &$form_state) {
user_cookie_save(array('name' => $message->getSenderName(), 'mail' => $message->getSenderMail())); user_cookie_save(array('name' => $message->getSenderName(), 'mail' => $message->getSenderMail()));
// For the email message, clarify that the sender name is not verified; it // For the email message, clarify that the sender name is not verified; it
// could potentially clash with a username on this site. // could potentially clash with a username on this site.
$sender->name = t('!name (not verified)', array('!name' => $message->getSenderName())); $sender->name = $this->t('!name (not verified)', array('!name' => $message->getSenderName()));
} }
// Build email parameters. // Build email parameters.
...@@ -217,7 +217,7 @@ public function save(array $form, array &$form_state) { ...@@ -217,7 +217,7 @@ public function save(array $form, array &$form_state) {
$params['recipient'] = $recipient; $params['recipient'] = $recipient;
} }
else { else {
throw new \RuntimeException(t('Unable to determine message recipient.')); throw new \RuntimeException($this->t('Unable to determine message recipient.'));
} }
// Send email to the recipient(s). // Send email to the recipient(s).
...@@ -236,8 +236,7 @@ public function save(array $form, array &$form_state) { ...@@ -236,8 +236,7 @@ public function save(array $form, array &$form_state) {
drupal_mail('contact', 'page_autoreply', $sender->getEmail(), $language_interface->id, $params); drupal_mail('contact', 'page_autoreply', $sender->getEmail(), $language_interface->id, $params);
} }
$config = $this->config('contact.settings'); $this->flood->register('contact', $this->config('contact.settings')->get('flood.interval'));
$this->flood->register('contact', $config->get('flood.interval'));
if (!$message->isPersonal()) { if (!$message->isPersonal()) {
watchdog('contact', '%sender-name (@sender-from) sent an email regarding %category.', array( watchdog('contact', '%sender-name (@sender-from) sent an email regarding %category.', array(
'%sender-name' => $sender->getUsername(), '%sender-name' => $sender->getUsername(),
...@@ -253,7 +252,7 @@ public function save(array $form, array &$form_state) { ...@@ -253,7 +252,7 @@ public function save(array $form, array &$form_state) {
)); ));
} }
drupal_set_message(t('Your message has been sent.')); drupal_set_message($this->t('Your message has been sent.'));
// To avoid false error messages caused by flood control, redirect away from // 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. // the contact form; either to the contacted user account or the front page.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment