Commit 2c88729a authored by catch's avatar catch

Issue #2578741 by amateescu, swentel, subson, larowlan: Add setting for size to email widget

parent 9a2b5880
......@@ -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
......
......@@ -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;
}
......
......@@ -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".
*/
......@@ -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.
......
<?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.');
}
}
<?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();
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