Allow empty 'replace' patterns for MachineName render element

Created on 5 November 2019, over 5 years ago
Updated 16 August 2024, 8 months ago

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21...

In the 'replace' key, you can set a string of characters to replace any invalid characters with. But an empty string results in a PHP warning.

Warning: preg_match(): Compilation failed: nothing to repeat at offset 1 in /var/www/html/docroot/core/lib/Drupal/Core/Render/Element/MachineName.php on line 240

Added an empty check before the preg_match in attached patch.

โœจ Feature request
Status

RTBC

Version

11.0 ๐Ÿ”ฅ

Component
Formย  โ†’

Last updated 3 days ago

Created by

๐Ÿ‡ง๐Ÿ‡ชBelgium Grayle

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    I ran into this as well, trying to use the following machine name element in a form for specifying a URL subdomain, in which I wanted to only allow alphanumeric and hypens (not underscores), and in which invalid characters should be removed entirely (not replaced with anything):

        $form['subdomain'] = [
          '#type' => 'machine_name',
          '#title' => $this->t('Subdomain'),
          '#machine_name' => [
            'exists' => [$this, 'subdomainExists'],
            'label' => $this->t('Subdomain'),
            'source' => ['site_name'],
            'replace_pattern' => '[^a-z0-9-]+',
            'replace' => '',
            'error' => $this->t('The subdomain can only contain letters, numbers, and dashes.'),
          ],
        ];
    

    I was experiencing the same warning. Applying the patch fixes it. Thanks @Grayle!

  • Status changed to Needs work 8 months ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance nod_ Lille

    Please convert the patch to a MR so that tests can run :)

  • Status changed to Needs review 8 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Converted patch by @Grayle to a merge request. Back to "Needs review" for tests to run...

  • Pipeline finished with Success
    8 months ago
    Total: 491s
    #259230
  • Status changed to Needs work 8 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Can the issue summary be updated, should be using the standard issue template.

    Also as a feature request probably need some kind of test coverage to make sure it doesn't break in the future.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    > as a feature request probably need some kind of test coverage

    I actually think this should be a bug report, because the machine_name element type already exists, but causes a warning under certain conditions. Updating the title and category to reflect that...

    Still probably needs tests. And issue summary update.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Recommendation for test is to see if there is an existing one you can picky back with hopefully just an additional assertion.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Updated issue summary.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Recommendation for test is to see if there is an existing one you can picky back with hopefully just an additional assertion.

    Hmm yea - I wonder if this will be tricky, because essentially we need to test to see if a PHP Warning is thrown. The code does "work", and thus a test of the actual functionality will pass, but it's the PHP Warning we are trying to fix.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    I always lean on the side of test coverage, microsoft re-enforced that

    But there is policy for small changes not needing test coverage https://www.drupal.org/about/core/policies/core-change-policies/core-gat... โ†’ . Just a matter of proving the points are hit to not need them.

  • Status changed to Needs review 13 days ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    I would argue this is a case where a test isn't needed, and may not be possible... but leave that to others to decide.

    there is policy for small changes not needing test coverage https://www.drupal.org/about/core/policies/core-change-policies/core-gat... โ†’ . Just a matter of proving the points are hit to not need them.

    Copied and answered the questions below (to the best of my understanding):

    An automated test for a bug fix may not be needed if the following are true and if the majority of answers to the questions below is 'Yes'.

    1. The issue has clear โ€˜Steps to reproduceโ€™ in the Issue Summary. โœ”๏ธ
    2. The fix is 'trivial' with small, easy to understand changes. โœ”๏ธ

    Questions

    1. Is the fix is easy to verify by manual testing? YES
    2. Is the fix in self-contained/@internal code where we expect minimal interaction with contrib? Examples are plugins, controllers etc. YES
    3. Is the fix achieved without adding new, untested, code paths? YES
    4. Is an explicit 'regression' test needed? NO
    5. Does the issue expose a general lack of test coverage for the specific subsystem? If so, is it better to add generic test coverage for that subsystem in a separate issue? NO
    6. If this fix is committed without test coverage but then later regresses, is the impact likely to be minimal or at least no worse than leaving the bug unfixed? NO

    I removed the "Needs Tests" tag, but please restore it if you disagree.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Looking at the steps I would assume a test could be written.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Moving to NW because the MR is now almost 1000 commits back and still think test coverage is needed.

Production build 0.71.5 2024