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.
Problem/Motivation
A notice is shown when opening a view:
Deprecated function: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in views_handler->sanitize_value() (Line 331 of views/includes/handlers.inc).
Steps to reproduce
- Create a view with a relationship to a second content type without defining the relationship as required.
- Add the title of the related view to the output fields.
- Create node(s) to show in the view which do not have the relationship.
One notices per node without the relationship is shown. The notice is not shown when using other fields. It's only shown for the title field.
Comments
Comment #2
Chase. CreditAttribution: Chase. commentedComment #3
cinarb CreditAttribution: cinarb at CI&T commentedHI, ill review this
Comment #4
Chase. CreditAttribution: Chase. commentedHi and thanks! I forgot to mention that the notice is shown only for entries where the relationship is missing. Crucial info that I will add in the issue summary as well.
Comment #5
Chase. CreditAttribution: Chase. commentedComment #6
cinarb CreditAttribution: cinarb at CI&T commentedSo I wasn't able to reproduce the error, unfortunately. I unassigned myself.
Comment #7
Chase. CreditAttribution: Chase. commentedI re-tried and can still reproduce this, with both the references and entityreference modules on an otherwise fresh install. Is there any further information I should supply?
Comment #8
awasson CreditAttribution: awasson commentedSorry about that. Wrong issue in the Queue. I'll attach the appropriate patch shortly.
I also see this on Drupal 7 Views that are on PHP 8.1.It appears that the $value can be passed as NULL.I've attached a patch that casts $value as a string before trimming it and that resolves the issue on my site.Drupal core 7.92
Views (for Drupal 7) 7.x-3.27
PHP 8.1
Comment #9
awasson CreditAttribution: awasson commentedI also see this on Drupal 7 Views that are on PHP 8.1.
It appears that the $value can be passed as NULL.
I've attached a patch that casts $value as a string before checking strlen(). That resolves the issue on my site.
Cheers,
Andrew
Comment #10
awasson CreditAttribution: awasson commentedComment #11
Chase. CreditAttribution: Chase. commentedThank you awasson. The patch from #9 removes the notice for me.
Comment #12
DamienMcKennaThat looks simple and reasonable, thank you.
Comment #13
DamienMcKennaLet's extend this to cover other uses of strlen() where the variable might not be a string.
Comment #14
awasson CreditAttribution: awasson commentedI suppose we could add !is_null($value) to the strlen($value) conditional block.
I've tested this on a site I'm running on Drupal 7 with PHP 8.1 and it does what it's supposed to according to my tests. If it's suitable, I can upload a patch.
Original code:
Proposed code:
Comment #15
awasson CreditAttribution: awasson commentedPatch attached
Comment #16
apadernoComment #17
jonnyToomey CreditAttribution: jonnyToomey commentedHello
Would
if (!empty($value)) {}
be more efficient? Not sure how many checks empty() does behind the scenesBest wishes
Jonny
Comment #18
DamienMcKennaPHP's empty() function returns FALSE for several valid values, specifically the number 0 in either int, float or string format; because these can be valid values for form fields, it is safest to not skip them.
Comment #19
AnybodyAgree with @DamienMcKenna in #18, so the approach in #15 looks correct to me and is better as #9. So setting this RTBC.
Comment #20
DamienMcKennaCommitted. Thanks everyone!
Comment #22
drumm(Updating to resolve Drupal.org issue indexing issue, please disregard.)