Commit 2187fd1e authored by alexpott's avatar alexpott

Issue #2198917 by andypost, Berdir: Use the string field type for the node title field.

parent 3f9cd457
......@@ -2,31 +2,31 @@
/**
* @file
* Contains \Drupal\text\Plugin\field\formatter\TextPlainFormatter.
* Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter.
*/
namespace Drupal\text\Plugin\Field\FieldFormatter;
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\String;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
/**
* Plugin implementation of the 'text_plain' formatter.
* Plugin implementation of the 'string' formatter.
*
* @FieldFormatter(
* id = "text_plain",
* id = "string",
* label = @Translation("Plain text"),
* field_types = {
* "text",
* "text_long",
* "text_with_summary"
* "string",
* "email"
* },
* edit = {
* "editor" = "plain_text"
* }
* )
*/
class TextPlainFormatter extends FormatterBase {
class StringFormatter extends FormatterBase {
/**
* {@inheritdoc}
......@@ -37,7 +37,7 @@ public function viewElements(FieldItemListInterface $items) {
foreach ($items as $delta => $item) {
// The text value has no text format assigned to it, so the user input
// should equal the output, including newlines.
$elements[$delta] = array('#markup' => nl2br(check_plain($item->value)));
$elements[$delta] = array('#markup' => nl2br(String::checkPlain($item->value)));
}
return $elements;
......
......@@ -18,7 +18,9 @@
* id = "email",
* label = @Translation("E-mail"),
* description = @Translation("An entity field containing an e-mail value."),
* configurable = FALSE
* configurable = FALSE,
* default_widget = "string",
* default_formatter = "string"
* )
*/
class EmailItem extends FieldItemBase {
......
......@@ -21,7 +21,9 @@
* settings = {
* "max_length" = "255"
* },
* configurable = FALSE
* configurable = FALSE,
* default_widget = "string",
* default_formatter = "string"
* )
*/
class StringItem extends FieldItemBase {
......@@ -60,7 +62,12 @@ public function getConstraints() {
if ($max_length = $this->getSetting('max_length')) {
$constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
$constraints[] = $constraint_manager->create('ComplexData', array(
'value' => array('Length' => array('max' => $max_length))
'value' => array(
'Length' => array(
'max' => $max_length,
'maxMessage' => t('%name: may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length)),
),
),
));
}
......
<?php
/**
* @file
* Contains \Drupal\Core\Field\Plugin\Field\FieldWidget\StringWidget.
*/
namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
/**
* Plugin implementation of the 'string' widget.
*
* @FieldWidget(
* id = "string",
* label = @Translation("String field"),
* field_types = {
* "string",
* "email"
* },
* settings = {
* "size" = "60",
* "placeholder" = ""
* }
* )
*/
class StringWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state) {
$element['size'] = array(
'#type' => 'number',
'#title' => t('Size of textfield'),
'#default_value' => $this->getSetting('size'),
'#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('Textfield size: !size', array('!size' => $this->getSetting('size')));
$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' => 'textfield',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
'#size' => $this->getSetting('size'),
'#placeholder' => $this->getSetting('placeholder'),
'#maxlength' => $this->getFieldSetting('max_length'),
'#attributes' => array('class' => array('text-full')),
);
return $element;
}
}
......@@ -136,7 +136,8 @@ protected function assertLengthViolation(CommentInterface $comment, $field_name,
$violations = $comment->validate();
$this->assertEqual(count($violations), 1, "Violation found when $field_name is too long.");
$this->assertEqual($violations[0]->getPropertyPath(), "$field_name.0.value");
$this->assertEqual($violations[0]->getMessage(), t('This value is too long. It should have %limit characters or less.', array('%limit' => $length)));
$field_label = $comment->get($field_name)->getFieldDefinition()->getLabel();
$this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', array('%name' => $field_label, '@max' => $length)));
}
}
......@@ -35,20 +35,5 @@ function email_field_info_alter(&$info) {
$info['email']['class'] = '\Drupal\email\ConfigurableEmailItem';
$info['email']['list_class'] = '\Drupal\Core\Field\ConfigFieldItemList';
$info['email']['default_widget'] = 'email_default';
if (\Drupal::moduleHandler()->moduleExists('text')) {
$info['email']['default_formatter'] = 'text_plain';
}
else {
$info['email']['default_formatter'] = 'email_mailto';
}
$info['email']['provider'] = 'email';
}
/**
* Implements hook_field_formatter_info_alter().
*/
function email_field_formatter_info_alter(&$info) {
if (isset($info['text_plain'])) {
$info['text_plain']['field_types'][] = 'email';
}
}
......@@ -139,3 +139,13 @@ entity_form_display.field.*:
weight:
type: integer
label: 'Weight'
entity_view_display.field.string:
type: entity_field_view_display_base
label: 'Plain text display format settings'
mapping:
settings:
type: sequence
label: 'Settings'
sequence:
- type: string
......@@ -73,7 +73,7 @@ public function testHandlerUI() {
}, $result);
// @todo Replace this sort by assertArray once it's in.
sort($options, SORT_STRING);
$this->assertEqual($options, array('text_default', 'text_plain', 'text_trimmed'), 'The text formatters for a simple text field appear as expected.');
$this->assertEqual($options, array('string', 'text_default', 'text_trimmed'), 'The text formatters for a simple text field appear as expected.');
$this->drupalPostForm(NULL, array('options[type]' => 'text_trimmed'), t('Apply'));
......
......@@ -63,7 +63,7 @@ abstract class DisplayOverviewBase extends OverviewBase {
public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager, ConfigFactoryInterface $config_factory) {
parent::__construct($entity_manager);
$this->fieldTypes = $field_type_manager->getConfigurableDefinitions();
$this->fieldTypes = $field_type_manager->getDefinitions();
$this->pluginManager = $plugin_manager;
$this->configFactory = $config_factory;
}
......
......@@ -378,7 +378,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Language code'))
->setDescription(t('The node language code.'));
$fields['title'] = FieldDefinition::create('text')
$fields['title'] = FieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The title of this node, always treated as non-markup plain text.'))
->setRequired(TRUE)
......@@ -386,15 +386,14 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setSettings(array(
'default_value' => '',
'max_length' => 255,
'text_processing' => 0,
))
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'text_default',
'type' => 'string',
'weight' => -5,
))
->setDisplayOptions('form', array(
'type' => 'text_textfield',
'type' => 'string',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE);
......
......@@ -54,7 +54,7 @@ public function testValidation() {
$violations = $node->validate();
$this->assertEqual(count($violations), 1, 'Violation found when title is too long.');
$this->assertEqual($violations[0]->getPropertyPath(), 'title.0.value');
$this->assertEqual($violations[0]->getMessage(), '<em class="placeholder">Title</em>: the text may not be longer than 255 characters.');
$this->assertEqual($violations[0]->getMessage(), '<em class="placeholder">Title</em>: may not be longer than 255 characters.');
$node->set('title', NULL);
$violations = $node->validate();
......
......@@ -73,7 +73,7 @@ public function testDefaultFormatter() {
* Tests the plain formatter.
*/
public function testPlainFormatter() {
$this->assertFormatterRdfa('text_plain', 'http://schema.org/text', $this->testValue);
$this->assertFormatterRdfa('string', 'http://schema.org/text', $this->testValue);
}
/**
......
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\rest\test\CreateTest.
* Contains \Drupal\rest\test\CreateTest.
*/
namespace Drupal\rest\Tests;
......@@ -106,7 +106,7 @@ public function testCreate() {
$response = $this->httpRequest('entity/' . $entity_type, 'POST', $invalid_serialized, $this->defaultMimeType);
$this->assertResponse(422);
$error = drupal_json_decode($response);
$this->assertEqual($error['error'], "Unprocessable Entity: validation failed.\nuuid.0.value: This value is too long. It should have <em class=\"placeholder\">128</em> characters or less.\n");
$this->assertEqual($error['error'], "Unprocessable Entity: validation failed.\nuuid.0.value: <em class=\"placeholder\">UUID</em>: may not be longer than 128 characters.\n");
// Try to create an entity without proper permissions.
$this->drupalLogout();
......@@ -125,4 +125,5 @@ public function testCreate() {
// @todo Add a security test. It should not be possible for example to
// create a test entity on a node resource route.
}
}
......@@ -2,7 +2,7 @@
/**
* @file
* Contains Drupal\rest\test\UpdateTest.
* Contains \Drupal\rest\test\UpdateTest.
*/
namespace Drupal\rest\Tests;
......@@ -149,7 +149,7 @@ public function testPatchUpdate() {
$response = $this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'PATCH', $invalid_serialized, $this->defaultMimeType);
$this->assertResponse(422);
$error = drupal_json_decode($response);
$this->assertEqual($error['error'], "Unprocessable Entity: validation failed.\nuuid.0.value: This value is too long. It should have <em class=\"placeholder\">128</em> characters or less.\n");
$this->assertEqual($error['error'], "Unprocessable Entity: validation failed.\nuuid.0.value: <em class=\"placeholder\">UUID</em>: may not be longer than 128 characters.\n");
// Try to update an entity without proper permissions.
$this->drupalLogout();
......@@ -162,4 +162,5 @@ public function testPatchUpdate() {
$this->httpRequest('entity/' . $entity_type . '/' . $entity->id(), 'PATCH', $serialized, $this->defaultMimeType);
$this->assertResponse(404);
}
}
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\system\Tests\TypedData\EntityTypedDataDefinitionTest.
* Contains \Drupal\system\Tests\Entity\EntityTypedDataDefinitionTest.
*/
namespace Drupal\system\Tests\Entity;
......@@ -101,7 +101,7 @@ public function testEntities() {
// Comparison should ignore the internal static cache, so compare the
// serialized objects instead.
$this->assertEqual(serialize($field_definitions), serialize(\Drupal::entityManager()->getBaseFieldDefinitions('node')));
$this->assertEqual($entity_definition->getPropertyDefinition('title')->getItemDefinition()->getDataType(), 'field_item:text');
$this->assertEqual($entity_definition->getPropertyDefinition('title')->getItemDefinition()->getDataType(), 'field_item:string');
$this->assertNull($entity_definition->getMainPropertyName());
$this->assertNull($entity_definition->getPropertyDefinition('invalid'));
......
......@@ -108,7 +108,7 @@ protected function checkValidation($entity_type) {
$test_entity->uuid->value = $this->randomString(129);
$violations = $test_entity->validate();
$this->assertEqual($violations->count(), 1, 'Validation failed.');
$this->assertEqual($violations[0]->getMessage(), t('This value is too long. It should have %limit characters or less.', array('%limit' => '128')));
$this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', array('%name' => 'UUID', '@max' => 128)));
$test_entity = clone $entity;
$test_entity->langcode->value = $this->randomString(13);
......@@ -126,7 +126,7 @@ protected function checkValidation($entity_type) {
$test_entity->name->value = $this->randomString(33);
$violations = $test_entity->validate();
$this->assertEqual($violations->count(), 1, 'Validation failed.');
$this->assertEqual($violations[0]->getMessage(), t('This value is too long. It should have %limit characters or less.', array('%limit' => '32')));
$this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', array('%name' => 'Name', '@max' => 32)));
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
......
......@@ -56,7 +56,8 @@ public function testValidation() {
$violations = $term->validate();
$this->assertEqual(count($violations), 1, 'Violation found when name is too long.');
$this->assertEqual($violations[0]->getPropertyPath(), 'name.0.value');
$this->assertEqual($violations[0]->getMessage(), t('This value is too long. It should have %limit characters or less.', array('%limit' => 255)));
$field_label = $term->get('name')->getFieldDefinition()->getLabel();
$this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', array('%name' => $field_label, '@max' => 255)));
$term->set('name', NULL);
$violations = $term->validate();
......
......@@ -19,7 +19,7 @@
* label = @Translation("Telephone number"),
* description = @Translation("This field stores a telephone number in the database."),
* default_widget = "telephone_default",
* default_formatter = "telephone_link"
* default_formatter = "string"
* )
*/
class TelephoneItem extends ConfigFieldItemBase {
......
......@@ -25,21 +25,9 @@ function telephone_help($path, $arg) {
}
}
/**
* Implements hook_field_info_alter().
*/
function telephone_field_info_alter(&$info) {
if (\Drupal::moduleHandler()->moduleExists('text')) {
$info['telephone']['default_formatter'] = 'text_plain';
}
}
/**
* Implements hook_field_formatter_info_alter().
*/
function telephone_field_formatter_info_alter(&$info) {
if (isset($info['text_plain'])) {
$info['text_plain']['field_types'][] = 'telephone';
}
$info['string']['field_types'][] = 'telephone';
}
......@@ -110,16 +110,6 @@ entity_view_display.field.text_default:
sequence:
- type: string
entity_view_display.field.text_plain:
type: entity_field_view_display_base
label: 'Plain text display format settings'
mapping:
settings:
type: sequence
label: 'Settings'
sequence:
- type: string
entity_view_display.field.text_summary_or_trimmed:
type: entity_field_view_display_base
label: 'Summary or trimmed text display format settings'
......
......@@ -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\StringWidget;
use Symfony\Component\Validator\ConstraintViolationInterface;
/**
......@@ -18,7 +18,8 @@
* id = "text_textfield",
* label = @Translation("Text field"),
* field_types = {
* "text"
* "text",
* "string"
* },
* settings = {
* "size" = "60",
......@@ -26,67 +27,22 @@
* }
* )
*/
class TextfieldWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state) {
$element['size'] = array(
'#type' => 'number',
'#title' => t('Size of textfield'),
'#default_value' => $this->getSetting('size'),
'#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('Textfield size: !size', array('!size' => $this->getSetting('size')));
$placeholder = $this->getSetting('placeholder');
if (!empty($placeholder)) {
$summary[] = t('Placeholder: @placeholder', array('@placeholder' => $placeholder));
}
return $summary;
}
class TextfieldWidget extends StringWidget {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
$main_widget = $element + array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
'#size' => $this->getSetting('size'),
'#placeholder' => $this->getSetting('placeholder'),
'#maxlength' => $this->getFieldSetting('max_length'),
'#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'] = isset($items[$delta]->format) ? $items[$delta]->format : NULL;
$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;
}
/**
......
......@@ -2,11 +2,12 @@
/**
* @file
* Definition of Drupal\text\TextProcessed.
* Contains \Drupal\text\TextProcessed.
*/
namespace Drupal\text;
use Drupal\Component\Utility\String;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TypedData;
......@@ -57,8 +58,8 @@ public function getValue($langcode = NULL) {
}
else {
// Escape all HTML and retain newlines.
// @see \Drupal\text\Plugin\field\formatter\TextPlainFormatter
$this->processed = nl2br(check_plain($text));
// @see \Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter
$this->processed = nl2br(String::checkPlain($text));
}
return $this->processed;
}
......
......@@ -170,3 +170,12 @@ function text_filter_format_update($format) {
function text_filter_format_disable($format) {
field_cache_clear();
}
/**
* Implements hook_field_formatter_info_alter().
*/
function text_field_formatter_info_alter(&$info) {
$info['string']['field_types'][] = 'text';
$info['string']['field_types'][] = 'text_with_summary';
$info['string']['field_types'][] = 'text_long';
}
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\user\Tests\UserValidationTest.
* Contains \Drupal\user\Tests\UserValidationTest.
*/
namespace Drupal\user\Tests;
......@@ -160,6 +160,8 @@ protected function assertLengthViolation(EntityInterface $entity, $field_name, $
$violations = $entity->validate();
$this->assertEqual(count($violations), 1, "Violation found when $field_name is too long.");
$this->assertEqual($violations[0]->getPropertyPath(), "$field_name.0.value");
$this->assertEqual($violations[0]->getMessage(), t('This value is too long. It should have %limit characters or less.', array('%limit' => $length)));
$field_label = $entity->get($field_name)->getFieldDefinition()->getLabel();
$this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', array('%name' => $field_label, '@max' => $length)));
}
}
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