Issue with creating URL field

Created on 27 December 2023, 11 months ago

I am encountering an issue when trying to create a URL field

Steps Reproduce:

1. Installed and enabled the module using Composer with the command: composer require 'drupal/url_text:^1.0@beta'.

2: When trying to create a URL field inside the content type it throws an error, error mentioned below

TypeError: Cannot assign null to property Drupal\url_text\Plugin\Field\FieldWidget\UrlTextfieldWidget::$schemes of type array in Drupal\url_text\Plugin\Field\FieldWidget\UrlTextfieldWidget->formElement() (line 78 of modules\contrib\url_text\src\Plugin\Field\FieldWidget\UrlTextfieldWidget.php).

I've attached an image illustrating my actions—installing the module and searching within the extended panel.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇮🇳India aman_lnwebworks

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

Comments & Activities

  • Issue created by @aman_lnwebworks
  • 🇮🇳India aman_lnwebworks

    Here is the patch for the issue

  • Assigned to j. ayen green
  • Status changed to Fixed 11 months ago
  • 🇺🇸United States j. ayen green

    Thanks for the catch! While your patch addresses null being returned, it also would prevent any scheme from being returned were any selected.

    Where Drupal directs the user to check the boxes for the schemes that should be allowed, and says that if none are selected all will be allowed, it turns out that this is a promise to the user that needs to be fulfilled by the coder...it's not automatic. I had thought all values would be returned.

    What I'm doing to fix this is that if null is returned from the field settings, I call the field item plugin to get the list of schemes and return that list instead of null.

    The code changes are:

    • Change the method in the field item to being public static
    • Add a use statement in the field widget identifying the field item class
    • Change the return from getSchemes() to be

    return $def->getSetting('allowed_schemes') ?? UrlItem::getSchemes();

    The results is that when no schemes are selected in the field settings, all are returned by getSchemes() and the resulting description under the text box is given as:

    Enter a URL. Allowed schemes are: http://, https://, ftp://, mailto://, sftp://, ssh://, tel://.

    You can pull the dev branch to get this change.

  • Status changed to Fixed 11 months ago
Production build 0.71.5 2024