Commit 7cea08f1 authored by webchick's avatar webchick

Issue #1704864 by nick_schuch, Stalski, larowlan, kostyashupenko, andymartha,...

Issue #1704864 by nick_schuch, Stalski, larowlan, kostyashupenko, andymartha, xjm, webchick, zuuperman, tim.plunkett, catch, alexverb, irawan, ling-drupal: Add a "Required" and "Show by default" option for "Text area with a summary" field/widget
parent 90534ace
......@@ -19,6 +19,7 @@ content:
rows: 9
summary_rows: 3
placeholder: ''
show_summary: false
third_party_settings: { }
created:
type: datetime_timestamp
......
......@@ -18,4 +18,5 @@ default_value: { }
default_value_callback: ''
settings:
display_summary: true
required_summary: false
field_type: text_with_summary
......@@ -22,6 +22,7 @@ content:
rows: 9
summary_rows: 3
placeholder: ''
show_summary: false
third_party_settings: { }
comment_forum:
type: comment_default
......
......@@ -18,4 +18,5 @@ default_value: { }
default_value_callback: ''
settings:
display_summary: true
required_summary: false
field_type: text_with_summary
......@@ -56,6 +56,7 @@ content:
rows: 9
summary_rows: 3
placeholder: ''
show_summary: false
third_party_settings: { }
hidden: { }
third_party_settings: { }
......@@ -61,6 +61,9 @@ field.field_settings.text_with_summary:
display_summary:
type: boolean
label: 'Summary input'
required_summary:
type: boolean
label: 'Require summary'
field.value.text_with_summary:
type: mapping
......@@ -120,6 +123,9 @@ field.widget.settings.text_textarea_with_summary:
placeholder:
type: label
label: 'Placeholder'
show_summary:
type: boolean
label: 'Always show summary'
field.widget.settings.text_textfield:
type: mapping
......
......@@ -26,6 +26,7 @@ class TextWithSummaryItem extends TextItemBase {
public static function defaultFieldSettings() {
return [
'display_summary' => 0,
'required_summary' => FALSE,
] + parent::defaultFieldSettings();
}
......@@ -95,7 +96,32 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
'#description' => t('This allows authors to input an explicit summary, to be displayed instead of the automatically trimmed text when using the "Summary or trimmed" display type.'),
];
$element['required_summary'] = [
'#type' => 'checkbox',
'#title' => t('Require summary'),
'#description' => t('The summary will also be visible when marked as required.'),
'#default_value' => $settings['required_summary'],
];
return $element;
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraints = parent::getConstraints();
if ($this->getSetting('required_summary')) {
$manager = $this->getTypedDataManager()->getValidationConstraintManager();
$constraints[] = $manager->create('ComplexData', [
'summary' => [
'NotNull' => [
'message' => $this->t('The summary field is required for @name', ['@name' => $this->getFieldDefinition()->getLabel()]),
],
],
]);
}
return $constraints;
}
}
......@@ -27,6 +27,7 @@ public static function defaultSettings() {
'rows' => '9',
'summary_rows' => '3',
'placeholder' => '',
'show_summary' => FALSE,
] + parent::defaultSettings();
}
......@@ -43,6 +44,11 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
'#required' => TRUE,
'#min' => 1,
];
$element['show_summary'] = [
'#type' => 'checkbox',
'#title' => t('Always show the summary field'),
'#default_value' => $this->getSetting('show_summary'),
];
return $element;
}
......@@ -53,6 +59,9 @@ public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = t('Number of summary rows: @rows', ['@rows' => $this->getSetting('summary_rows')]);
if ($this->getSetting('show_summary')) {
$summary[] = t('Summary field will always be visible');
}
return $summary;
}
......@@ -64,21 +73,25 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$display_summary = $items[$delta]->summary || $this->getFieldSetting('display_summary');
$required = empty($form['#type']) && $this->getFieldSetting('required_summary');
$element['summary'] = [
'#type' => $display_summary ? 'textarea' : 'value',
'#default_value' => $items[$delta]->summary,
'#title' => t('Summary'),
'#rows' => $this->getSetting('summary_rows'),
'#description' => t('Leave blank to use trimmed value of full text as the summary.'),
'#attached' => [
'library' => ['text/drupal.text'],
],
'#description' => !$required ? $this->t('Leave blank to use trimmed value of full text as the summary.') : '',
'#attributes' => ['class' => ['js-text-summary', 'text-summary']],
'#prefix' => '<div class="js-text-summary-wrapper text-summary-wrapper">',
'#suffix' => '</div>',
'#weight' => -10,
'#required' => $required,
];
if (!$this->getSetting('show_summary') && !$required) {
$element['summary']['#attached']['library'][] = 'text/drupal.text';
}
return $element;
}
......
<?php
namespace Drupal\Tests\text\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
/**
* Tests updates for adding required summary flags to widgets and fields.
*
* @group text
* @group legacy
*/
class TextRequiredSummaryUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.4.0.bare.standard.php.gz',
];
}
/**
* Tests that widgets and fields are updated for required summary flag.
*
* @see text_post_update_add_required_summary_flag()
*/
public function testFieldAndWidgetUpdate() {
// No show summary flag exists pre-update.
$entity_form_display = EntityFormDisplay::load('node.article.default');
$options = $entity_form_display->getComponent('body');
$this->assertFalse(array_key_exists('show_summary', $options['settings']));
$field = FieldConfig::load('node.article.body');
$settings = $field->getSettings();
$this->assertFalse(array_key_exists('required_summary', $settings));
$this->runUpdates();
// The show summary setting has been populated on the widget.
$entity_form_display = EntityFormDisplay::load('node.article.default');
$options = $entity_form_display->getComponent('body');
$this->assertIdentical(FALSE, $options['settings']['show_summary']);
// And the so has the required sumamry setting on the field.
$field = FieldConfig::load('node.article.body');
$settings = $field->getSettings();
$this->assertIdentical(FALSE, $settings['required_summary']);
}
}
......@@ -2,8 +2,13 @@
namespace Drupal\Tests\text\Kernel;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\user\Traits\UserCreationTrait;
/**
* Tests text_summary() with different strings and lengths.
......@@ -12,7 +17,16 @@
*/
class TextSummaryTest extends KernelTestBase {
public static $modules = ['system', 'user', 'filter', 'text'];
use UserCreationTrait;
public static $modules = [
'system',
'user',
'filter',
'text',
'field',
'entity_test',
];
protected function setUp() {
parent::setUp();
......@@ -227,4 +241,66 @@ public function assertTextSummary($text, $expected, $format = NULL, $size = NULL
]));
}
/**
* Test required summary.
*/
public function testRequiredSummary() {
$this->installEntitySchema('entity_test');
$this->setUpCurrentUser();
$field_definition = FieldStorageConfig::create([
'field_name' => 'test_textwithsummary',
'type' => 'text_with_summary',
'entity_type' => 'entity_test',
'cardinality' => 1,
'settings' => [
'max_length' => 200,
],
]);
$field_definition->save();
$instance = FieldConfig::create([
'field_name' => 'test_textwithsummary',
'label' => 'A text field',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'settings' => [
'text_processing' => TRUE,
'display_summary' => TRUE,
'required_summary' => TRUE,
],
]);
$instance->save();
EntityFormDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
])->setComponent('test_textwithsummary', [
'type' => 'text_textarea_with_summary',
'settings' => [
'summary_rows' => 2,
'show_summary' => TRUE,
],
])
->save();
// Check the required summary.
$entity = EntityTest::create([
'name' => $this->randomMachineName(),
'type' => 'entity_test',
'test_textwithsummary' => ['value' => $this->randomMachineName()],
]);
$form = \Drupal::service('entity.form_builder')->getForm($entity);
$this->assertTrue(!empty($form['test_textwithsummary']['widget'][0]['summary']), 'Summary field is shown');
$this->assertTrue(!empty($form['test_textwithsummary']['widget'][0]['summary']['#required']), 'Summary field is required');
// Test validation.
/** @var \Symfony\Component\Validator\ConstraintViolation[] $violations */
$violations = $entity->validate();
$this->assertCount(1, $violations);
$this->assertEquals('test_textwithsummary.0.summary', $violations[0]->getPropertyPath());
$this->assertEquals('The summary field is required for A text field', $violations[0]->getMessage());
}
}
<?php
/**
* @file
* Contains post update hooks for the text module.
*/
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\text\Plugin\Field\FieldWidget\TextareaWithSummaryWidget;
/**
* Update text_with_summary fields and widgets to add summary required flags.
*/
function text_post_update_add_required_summary_flag(&$sandbox = NULL) {
$config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
/** @var \Drupal\Core\Field\WidgetPluginManager $field_widget_manager */
$field_widget_manager = \Drupal::service('plugin.manager.field.widget');
$widget_callback = function (EntityDisplayInterface $display) use ($field_widget_manager) {
$needs_save = FALSE;
foreach ($display->getComponents() as $field_name => $component) {
if (empty($component['type'])) {
continue;
}
$plugin_definition = $field_widget_manager->getDefinition($component['type'], FALSE);
if (is_a($plugin_definition['class'], TextareaWithSummaryWidget::class, TRUE)) {
$component['settings']['show_summary'] = FALSE;
$display->setComponent($field_name, $component);
$needs_save = TRUE;
}
}
return $needs_save;
};
$field_callback = function (FieldConfigInterface $field) {
if ($field->getType() !== 'text_with_summary') {
return FALSE;
}
$field->setSetting('required_summary', FALSE);
return TRUE;
};
$config_entity_updater->update($sandbox, 'entity_form_display', $widget_callback);
$config_entity_updater->update($sandbox, 'field_config', $field_callback);
}
......@@ -19,6 +19,7 @@ content:
rows: 9
summary_rows: 3
placeholder: ''
show_summary: false
third_party_settings: { }
info:
type: string_textfield
......
......@@ -26,6 +26,7 @@ content:
rows: 9
summary_rows: 3
placeholder: ''
show_summary: false
third_party_settings: { }
created:
type: datetime_timestamp
......
......@@ -22,6 +22,7 @@ content:
rows: 9
summary_rows: 3
placeholder: ''
show_summary: false
third_party_settings: { }
created:
type: datetime_timestamp
......
......@@ -18,4 +18,5 @@ default_value: { }
default_value_callback: ''
settings:
display_summary: false
required_summary: false
field_type: text_with_summary
......@@ -18,4 +18,5 @@ default_value: { }
default_value_callback: ''
settings:
display_summary: true
required_summary: false
field_type: text_with_summary
......@@ -18,4 +18,5 @@ default_value: { }
default_value_callback: ''
settings:
display_summary: true
required_summary: false
field_type: text_with_summary
......@@ -19,6 +19,7 @@ content:
rows: 9
summary_rows: 3
placeholder: ''
show_summary: false
third_party_settings: { }
info:
type: string_textfield
......
......@@ -26,6 +26,7 @@ content:
rows: 9
summary_rows: 3
placeholder: ''
show_summary: false
third_party_settings: { }
comment:
type: comment_default
......
......@@ -20,6 +20,7 @@ content:
rows: 9
summary_rows: 3
placeholder: ''
show_summary: false
third_party_settings: { }
created:
type: datetime_timestamp
......
......@@ -18,4 +18,5 @@ default_value: { }
default_value_callback: ''
settings:
display_summary: false
required_summary: false
field_type: text_with_summary
......@@ -18,4 +18,5 @@ default_value: { }
default_value_callback: ''
settings:
display_summary: true
required_summary: false
field_type: text_with_summary
......@@ -18,4 +18,5 @@ default_value: { }
default_value_callback: ''
settings:
display_summary: true
required_summary: false
field_type: text_with_summary
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