From 2c88729acfae6cc66796b9a5d559bc8a374e571e Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Mon, 29 Feb 2016 14:43:26 +0900 Subject: [PATCH] Issue #2578741 by amateescu, swentel, subson, larowlan: Add setting for size to email widget --- core/config/schema/core.entity.schema.yml | 3 ++ .../Field/FieldWidget/EmailDefaultWidget.php | 12 +++++ core/modules/field/field.post_update.php | 32 +++++++++++ .../d6/MigrateFieldWidgetSettingsTest.php | 1 + .../EmailWidgetSizeSettingUpdateTest.php | 53 +++++++++++++++++++ ...al-8.email_widget_size_setting-2578741.php | 37 +++++++++++++ 6 files changed, 138 insertions(+) create mode 100644 core/modules/field/src/Tests/Update/EmailWidgetSizeSettingUpdateTest.php create mode 100644 core/modules/field/tests/fixtures/update/drupal-8.email_widget_size_setting-2578741.php diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml index 8d15f70cca13..bf0e12d939cb 100644 --- a/core/config/schema/core.entity.schema.yml +++ b/core/config/schema/core.entity.schema.yml @@ -178,6 +178,9 @@ field.widget.settings.email_default: placeholder: type: label label: 'Placeholder' + size: + type: integer + label: 'Size of email field' field.widget.settings.datetime_timestamp: type: mapping diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php index ce60ebc8a28a..8114c39663dd 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php @@ -10,6 +10,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\Element\Email; /** * Plugin implementation of the 'email_default' widget. @@ -29,6 +30,7 @@ class EmailDefaultWidget extends WidgetBase { */ public static function defaultSettings() { return array( + 'size' => 60, 'placeholder' => '', ) + parent::defaultSettings(); } @@ -37,6 +39,13 @@ public static function defaultSettings() { * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { + $element['size'] = array( + '#type' => 'number', + '#title' => $this->t('Textfield size'), + '#default_value' => $this->getSetting('size'), + '#required' => TRUE, + '#min' => 1, + ); $element['placeholder'] = array( '#type' => 'textfield', '#title' => t('Placeholder'), @@ -59,6 +68,7 @@ public function settingsSummary() { else { $summary[] = t('No placeholder'); } + $summary[] = t('Textfield size: @size', array('@size' => $this->getSetting('size'))); return $summary; } @@ -71,6 +81,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#type' => 'email', '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL, '#placeholder' => $this->getSetting('placeholder'), + '#size' => $this->getSetting('size'), + '#maxlength' => Email::EMAIL_MAX_LENGTH, ); return $element; } diff --git a/core/modules/field/field.post_update.php b/core/modules/field/field.post_update.php index faa6a69f91fd..4d0c80bd8503 100644 --- a/core/modules/field/field.post_update.php +++ b/core/modules/field/field.post_update.php @@ -5,6 +5,7 @@ * Post update functions for Field module. */ +use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldConfig; @@ -44,3 +45,34 @@ function field_post_update_entity_reference_handler_setting() { /** * @} End of "addtogroup updates-8.0.0-beta". */ + +/** + * @addtogroup updates-8.1.0 + * @{ + */ + +/** + * Adds the 'size' setting for email widgets. + */ +function field_post_update_email_widget_size_setting() { + foreach (EntityFormDisplay::loadMultiple() as $entity_form_display) { + $changed = FALSE; + foreach ($entity_form_display->getComponents() as $name => $options) { + if (isset($options['type']) && $options['type'] === 'email_default') { + $options['settings']['size'] = '60'; + $entity_form_display->setComponent($name, $options); + $changed = TRUE; + } + } + + if ($changed) { + $entity_form_display->save(); + } + } + + return t('The new size setting for email widgets has been added.'); +} + +/** + * @} End of "addtogroup updates-8.1.0". + */ diff --git a/core/modules/field/src/Tests/Migrate/d6/MigrateFieldWidgetSettingsTest.php b/core/modules/field/src/Tests/Migrate/d6/MigrateFieldWidgetSettingsTest.php index 891eb69e0597..8a4140ec2cd2 100644 --- a/core/modules/field/src/Tests/Migrate/d6/MigrateFieldWidgetSettingsTest.php +++ b/core/modules/field/src/Tests/Migrate/d6/MigrateFieldWidgetSettingsTest.php @@ -56,6 +56,7 @@ public function testWidgetSettings() { $component = $form_display->getComponent('field_test_email'); $expected['type'] = 'email_default'; $expected['weight'] = 6; + $expected['settings'] = array('placeholder' => '', 'size' => 60); $this->assertIdentical($expected, $component); // Link field. diff --git a/core/modules/field/src/Tests/Update/EmailWidgetSizeSettingUpdateTest.php b/core/modules/field/src/Tests/Update/EmailWidgetSizeSettingUpdateTest.php new file mode 100644 index 000000000000..c0b56d817dbd --- /dev/null +++ b/core/modules/field/src/Tests/Update/EmailWidgetSizeSettingUpdateTest.php @@ -0,0 +1,53 @@ +<?php + +/** + * @file + * Contains \Drupal\field\Tests\Update\EmailWidgetSizeSettingUpdateTest. + */ + +namespace Drupal\field\Tests\Update; + +use Drupal\system\Tests\Update\UpdatePathTestBase; + +/** + * Tests the update for the 'size' setting of the 'email_default' field widget. + * + * @group field + */ +class EmailWidgetSizeSettingUpdateTest extends UpdatePathTestBase { + + /** + * {@inheritdoc} + */ + protected function setDatabaseDumpFiles() { + $this->databaseDumpFiles = [ + __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz', + __DIR__ . '/../../../tests/fixtures/update/drupal-8.email_widget_size_setting-2578741.php', + ]; + } + + /** + * Tests field_post_update_email_widget_size_setting(). + * + * @see field_post_update_email_widget_size_setting() + */ + public function testFieldPostUpdateEmailWidgetSizeSetting() { + $configFactory = $this->container->get('config.factory'); + + // Load the 'node.article.default' entity form display and check that the + // widget for 'field_email_2578741' does not have a 'size' setting. + /** @var \Drupal\Core\Config\Config $config */ + $config = $configFactory->get('core.entity_form_display.node.article.default'); + $settings = $config->get('content.field_email_2578741.settings'); + $this->assertTrue(!isset($settings['size']), 'The size setting does not exist prior to running the update functions.'); + + // Run updates. + $this->runUpdates(); + + // Reload the config and check that the 'size' setting has been populated. + $config = $configFactory->get('core.entity_form_display.node.article.default'); + $settings = $config->get('content.field_email_2578741.settings'); + $this->assertEqual($settings['size'], 60, 'The size setting exists and it has the correct default value.'); + } + +} diff --git a/core/modules/field/tests/fixtures/update/drupal-8.email_widget_size_setting-2578741.php b/core/modules/field/tests/fixtures/update/drupal-8.email_widget_size_setting-2578741.php new file mode 100644 index 000000000000..ca762bc034d2 --- /dev/null +++ b/core/modules/field/tests/fixtures/update/drupal-8.email_widget_size_setting-2578741.php @@ -0,0 +1,37 @@ +<?php + +/** + * @file + * Contains SQL necessary to add a new component for an email field/widget to + * the 'node.article.default' entity form display. + */ + +use Drupal\Core\Database\Database; + +$connection = Database::getConnection(); + +$config = $connection->select('config', 'c') + ->fields('c') + ->condition('collection', '') + ->condition('name', 'core.entity_form_display.node.article.default') + ->execute() + ->fetchAssoc(); + +$data = unserialize($config['data']); + +// Manually add a new component that simulates an email field using the default +// email widget. +$data['content']['field_email_2578741'] = [ + 'weight' => 20, + 'settings' => [ + 'placeholder' => '', + ], + 'third_party_settings' => [], + 'type' => 'email_default', +]; + +$connection->update('config') + ->fields(['data' => serialize($data)]) + ->condition('collection', '') + ->condition('name', 'core.entity_form_display.node.article.default') + ->execute(); -- GitLab