Issue #2862922 by smustgrave, NitinLama, zaporylie, rpayanm, DanielVeza, pooja saraah, ranjith_kumar_k_u, Abhijith S, bnjmnm, Kristen Pol, alexpott, ameymudras, murilohp: Set #maxlength for TelephoneDefaultWidget to the one specified in TelephoneItem plugin

(cherry picked from commit d5b4f0050f)
merge-requests/2670/head
Lee Rowlands 2022-11-28 10:40:11 +10:00
parent ba5b309131
commit b4448877ce
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
3 changed files with 12 additions and 4 deletions

View File

@ -22,6 +22,11 @@ use Drupal\Core\StringTranslation\TranslatableMarkup;
*/
class TelephoneItem extends FieldItemBase {
/**
* The maximum length for a telephone value.
*/
const MAX_LENGTH = 256;
/**
* {@inheritdoc}
*/
@ -30,7 +35,7 @@ class TelephoneItem extends FieldItemBase {
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 256,
'length' => self::MAX_LENGTH,
],
],
];
@ -62,12 +67,11 @@ class TelephoneItem extends FieldItemBase {
$constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
$constraints = parent::getConstraints();
$max_length = 256;
$constraints[] = $constraint_manager->create('ComplexData', [
'value' => [
'Length' => [
'max' => $max_length,
'maxMessage' => $this->t('%name: the telephone number may not be longer than @max characters.', ['%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length]),
'max' => self::MAX_LENGTH,
'maxMessage' => $this->t('%name: the telephone number may not be longer than @max characters.', ['%name' => $this->getFieldDefinition()->getLabel(), '@max' => self::MAX_LENGTH]),
],
],
]);

View File

@ -5,6 +5,7 @@ namespace Drupal\telephone\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\telephone\Plugin\Field\FieldType\TelephoneItem;
/**
* Plugin implementation of the 'telephone_default' widget.
@ -66,6 +67,7 @@ class TelephoneDefaultWidget extends WidgetBase {
'#type' => 'tel',
'#default_value' => $items[$delta]->value ?? NULL,
'#placeholder' => $this->getSetting('placeholder'),
'#maxlength' => TelephoneItem::MAX_LENGTH,
];
return $element;
}

View File

@ -3,6 +3,7 @@
namespace Drupal\Tests\telephone\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\telephone\Plugin\Field\FieldType\TelephoneItem;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldStorageConfig;
@ -89,6 +90,7 @@ class TelephoneFieldTest extends BrowserTestBase {
public function testTelephoneWidget() {
$this->drupalGet('node/add/article');
$this->assertSession()->fieldValueEquals("field_telephone[0][value]", '');
$this->assertSession()->elementAttributeContains('css', 'input[name="field_telephone[0][value]"]', 'maxlength', TelephoneItem::MAX_LENGTH);
$this->assertSession()->responseContains('placeholder="123-456-7890"');
}