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 d5e0c17..19f1300 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" * }, * edit = { 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 63964de..93269f8 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/modules/text/lib/Drupal/text/Plugin/Field/FieldWidget/TextareaWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php similarity index 62% copy from core/modules/text/lib/Drupal/text/Plugin/Field/FieldWidget/TextareaWidget.php copy to core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php index 3c57008..fae98b1 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/Field/FieldWidget/TextareaWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/StringTextareaWidget.php @@ -2,27 +2,26 @@ /** * @file - * Contains \Drupal\text\Plugin\Field\FieldWidget\TextareaWidget. + * Contains \Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextareaWidget. */ -namespace Drupal\text\Plugin\Field\FieldWidget; +namespace Drupal\Core\Field\Plugin\Field\FieldWidget; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; -use Symfony\Component\Validator\ConstraintViolationInterface; /** - * Plugin implementation of the 'text_textarea' widget. + * Plugin implementation of the 'string_textarea' widget. * * @FieldWidget( - * id = "text_textarea", + * id = "string_textarea", * label = @Translation("Text area (multiple rows)"), * field_types = { - * "text_long" + * "string_long" * } * ) */ -class TextareaWidget extends WidgetBase { +class StringTextareaWidget extends WidgetBase { /** * {@inheritdoc} @@ -73,7 +72,7 @@ public function settingsSummary() { * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) { - $main_widget = $element + array( + $element['value'] = $element + array( '#type' => 'textarea', '#default_value' => $items[$delta]->value, '#rows' => $this->getSetting('rows'), @@ -81,28 +80,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#attributes' => array('class' => array('text-full')), ); - if ($this->getFieldSetting('text_processing')) { - $element = $main_widget; - $element['#type'] = 'text_format'; - $element['#format'] = $items[$delta]->format; - $element['#base_type'] = $main_widget['#type']; - } - else { - $element['value'] = $main_widget; - } - - return $element; - } - - /** - * {@inheritdoc} - */ - public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, array &$form_state) { - if ($violation->arrayPropertyPath == array('format') && isset($element['format']['#access']) && !$element['format']['#access']) { - // Ignore validation errors for formats if formats may not be changed, - // i.e. when existing formats become invalid. See filter_process_format(). - return FALSE; - } return $element; } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationUiTest.php b/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationUiTest.php index 405aaf5..16b3718 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationUiTest.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/Tests/ConfigTranslationUiTest.php @@ -299,8 +299,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 17db732..813f149 100644 --- a/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -94,27 +94,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/lib/Drupal/contact/Entity/Message.php b/core/modules/contact/lib/Drupal/contact/Entity/Message.php index ff35420..e504674 100644 --- a/core/modules/contact/lib/Drupal/contact/Entity/Message.php +++ b/core/modules/contact/lib/Drupal/contact/Entity/Message.php @@ -155,13 +155,37 @@ 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) + ->setSettings(array( + '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')) diff --git a/core/modules/contact/lib/Drupal/contact/MessageFormController.php b/core/modules/contact/lib/Drupal/contact/MessageFormController.php index 122665f..fea027f 100644 --- a/core/modules/contact/lib/Drupal/contact/MessageFormController.php +++ b/core/modules/contact/lib/Drupal/contact/MessageFormController.php @@ -83,19 +83,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/lib/Drupal/contact/Tests/ContactAuthenticatedUserTest.php b/core/modules/contact/lib/Drupal/contact/Tests/ContactAuthenticatedUserTest.php index 059923f..cd8e39d 100644 --- a/core/modules/contact/lib/Drupal/contact/Tests/ContactAuthenticatedUserTest.php +++ b/core/modules/contact/lib/Drupal/contact/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/lib/Drupal/contact/Tests/ContactPersonalTest.php b/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php index c7056a7..91aaace 100644 --- a/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php +++ b/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php @@ -2,11 +2,12 @@ /** * @file - * Definition of Drupal\contact\Tests\ContactPersonalTest. + * Contains \Drupal\contact\Tests\ContactPersonalTest. */ namespace Drupal\contact\Tests; +use Drupal\Core\Session\AccountInterface; use Drupal\simpletest\WebTestBase; /** @@ -78,13 +79,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.'); } /** @@ -212,18 +213,22 @@ function testPersonalContactFlood() { /** * 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/lib/Drupal/contact/Tests/ContactSitewideTest.php b/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php index 126fc75..9d23432 100644 --- a/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php +++ b/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\contact\Tests\ContactSitewideTest. + * Contains \Drupal\contact\Tests\ContactSitewideTest. */ namespace Drupal\contact\Tests; @@ -257,14 +257,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]'])); } @@ -381,8 +381,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/lib/Drupal/text/Plugin/Field/FieldWidget/TextareaWidget.php b/core/modules/text/lib/Drupal/text/Plugin/Field/FieldWidget/TextareaWidget.php index 3c57008..a8afeb3 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/Field/FieldWidget/TextareaWidget.php +++ b/core/modules/text/lib/Drupal/text/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; } /**