summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2016-02-29 05:43:26 (GMT)
committerNathaniel Catchpole2016-02-29 05:43:26 (GMT)
commit2c88729acfae6cc66796b9a5d559bc8a374e571e (patch)
treeb9f0b9892d7ede9cf074478ac561745e14fe1651
parent9a2b5880af586d571b5972ba1b0ccea2fc9a0920 (diff)
Issue #2578741 by amateescu, swentel, subson, larowlan: Add setting for size to email widget
-rw-r--r--core/config/schema/core.entity.schema.yml3
-rw-r--r--core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php12
-rw-r--r--core/modules/field/field.post_update.php32
-rw-r--r--core/modules/field/src/Tests/Migrate/d6/MigrateFieldWidgetSettingsTest.php1
-rw-r--r--core/modules/field/src/Tests/Update/EmailWidgetSizeSettingUpdateTest.php53
-rw-r--r--core/modules/field/tests/fixtures/update/drupal-8.email_widget_size_setting-2578741.php37
6 files changed, 138 insertions, 0 deletions
diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml
index 8d15f70..bf0e12d 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 ce60ebc..8114c39 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 @@ namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
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 @@ class EmailDefaultWidget extends WidgetBase {
* {@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 @@ class EmailDefaultWidget extends WidgetBase {
else {
$summary[] = t('No placeholder');
}
+ $summary[] = t('Textfield size: @size', array('@size' => $this->getSetting('size')));
return $summary;
}
@@ -71,6 +81,8 @@ class EmailDefaultWidget extends WidgetBase {
'#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 faa6a69..4d0c80b 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 891eb69..8a4140e 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 @@ class MigrateFieldWidgetSettingsTest extends MigrateDrupal6TestBase {
$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 0000000..c0b56d8
--- /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 0000000..ca762bc
--- /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();