Commit 158863a0 authored by alexpott's avatar alexpott

Issue #1856562 by andypost | sun: Convert "Subject" and "Message" into Message base fields.

parent 686b47cd
......@@ -19,6 +19,7 @@
* label = @Translation("Plain text"),
* field_types = {
* "string",
* "string_long",
* "email"
* },
* quickedit = {
......
......@@ -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
* )
*/
......
<?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;
}
}
......@@ -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'));
}
......
......@@ -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(
......
......@@ -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;
}
......
......@@ -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.'),
......
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\contact\ContactAuthenticatedUserTest.
* Contains \Drupal\contact\ContactAuthenticatedUserTest.
*/
namespace Drupal\contact\Tests;
......
......@@ -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;
}
}
......@@ -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'));
}
......
......@@ -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;
}
/**
......
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