Phone Number field does not support setting a custom size of the input field

Created on 10 July 2023, over 1 year ago
Updated 21 July 2023, over 1 year ago

The size (also known as width or length) of other text fields can be controlled with a field setting, but the phone number doesn't have this option.

Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇳🇮Nicaragua ldavidsp Nicaragua

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @ldavidsp
  • 🇳🇮Nicaragua ldavidsp Nicaragua
  • Status changed to Needs work over 1 year ago
  • heddn Nicaragua
    1. +++ b/src/Plugin/Field/FieldWidget/PhoneNumberWidget.php
      @@ -61,6 +61,7 @@ class PhoneNumberWidget extends WidgetBase {
      +      'size' => NULL,
      

      default to 60

      'size' => 60,

    2. +++ b/src/Plugin/Field/FieldWidget/PhoneNumberWidget.php
      @@ -94,6 +95,14 @@ class PhoneNumberWidget extends WidgetBase {
      +      '#default_value' => $this->getSetting('size') !== NULL ? $this->getSetting('size') : 60,
      

      Then this can just be:

      '#default_value' => $this->getSetting('size'),

    3. +++ b/src/Element/PhoneNumber.php
      @@ -172,6 +172,7 @@ class PhoneNumber extends FormElement {
      +        'size' => $settings['size'] ?? 60,
      

      A default size should be set in defaultSettings() and we need an entry in config schema for the setting.

    4. +++ b/src/Plugin/Field/FieldWidget/PhoneNumberWidget.php
      @@ -139,6 +150,7 @@ class PhoneNumberWidget extends WidgetBase {
      +        'size' => isset($settings['size']) ? $settings['size'] : NULL,
      

      'size' => $settings['size'],

    Some minor feedback. But generally this looks like a great idea.

  • 🇳🇮Nicaragua ldavidsp Nicaragua
  • Status changed to Needs review over 1 year ago
  • 🇳🇮Nicaragua ldavidsp Nicaragua
  • heddn Nicaragua
    +++ b/src/Element/PhoneNumber.php
    @@ -172,6 +172,7 @@ class PhoneNumber extends FormElement {
    +        'size' => $settings['size'],
    

    Oh, this is the render element, we need an isset like the line before.

  • 🇳🇮Nicaragua ldavidsp Nicaragua
  • heddn Nicaragua

    Patch in #7 seems the same as in #4.

  • heddn Nicaragua

    This responds to the feedback. And doesn't limit the render element to only sizing the phone number but also makes it possible to alter the size of the phone extension as well. The change is only to the API of the render element. I don't think changing the size of the extension is all that needed, so I didn't make it a configuration option on the field widget.

  • 🇮🇳India kapil-virasat

    I have reproduce the issue on my local , and I have added changes for fix the issue by adding some changes in phone number field for set size.

  • heddn Nicaragua

    I'm not sure where #10 came from. There's no interdiff and it looks like a reposting of an earlier patch. Hiding it so it is clear that #9 is the preferred direction here.

  • 🇺🇸United States volkswagenchick San Francisco Bay Area

    @kapil-virasat

    As @heddn mentioned, your patch does look like a reposting of an earlier patch.

    I find it helpful to create an interdiff to be sure that I made the necessary changes and the changes are reflected in the patch. In the past, I have inadvertently shared the original patch or an empty patch, but with an interdiff I can quickly scan the file and confirm that the changes were made.

    This also helps those who review your work.

    Here is some documentation around what is an interdiff , why we create them, and how others find them useful when reviewing patches.

    Thanks,

    AmyJune HIneline, Drupal Core Mentor

  • Status changed to Needs work over 1 year ago
  • 🇧🇪Belgium tim-diels Belgium 🇧🇪

    Thanks for the issue and all the work already. I'm willing to add this if the patch can be completed. I'm missing some more parts like:

    • config schema for the extension_size
    • maybe also expose the extension_size as field on the form?
    • properties documentation in PhoneNumber for development usage

    For the rest is seems ok. Did not test it yet, but will test once these extras are addressed.

  • Status changed to Needs review over 1 year ago
  • heddn Nicaragua

    I intentionally didn't add the phone extension to the widget. The chance that someone needs to ever modify the extension is pretty low. However, if we are going to modify the render element's API, it seemed like a pretty easy addition while we are messing with things. If someone _does_ need to ever change the widget, they can do so pretty easily in a custom widget. That should cover bullets 1 & 2 in #14.

    Last bullet addressed here.

  • 🇧🇪Belgium tim-diels Belgium 🇧🇪

    Interdiff was same as previous interdiff. So just for easier review, i've uploaded the correct interdiff.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    CI aborted
  • 🇧🇪Belgium tim-diels Belgium 🇧🇪

    For my bullet point for the config schema, this was still a valid point to be taken care of as it gave me an error on the extension_size key for config.

    After testings, it seems the part where you add the defaults in valueCallback is not needed.
    This seems not to be the place to set defaults.

    I adjusted the patch and tested this with both a field added through UI and on a custom form. But would like some feedback if this works for you too.

    Example custom form:

    $form['phone_number'] = [
      '#type' => 'phone_number',
      '#title' => $this->t('Phone Number'),
      '#phone_number' => [
        'extension_field' => TRUE,
        'phone_size' => 40,
        'extension_size' => 10,
      ]
    ];
    
  • heddn Nicaragua

    Did we test without any defined phone or extension size? Does that work? If that works, then I think this is good to go.

  • Status changed to RTBC over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    Patch Failed to Apply
  • 🇧🇪Belgium tim-diels Belgium 🇧🇪

    Yes, I tested with just adding the fields and no changes. I'm going to commit this, thanks for the help @heddn.

  • Status changed to Fixed over 1 year ago
  • 🇧🇪Belgium tim-diels Belgium 🇧🇪

    Thanks all for the work, committed.

  • 🇧🇪Belgium tim-diels Belgium 🇧🇪
  • Status changed to Fixed over 1 year ago
  • 🇧🇪Belgium tim-diels Belgium 🇧🇪
Production build 0.71.5 2024