Problem/Motivation
If you have a value condition using a regular expression condition, but the field has no value (including for example on the new content form) then it causes an error:
Deprecated function: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated in Drupal\conditional_fields\ConditionalFieldsFormHelper::evaluateDependency() (line 793 of modules/contrib/conditional_fields/src/ConditionalFieldsFormHelper.php).
Steps to reproduce
- Create an entity reference field which uses a select widget. The field should not be required.
- Create another field (e.g. a text field).
- Add a conditional field to control visibility of this 2nd field based on the 1st (entity reference) field.
- Choose "Condition" = "Value".
- Choose "Values input mode" = "Regular expression..."
- Enter a regular expression which will match one of the values in the entity reference field.
- Edit some content which doesn't have a value for the entity reference field.
- Save the content.
- The deprecation appears.
Proposed resolution
It should be able to handle the fact that a field might not have a value.
Now that PHP 8.1 requires string values to string functions, the simplest thing would be to cast the value and this will maintain the PHP 7 behaviour.
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | preg_match-3411928-10.patch | 686 bytes | dineshkumarbollu |
| #3 | conditional_fields-3411928-3.patch | 663 bytes | rob230 |
Issue fork conditional_fields-3411928
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
rob230 commentedComment #3
rob230 commentedPatch to add null coalescing operator. Casting to string would also work.
Comment #4
rob230 commentedComment #7
abhishek_gupta1 commented@colan, fixed the issue and created. please kindly review the MR
Comment #8
liam morlandIf
$reference_valueis empty, then it won't match the regex anyway so might as well avoid the call.Comment #9
dqdregarding #8
Comment #10
dineshkumarbollu commentedBased on #8 made changes.
Comment #11
dqdRegarding
?? vs ?: vs !=NULLsee following code test examples: https://3v4l.org/McavCComment #12
dqdComment #13
heddnSee most recent comments.
Comment #15
prem suthar commentedTry To Addressed the #8 point .
Comment #16
liam morlandThe best thing might just to be cast to string; see merge request. The problem with
empt()is that0is empty.Comment #18
heddn