From 158863a08ce36598901978148c8366d2bf4e685e Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Sun, 13 Jul 2014 09:16:47 +0100 Subject: [PATCH] Issue #1856562 by andypost | sun: Convert "Subject" and "Message" into Message base fields. --- .../Field/FieldFormatter/StringFormatter.php | 1 + .../Plugin/Field/FieldType/StringLongItem.php | 2 + .../FieldWidget/StringTextareaWidget.php | 86 +++++++++++++++++++ .../src/Tests/ConfigTranslationUiTest.php | 4 +- core/modules/contact/contact.module | 16 ---- core/modules/contact/src/Entity/Message.php | 38 ++++++-- core/modules/contact/src/MessageForm.php | 13 --- .../Tests/ContactAuthenticatedUserTest.php | 2 +- .../contact/src/Tests/ContactPersonalTest.php | 21 +++-- .../contact/src/Tests/ContactSitewideTest.php | 12 +-- .../Field/FieldWidget/TextareaWidget.php | 67 ++------------- 11 files changed, 148 insertions(+), 114 deletions(-) create mode 100644 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php index 20dcb808bee5..544ae4682659 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php @@ -19,6 +19,7 @@ * label = @Translation("Plain text"), * field_types = { * "string", + * "string_long", * "email" * }, * quickedit = { diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringLongItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringLongItem.php index 6337c120c0a1..ec7928131ff3 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringLongItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringLongItem.php @@ -16,6 +16,8 @@ * id = "string_long", * label = @Translation("Long string"), * description = @Translation("An entity field containing a long string value."), + * default_widget = "string_textarea", + * default_formatter = "string", * no_ui = TRUE * ) */ diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php new file mode 100644 index 000000000000..fae98b156bc6 --- /dev/null +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php @@ -0,0 +1,86 @@ +<?php + +/** + * @file + * Contains \Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextareaWidget. + */ + +namespace Drupal\Core\Field\Plugin\Field\FieldWidget; + +use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Field\WidgetBase; + +/** + * Plugin implementation of the 'string_textarea' widget. + * + * @FieldWidget( + * id = "string_textarea", + * label = @Translation("Text area (multiple rows)"), + * field_types = { + * "string_long" + * } + * ) + */ +class StringTextareaWidget extends WidgetBase { + + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + return array( + 'rows' => '5', + 'placeholder' => '', + ) + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, array &$form_state) { + $element['rows'] = array( + '#type' => 'number', + '#title' => t('Rows'), + '#default_value' => $this->getSetting('rows'), + '#required' => TRUE, + '#min' => 1, + ); + $element['placeholder'] = array( + '#type' => 'textfield', + '#title' => t('Placeholder'), + '#default_value' => $this->getSetting('placeholder'), + '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), + ); + return $element; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = array(); + + $summary[] = t('Number of rows: !rows', array('!rows' => $this->getSetting('rows'))); + $placeholder = $this->getSetting('placeholder'); + if (!empty($placeholder)) { + $summary[] = t('Placeholder: @placeholder', array('@placeholder' => $placeholder)); + } + + return $summary; + } + + /** + * {@inheritdoc} + */ + public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) { + $element['value'] = $element + array( + '#type' => 'textarea', + '#default_value' => $items[$delta]->value, + '#rows' => $this->getSetting('rows'), + '#placeholder' => $this->getSetting('placeholder'), + '#attributes' => array('class' => array('text-full')), + ); + + return $element; + } + +} diff --git a/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php b/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php index 0e78018d8e16..e0a0ddbbff33 100644 --- a/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php +++ b/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php @@ -294,8 +294,8 @@ public function testContactConfigEntityTranslation() { // Submit feedback. $edit = array( - 'subject' => 'Test subject', - 'message' => 'Test message', + 'subject[0][value]' => 'Test subject', + 'message[0][value]' => 'Test message', ); $this->drupalPostForm(NULL, $edit, t('Send message')); } diff --git a/core/modules/contact/contact.module b/core/modules/contact/contact.module index 340b5185010a..e8485c11c1ae 100644 --- a/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -85,27 +85,11 @@ function contact_entity_extra_field_info() { 'weight' => -30, ); } - $fields['contact_message'][$bundle]['form']['subject'] = array( - 'label' => t('Subject'), - 'description' => t('Text'), - 'weight' => -10, - ); - $fields['contact_message'][$bundle]['form']['message'] = array( - 'label' => t('Message'), - 'description' => t('Long text'), - 'weight' => 0, - ); $fields['contact_message'][$bundle]['form']['copy'] = array( 'label' => t('Send copy to sender'), 'description' => t('Option'), 'weight' => 50, ); - - $fields['contact_message'][$bundle]['display']['message'] = array( - 'label' => t('Message'), - 'description' => t('The main contact message'), - 'weight' => 0, - ); } $fields['user']['user']['form']['contact'] = array( diff --git a/core/modules/contact/src/Entity/Message.php b/core/modules/contact/src/Entity/Message.php index f2c05fc9d5ed..d3c8c38ea08c 100644 --- a/core/modules/contact/src/Entity/Message.php +++ b/core/modules/contact/src/Entity/Message.php @@ -151,7 +151,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['category'] = FieldDefinition::create('entity_reference') ->setLabel(t('Category ID')) ->setDescription(t('The ID of the associated category.')) - ->setSettings(array('target_type' => 'contact_category')) + ->setSetting('target_type', 'contact_category') ->setRequired(TRUE); $fields['name'] = FieldDefinition::create('string') @@ -162,13 +162,35 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(t("The sender's email")) ->setDescription(t('The email of the person that is sending the contact message.')); + // The subject of the contact message. $fields['subject'] = FieldDefinition::create('string') - ->setLabel(t('The message subject')) - ->setDescription(t('The subject of the contact message.')); - - $fields['message'] = FieldDefinition::create('string') - ->setLabel(t('The message text')) - ->setDescription(t('The text of the contact message.')); + ->setLabel(t('Subject')) + ->setRequired(TRUE) + ->setSetting('max_length', 100) + ->setDisplayOptions('form', array( + 'type' => 'string', + 'weight' => -10, + )) + ->setDisplayConfigurable('form', TRUE); + + // The text of the contact message. + $fields['message'] = FieldDefinition::create('string_long') + ->setLabel(t('Message')) + ->setRequired(TRUE) + ->setDisplayOptions('form', array( + 'type' => 'string_textarea', + 'weight' => 0, + 'settings' => array( + 'rows' => 12, + ), + )) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayOptions('view', array( + 'type' => 'string', + 'weight' => 0, + 'label' => 'above', + )) + ->setDisplayConfigurable('view', TRUE); $fields['copy'] = FieldDefinition::create('boolean') ->setLabel(t('Copy')) @@ -177,7 +199,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['recipient'] = FieldDefinition::create('entity_reference') ->setLabel(t('Recipient ID')) ->setDescription(t('The ID of the recipient user for personal contact messages.')) - ->setSettings(array('target_type' => 'user')); + ->setSetting('target_type', 'user'); return $fields; } diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index 85dabd1afb94..a9729f930bf5 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -116,19 +116,6 @@ public function form(array $form, array &$form_state) { ); } - $form['subject'] = array( - '#type' => 'textfield', - '#title' => t('Subject'), - '#maxlength' => 100, - '#required' => TRUE, - ); - $form['message'] = array( - '#type' => 'textarea', - '#title' => t('Message'), - '#required' => TRUE, - '#rows' => 12, - ); - $form['copy'] = array( '#type' => 'checkbox', '#title' => t('Send yourself a copy.'), diff --git a/core/modules/contact/src/Tests/ContactAuthenticatedUserTest.php b/core/modules/contact/src/Tests/ContactAuthenticatedUserTest.php index d343f8d33c90..d88ba5a474d2 100644 --- a/core/modules/contact/src/Tests/ContactAuthenticatedUserTest.php +++ b/core/modules/contact/src/Tests/ContactAuthenticatedUserTest.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\contact\ContactAuthenticatedUserTest. + * Contains \Drupal\contact\ContactAuthenticatedUserTest. */ namespace Drupal\contact\Tests; diff --git a/core/modules/contact/src/Tests/ContactPersonalTest.php b/core/modules/contact/src/Tests/ContactPersonalTest.php index 6f3e2f1faa34..eb033bd2c091 100644 --- a/core/modules/contact/src/Tests/ContactPersonalTest.php +++ b/core/modules/contact/src/Tests/ContactPersonalTest.php @@ -2,12 +2,13 @@ /** * @file - * Definition of Drupal\contact\Tests\ContactPersonalTest. + * Contains \Drupal\contact\Tests\ContactPersonalTest. */ namespace Drupal\contact\Tests; use Drupal\Component\Utility\String; +use Drupal\Core\Session\AccountInterface; use Drupal\simpletest\WebTestBase; /** @@ -73,13 +74,13 @@ function testSendPersonalContactMessage() { $this->assertEqual($mail['key'], 'user_mail'); $variables = array( '!site-name' => \Drupal::config('system.site')->get('name'), - '!subject' => $message['subject'], + '!subject' => $message['subject[0][value]'], '!recipient-name' => $this->contact_user->getUsername(), ); $this->assertEqual($mail['subject'], t('[!site-name] !subject', $variables), 'Subject is in sent message.'); $this->assertTrue(strpos($mail['body'], t('Hello !recipient-name,', $variables)) !== FALSE, 'Recipient name is in sent message.'); $this->assertTrue(strpos($mail['body'], $this->web_user->getUsername()) !== FALSE, 'Sender name is in sent message.'); - $this->assertTrue(strpos($mail['body'], $message['message']) !== FALSE, 'Message body is in sent message.'); + $this->assertTrue(strpos($mail['body'], $message['message[0][value]']) !== FALSE, 'Message body is in sent message.'); // Check there was no problems raised during sending. $this->drupalLogout(); @@ -268,18 +269,22 @@ protected function checkContactAccess($response, $contact_value = NULL) { /** * Fills out a user's personal contact form and submits it. * - * @param $account + * @param \Drupal\Core\Session\AccountInterface $account * A user object of the user being contacted. - * @param $message + * @param array $message * (optional) An array with the form fields being used. Defaults to an empty * array. + * + * @return array + * An array with the form fields being used. */ - protected function submitPersonalContact($account, array $message = array()) { + protected function submitPersonalContact(AccountInterface $account, array $message = array()) { $message += array( - 'subject' => $this->randomName(16), - 'message' => $this->randomName(64), + 'subject[0][value]' => $this->randomName(16), + 'message[0][value]' => $this->randomName(64), ); $this->drupalPostForm('user/' . $account->id() . '/contact', $message, t('Send message')); return $message; } + } diff --git a/core/modules/contact/src/Tests/ContactSitewideTest.php b/core/modules/contact/src/Tests/ContactSitewideTest.php index a89e7a8a04e3..2be42085d6e2 100644 --- a/core/modules/contact/src/Tests/ContactSitewideTest.php +++ b/core/modules/contact/src/Tests/ContactSitewideTest.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\contact\Tests\ContactSitewideTest. + * Contains \Drupal\contact\Tests\ContactSitewideTest. */ namespace Drupal\contact\Tests; @@ -260,14 +260,14 @@ function testSiteWideContact() { // Submit the contact form and verify the content. $edit = array( - 'subject' => $this->randomName(), - 'message' => $this->randomName(), + 'subject[0][value]' => $this->randomName(), + 'message[0][value]' => $this->randomName(), $field_name . '[0][value]' => $this->randomName(), ); $this->drupalPostForm(NULL, $edit, t('Send message')); $mails = $this->drupalGetMails(); $mail = array_pop($mails); - $this->assertEqual($mail['subject'], t('[@label] @subject', array('@label' => $label, '@subject' => $edit['subject']))); + $this->assertEqual($mail['subject'], t('[@label] @subject', array('@label' => $label, '@subject' => $edit['subject[0][value]']))); $this->assertTrue(strpos($mail['body'], $field_label)); $this->assertTrue(strpos($mail['body'], $edit[$field_name . '[0][value]'])); } @@ -384,8 +384,8 @@ function submitContact($name, $mail, $subject, $id, $message) { $edit = array(); $edit['name'] = $name; $edit['mail'] = $mail; - $edit['subject'] = $subject; - $edit['message'] = $message; + $edit['subject[0][value]'] = $subject; + $edit['message[0][value]'] = $message; if ($id == \Drupal::config('contact.settings')->get('default_category')) { $this->drupalPostForm('contact', $edit, t('Send message')); } diff --git a/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWidget.php b/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWidget.php index 3c57008fedfc..a8afeb3d3853 100644 --- a/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWidget.php +++ b/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWidget.php @@ -8,7 +8,7 @@ namespace Drupal\text\Plugin\Field\FieldWidget; use Drupal\Core\Field\FieldItemListInterface; -use Drupal\Core\Field\WidgetBase; +use Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextareaWidget; use Symfony\Component\Validator\ConstraintViolationInterface; /** @@ -22,76 +22,23 @@ * } * ) */ -class TextareaWidget extends WidgetBase { - - /** - * {@inheritdoc} - */ - public static function defaultSettings() { - return array( - 'rows' => '5', - 'placeholder' => '', - ) + parent::defaultSettings(); - } - - /** - * {@inheritdoc} - */ - public function settingsForm(array $form, array &$form_state) { - $element['rows'] = array( - '#type' => 'number', - '#title' => t('Rows'), - '#default_value' => $this->getSetting('rows'), - '#required' => TRUE, - '#min' => 1, - ); - $element['placeholder'] = array( - '#type' => 'textfield', - '#title' => t('Placeholder'), - '#default_value' => $this->getSetting('placeholder'), - '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), - ); - return $element; - } - - /** - * {@inheritdoc} - */ - public function settingsSummary() { - $summary = array(); - - $summary[] = t('Number of rows: !rows', array('!rows' => $this->getSetting('rows'))); - $placeholder = $this->getSetting('placeholder'); - if (!empty($placeholder)) { - $summary[] = t('Placeholder: @placeholder', array('@placeholder' => $placeholder)); - } - - return $summary; - } +class TextareaWidget extends StringTextareaWidget { /** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) { - $main_widget = $element + array( - '#type' => 'textarea', - '#default_value' => $items[$delta]->value, - '#rows' => $this->getSetting('rows'), - '#placeholder' => $this->getSetting('placeholder'), - '#attributes' => array('class' => array('text-full')), - ); + $main_widget = parent::formElement($items, $delta, $element, $form, $form_state); if ($this->getFieldSetting('text_processing')) { - $element = $main_widget; + $element = $main_widget['value']; $element['#type'] = 'text_format'; $element['#format'] = $items[$delta]->format; - $element['#base_type'] = $main_widget['#type']; - } - else { - $element['value'] = $main_widget; + $element['#base_type'] = $main_widget['value']['#type']; + return $element; } - return $element; + return $main_widget; } /** -- GitLab