Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Currently the UUID pattern is hardcoded in Uuid::isValid() method. The downside of this is that any contrib module that wants to use the UUID pattern in regex has to copy and paste the string. I think developers would find it more useful if the pattern was moved to a class constant and so then it could be reused.
An example of a use case for this is a UUID url resolver, as currently exists in the d7 UUID contrib module.
Comment | File | Size | Author |
---|---|---|---|
#9 | interdiff-1847932-1-9.txt | 1.73 KB | skwashd |
#9 | uuid_pattern_constant-1847932-9.patch | 858 bytes | skwashd |
Comments
Comment #1
skwashd CreditAttribution: skwashd commentedComment #2
skwashd CreditAttribution: skwashd commentedUpdating status.
Comment #3
klausiThis should use late static binding per default, so use "static" instead of "self".
Why are the start and end characters "^" and "$" not part of the pattern? They are part of the regex, right?
Also: the "/" delimiter should be escaped in case the pattern contains slashes as well.
Comment #4
skwashd CreditAttribution: skwashd commentedI thought about this, but given UUIDs are defined in RFC 4122 the value shouldn't be overridden by sub classes.
Because then the pattern can only be used for matching a complete UUID and nothing else. The whole idea of this is to allow contrib to use the pattern for matching UUIDs in larger strings. If I include the '^' and '$', then you can't do something like this:
if (preg_match('#^uuid/node/(' . Uuid::VALID_PATTERN . ')$#', $url, $m)) { /* do stuff */ }
.Escaping the "/" will break the regex. If the pattern did contain slashes, which is shouldn't as per the RFC, then the slashes in the pattern should be escaped not the delimiter.
Personally I prefer using "#" over "/" as a delimiter for web stuff these days as it is less likely to require escaping in patterns. The existing code uses slashes as the delimiters, so I'm leaving them as they are.
Setting back to needs review. Hopefully the test bot can clone properly this time.
Comment #5
skwashd CreditAttribution: skwashd commented#1: drupal-1847932-uuid-pattern-class-const.patch queued for re-testing.
Comment #6
skwashd CreditAttribution: skwashd commentedThe tests passed, but the status hasn't updated here. http://qa.drupal.org/pifr/test/390413
Comment #9
skwashd CreditAttribution: skwashd commentedRerolled patch and attached interdiff.
Comment #10
klausiOk, the constant seems to be a better idea anyway. Looks good!
Comment #11
alexpottCommitted 393fccc and pushed to 8.0.x. Thanks!