Commit 57926bf5 authored by alexpott's avatar alexpott

Issue #2313757 by Berdir, Wim Leers, dawehner: Remove text_processing option...

Issue #2313757 by Berdir, Wim Leers, dawehner: Remove text_processing option from text fields, expose existing string field types as plain text in UI.
parent 55ef0e86
......@@ -130,7 +130,7 @@ entity_form_display.field.*:
type: entity_field_form_display_base
label: 'Entity form display default'
entity_form_display.field.string:
entity_form_display.field.string_textfield:
type: entity_field_form_display_base
label: 'Text field display format settings'
mapping:
......
......@@ -12,6 +12,7 @@
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
......@@ -19,10 +20,9 @@
*
* @FieldType(
* id = "string",
* label = @Translation("String"),
* description = @Translation("An entity field containing a string value."),
* no_ui = TRUE,
* default_widget = "string",
* label = @Translation("Text (plain)"),
* description = @Translation("A field containing a plain string value."),
* default_widget = "string_textfield",
* default_formatter = "string"
* )
*/
......@@ -95,4 +95,23 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
return $values;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = array();
$element['max_length'] = array(
'#type' => 'number',
'#title' => t('Maximum length'),
'#default_value' => $this->getSetting('max_length'),
'#required' => TRUE,
'#description' => t('The maximum length of the field in characters.'),
'#min' => 1,
'#disabled' => $has_data,
);
return $element;
}
}
......@@ -14,11 +14,10 @@
*
* @FieldType(
* id = "string_long",
* label = @Translation("Long string"),
* description = @Translation("An entity field containing a long string value."),
* label = @Translation("Text (plain, long)"),
* description = @Translation("A field containing a long string value."),
* default_widget = "string_textarea",
* default_formatter = "string",
* no_ui = TRUE
* )
*/
class StringLongItem extends StringItem {
......
......@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\Core\Field\Plugin\Field\FieldWidget\StringWidget.
* Contains \Drupal\Core\Field\Plugin\Field\FieldWidget\StringTextfieldWidget.
*/
namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
......@@ -12,17 +12,17 @@
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'string' widget.
* Plugin implementation of the 'string_textfield' widget.
*
* @FieldWidget(
* id = "string",
* label = @Translation("String field"),
* id = "string_textfield",
* label = @Translation("Textfield"),
* field_types = {
* "string"
* }
* )
*/
class StringWidget extends WidgetBase {
class StringTextfieldWidget extends WidgetBase {
/**
* {@inheritdoc}
......
......@@ -146,7 +146,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', array(
'type' => 'string',
'type' => 'string_textfield',
'weight' => -5,
));
......
......@@ -168,7 +168,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setTranslatable(TRUE)
->setRequired(TRUE)
->setDisplayOptions('form', array(
'type' => 'string',
'type' => 'string_textfield',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE);
......
......@@ -234,7 +234,6 @@ public function addBodyField($comment_type_id) {
'label' => 'Comment',
'entity_type' => 'comment',
'bundle' => $comment_type_id,
'settings' => array('text_processing' => 1),
'required' => TRUE,
));
$field_instance->save();
......
......@@ -230,7 +230,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setTranslatable(TRUE)
->setSetting('max_length', 64)
->setDisplayOptions('form', array(
'type' => 'string',
'type' => 'string_textfield',
// Default comment body field has weight 20.
'weight' => 10,
))
......
......@@ -114,24 +114,4 @@ function testCommentInstallAfterContentModule() {
$this->postComment($book_node, $this->randomMachineName(), $this->randomMachineName());
}
/**
* Tests that comment module works correctly with plain text format.
*/
function testCommentFormat() {
// Disable text processing for comments.
$this->drupalLogin($this->admin_user);
$edit = array('instance[settings][text_processing]' => 0);
$this->drupalPostForm('admin/structure/comment/manage/comment/fields/comment.comment.comment_body', $edit, t('Save settings'));
// Change formatter settings.
$this->drupalGet('admin/structure/comment/manage/comment/display');
$edit = array('fields[comment_body][type]' => 'text_trimmed', 'refresh_rows' => 'comment_body');
$commands = $this->drupalPostAjaxForm(NULL, $edit, array('op' => t('Refresh')));
$this->assertTrue($commands, 'Ajax commands returned');
// Post a comment without an explicit subject.
$this->drupalLogin($this->web_user);
$edit = array('comment_body[0][value]' => $this->randomMachineName(8));
$this->drupalPostForm('node/' . $this->node->id(), $edit, t('Save'));
}
}
......@@ -218,7 +218,7 @@ public function setCommentSubject($enabled) {
$form_display = entity_get_form_display('comment', 'comment', 'default');
if ($enabled) {
$form_display->setComponent('subject', array(
'type' => 'string',
'type' => 'string_textfield',
));
}
else {
......
......@@ -162,7 +162,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setRequired(TRUE)
->setSetting('max_length', 100)
->setDisplayOptions('form', array(
'type' => 'string',
'type' => 'string_textfield',
'weight' => -10,
))
->setDisplayConfigurable('form', TRUE);
......
......@@ -180,7 +180,7 @@ protected function setupTestFields() {
}
entity_create('field_storage_config', array(
'name' => $this->fieldName,
'type' => 'text',
'type' => 'string',
'entity_type' => $this->entityTypeId,
'cardinality' => 1,
'translatable' => TRUE,
......@@ -193,7 +193,7 @@ protected function setupTestFields() {
))->save();
entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
->setComponent($this->fieldName, array(
'type' => 'text_textfield',
'type' => 'string_textfield',
'weight' => 0,
))
->save();
......
......@@ -7,6 +7,7 @@
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
......@@ -396,7 +397,7 @@ function editor_entity_revision_delete(EntityInterface $entity) {
}
/**
* Records file usage of files referenced by processed text fields.
* Records file usage of files referenced by formatted text fields.
*
* Every referenced file that does not yet have the FILE_STATUS_PERMANENT state,
* will be given that state.
......@@ -419,7 +420,7 @@ function _editor_record_file_usage(array $uuids, EntityInterface $entity) {
}
/**
* Deletes file usage of files referenced by processed text fields.
* Deletes file usage of files referenced by formatted text fields.
*
* @param array $uuids
* An array of file entity UUIDs.
......@@ -440,7 +441,7 @@ function _editor_delete_file_usage(array $uuids, EntityInterface $entity, $count
}
/**
* Finds all files referenced (data-editor-file-uuid) by processed text fields.
* Finds all files referenced (data-editor-file-uuid) by formatted text fields.
*
* @param EntityInterface $entity
* An entity whose fields to analyze.
......@@ -451,32 +452,32 @@ function _editor_delete_file_usage(array $uuids, EntityInterface $entity, $count
function _editor_get_file_uuids_by_field(EntityInterface $entity) {
$uuids = array();
$processed_text_fields = _editor_get_processed_text_fields($entity);
foreach ($processed_text_fields as $processed_text_field) {
$text = $entity->get($processed_text_field)->value;
$uuids[$processed_text_field] = _editor_parse_file_uuids($text);
$formatted_text_fields = _editor_get_formatted_text_fields($entity);
foreach ($formatted_text_fields as $formatted_text_field) {
$text = $entity->get($formatted_text_field)->value;
$uuids[$formatted_text_field] = _editor_parse_file_uuids($text);
}
return $uuids;
}
/**
* Determines the text fields on an entity that have text processing enabled.
* Determines the formatted text fields on an entity.
*
* @param EntityInterface $entity
* @param ContentEntityInterface $entity
* An entity whose fields to analyze.
*
* @return array
* The names of the fields on this entity that have text processing enabled.
* The names of the fields on this entity that support formatted text.
*/
function _editor_get_processed_text_fields(ContentEntityInterface $entity) {
function _editor_get_formatted_text_fields(ContentEntityInterface $entity) {
$field_definitions = $entity->getFieldDefinitions();
if (empty($field_definitions)) {
return array();
}
// Only return fields that have text processing enabled.
return array_keys(array_filter($field_definitions, function ($definition) {
return $definition->getSetting('text_processing') === TRUE;
// Only return formatted text fields.
return array_keys(array_filter($field_definitions, function (FieldDefinitionInterface $definition) {
return in_array($definition->getType(), array('text', 'text_long', 'text_with_summary'), TRUE);
}));
}
......
/**
* @file
* Text editor-based in-place editor for processed text content in Drupal.
* Text editor-based in-place editor for formatted text content in Drupal.
*
* Depends on editor.module. Works with any (WYSIWYG) editor that implements the
* editor.js API, including the optional attachInlineEditor() and onChange()
......@@ -78,9 +78,9 @@
break;
case 'activating':
// When transformation filters have been been applied to the processed
// text of this field, then we'll need to load a re-processed version of
// it without the transformation filters.
// When transformation filters have been been applied to the formatted
// text of this field, then we'll need to load a re-formatted version
// of it without the transformation filters.
if (this.textFormatHasTransformations) {
var $textElement = this.$textElement;
this._getUntransformedText(function (untransformedText) {
......@@ -151,7 +151,7 @@
/**
* Loads untransformed text for this field.
*
* More accurately: it re-processes processed text to exclude transformation
* More accurately: it re-filters formatted text to exclude transformation
* filters used by the text format.
*
* @param Function callback
......
......@@ -11,7 +11,7 @@
use Drupal\quickedit\Ajax\BaseCommand;
/**
* AJAX command to rerender a processed text field without any transformation
* AJAX command to rerender a formatted text field without any transformation
* filters.
*/
class GetUntransformedTextCommand extends BaseCommand {
......
......@@ -29,15 +29,15 @@ class EditorController extends ControllerBase {
/**
* Returns an Ajax response to render a text field without transformation filters.
*
* @param int $entity
* The entity of which a processed text field is being rerendered.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity of which a formatted text field is being rerendered.
* @param string $field_name
* The name of the (processed text) field that that is being rerendered
* The name of the (formatted text) field that that is being rerendered
* @param string $langcode
* The name of the language for which the processed text field is being
* rererendered.
* The name of the language for which the formatted text field is being
* rerendered.
* @param string $view_mode_id
* The view mode the processed text field should be rerendered in.
* The view mode the formatted text field should be rerendered in.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* The Ajax response.
......
......@@ -32,10 +32,10 @@ public function isCompatible(FieldItemListInterface $items) {
if ($field_definition->getFieldStorageDefinition()->getCardinality() != 1) {
return FALSE;
}
// This editor is compatible with processed ("rich") text fields; but only
// This editor is compatible with formatted ("rich") text fields; but only
// if there is a currently active text format, that text format has an
// associated editor and that editor supports inline editing.
elseif ($field_definition->getSetting('text_processing')) {
elseif (in_array($field_definition->getType(), array('text', 'text_long', 'text_with_summary'), TRUE)) {
if ($editor = editor_load($items[0]->format)) {
$definition = \Drupal::service('plugin.manager.editor')->getDefinition($editor->getEditor());
if ($definition['supports_inline_editing'] === TRUE) {
......
......@@ -75,7 +75,7 @@ protected function setUp() {
$this->createFieldWithInstance(
$this->field_name, 'text', 1, 'Long text field',
// Instance settings.
array('text_processing' => 1),
array(),
// Widget type & settings.
'text_textarea',
array('size' => 42),
......@@ -122,7 +122,7 @@ protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'defa
/**
* Tests editor selection when the Editor module is present.
*
* Tests a textual field, with text processing, with cardinality 1 and >1,
* Tests a textual field, with text filtering, with cardinality 1 and >1,
* always with a ProcessedTextEditor plug-in present, but with varying text
* format compatibility.
*/
......
......@@ -69,9 +69,6 @@ protected function setUp() {
'bundle' => $this->bundle,
'field_name' => 'body',
'label' => 'Body',
'settings' => array(
'text_processing' => TRUE,
),
))->save();
entity_get_display($this->entityType, $this->bundle, 'default')
->setComponent('body', array(
......
<?php
/**
* @file
* Contains \Drupal\field\Tests\String\StringFormatterTest.
*/
namespace Drupal\field\Tests\String;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldInstanceConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\KernelTestBase;
/**
* Tests the creation of text fields.
*
* @group field
*/
class StringFormatterTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity', 'field', 'text', 'entity_test', 'system', 'filter', 'user');
/**
* @var string
*/
protected $entityType;
/**
* @var string
*/
protected $bundle;
/**
* @var string
*/
protected $fieldName;
/**
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
*/
protected $display;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Configure the theme system.
$this->installConfig(array('system', 'field'));
$this->installEntitySchema('entity_test');
$this->entityType = 'entity_test';
$this->bundle = $this->entityType;
$this->fieldName = Unicode::strtolower($this->randomMachineName());
$field_storage = FieldStorageConfig::create(array(
'name' => $this->fieldName,
'entity_type' => $this->entityType,
'type' => 'string',
));
$field_storage->save();
$instance = FieldInstanceConfig::create(array(
'field_storage' => $field_storage,
'bundle' => $this->bundle,
'label' => $this->randomMachineName(),
));
$instance->save();
$this->display = entity_get_display($this->entityType, $this->bundle, 'default')
->setComponent($this->fieldName, array(
'type' => 'string',
'settings' => array(),
));
$this->display->save();
}
/**
* Renders fields of a given entity with a given display.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity object with attached fields to render.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display to render the fields in.
*
* @return string
* The rendered entity fields.
*/
protected function renderEntityFields(ContentEntityInterface $entity, EntityViewDisplayInterface $display) {
$content = $display->build($entity);
$content = $this->render($content);
return $content;
}
/**
* Tests string formatter output.
*/
public function testStringFormatter() {
$value = $this->randomString();
$value .= "\n\n<strong>" . $this->randomString() . '</strong>';
$value .= "\n\n" . $this->randomString();
$entity = EntityTest::create(array());
$entity->{$this->fieldName}->value = $value;
// Verify that all HTML is escaped and newlines are retained.
$this->renderEntityFields($entity, $this->display);
$this->assertNoRaw($value);
$this->assertRaw(nl2br(String::checkPlain($value)));
// Verify the cache tags.
$build = $entity->{$this->fieldName}->view();
$this->assertTrue(!isset($build[0]['#cache']), format_string('The string formatter has no cache tags.'));
}
}
......@@ -61,7 +61,7 @@ protected function setUp() {
ViewTestData::createTestViews(get_class($this), array('field_test_views'));
}
function setUpFields($amount = 3) {
function setUpFields($amount = 3, $type = 'string') {
// Create three fields.
$field_names = array();
for ($i = 0; $i < $amount; $i++) {
......@@ -69,7 +69,7 @@ function setUpFields($amount = 3) {
$this->fieldStorages[$i] = entity_create('field_storage_config', array(
'name' => $field_names[$i],
'entity_type' => 'node',
'type' => 'text',
'type' => $type,
));
$this->fieldStorages[$i]->save();
}
......
......@@ -47,7 +47,7 @@ protected function setUp() {
$this->account = $this->drupalCreateUser(array('administer views'));
$this->drupalLogin($this->account);
$this->setUpFields();
$this->setUpFields(1, 'text');
$this->setUpInstances();
}
......@@ -65,7 +65,7 @@ public function testHandlerUI() {
}, $result);
// @todo Replace this sort by assertArray once it's in.
sort($options, SORT_STRING);
$this->assertEqual($options, array('string', 'text_default', 'text_trimmed'), 'The text formatters for a simple text field appear as expected.');
$this->assertEqual($options, array('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'));
......
......@@ -45,7 +45,7 @@ protected function setUp() {
$this->fieldStorages[3] = entity_create('field_storage_config', array(
'name' => 'field_name_3',
'entity_type' => 'node',
'type' => 'text',
'type' => 'string',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
));
$this->fieldStorages[3]->save();
......@@ -53,11 +53,19 @@ protected function setUp() {
$this->fieldStorages[4] = entity_create('field_storage_config', array(
'name' => 'field_name_4',
'entity_type' => 'node',
'type' => 'text',
'type' => 'string',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
));
$this->fieldStorages[4]->save();
// Setup a text field.
$this->fieldStorages[5] = entity_create('field_storage_config', array(
'name' => 'field_name_5',
'entity_type' => 'node',
'type' => 'text',
));
$this->fieldStorages[5]->save();
$this->setUpInstances();
// Create some nodes.
......@@ -65,7 +73,7 @@ protected function setUp() {
for ($i = 0; $i < 3; $i++) {
$edit = array('type' => 'page');
for ($key = 0; $key < 3; $key++) {
foreach (array(0, 1, 2, 5) as $key) {
$field_storage = $this->fieldStorages[$key];
$edit[$field_storage->getName()][0]['value'] = $this->randomMachineName(8);
}
......@@ -125,8 +133,8 @@ public function _testSimpleFieldRender() {
public function _testFormatterSimpleFieldRender() {
$view = Views::getView('test_view_fieldapi');
$this->prepareView($view);
$view->displayHandlers->get('default')->options['fields'][$this->fieldStorages[0]->getName()]['type'] = 'text_trimmed';
$view->displayHandlers->get('default')->options['fields'][$this->fieldStorages[0]->getName()]['settings'] = array(
$view->displayHandlers->get('default')->options['fields'][$this->fieldStorages[5]->getName()]['type'] = 'text_trimmed';
$view->displayHandlers->get('default')->options['fields'][$this->fieldStorages[5]->getName()]['settings'] = array(
'trim_length' => 3,
);
$this->executeView($view);
......@@ -134,8 +142,8 @@ public function _testFormatterSimpleFieldRender() {
// Make sure that the formatter works as expected.
// @TODO: actually there should be a specific formatter.
for ($i = 0; $i < 2; $i++) {
$rendered_field = $view->style_plugin->getField($i, $this->fieldStorages[0]->getName());
$this->assertEqual(strlen($rendered_field), 3);
$rendered_field = $view->style_plugin->getField($i, $this->fieldStorages[5]->getName());
$this->assertEqual(strlen(html_entity_decode($rendered_field)), 3);
}
}
......
......@@ -8,8 +8,7 @@ description: ''
required: false
default_value: { }
default_value_function: ''
settings:
text_processing: 0
settings: { }
field_type: text
dependencies:
entity:
......
......@@ -8,8 +8,7 @@ description: ''
required: false
default_value: { }
default_value_function: ''
settings:
text_processing: 0
settings: { }
field_type: text
dependencies:
entity:
......
......@@ -8,8 +8,7 @@ description: ''
required: false
default_value: { }
default_value_function: ''
settings:
text_processing: 0
settings: { }
field_type: text
dependencies:
entity:
......
......@@ -9,8 +9,7 @@ description: ''
required: '0'
default_value: { }
default_value_function: ''
settings:
text_processing: '0'
settings: { }
field_type: text
dependencies:
entity:
......
......@@ -9,8 +9,7 @@ description: ''
required: '0'
default_value: { }
default_value_function: ''
settings:
text_processing: '0'
settings: { }
field_type: text
dependencies:
entity:
......
......@@ -9,8 +9,7 @@ description: ''
required: '0'
default_value: { }
default_value_function: ''
settings:
text_processing: '0'
settings: { }
field_type: text
dependencies:
entity:
......