Verified Commit b4448877 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2862922 by smustgrave, NitinLama, zaporylie, rpayanm, DanielVeza, pooja...

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 d5b4f005)
parent ba5b3091
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -22,6 +22,11 @@
 */
class TelephoneItem extends FieldItemBase {

  /**
   * The maximum length for a telephone value.
   */
  const MAX_LENGTH = 256;

  /**
   * {@inheritdoc}
   */
@@ -30,7 +35,7 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
      'columns' => [
        'value' => [
          'type' => 'varchar',
          'length' => 256,
          'length' => self::MAX_LENGTH,
        ],
      ],
    ];
@@ -62,12 +67,11 @@ public function getConstraints() {
    $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]),
        ],
      ],
    ]);
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
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 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
      '#type' => 'tel',
      '#default_value' => $items[$delta]->value ?? NULL,
      '#placeholder' => $this->getSetting('placeholder'),
      '#maxlength' => TelephoneItem::MAX_LENGTH,
    ];
    return $element;
  }
+2 −0
Original line number Diff line number Diff line
@@ -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 @@ protected function setUp(): void {
  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"');
  }