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

  1. Create a view with a relationship to a second content type without defining the relationship as required.
  2. Add the title of the related view to the output fields.
  3. 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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Chase. created an issue. See original summary.

Chase.’s picture

Issue summary: View changes
cinarb’s picture

Assigned: Unassigned » cinarb

HI, ill review this

Chase.’s picture

Hi 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.

Chase.’s picture

Issue summary: View changes
cinarb’s picture

Assigned: cinarb » Unassigned

So I wasn't able to reproduce the error, unfortunately. I unassigned myself.

Chase.’s picture

I 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?

awasson’s picture

Component: Miscellaneous » Code
Assigned: Unassigned » awasson
Status: Active » Needs review
FileSize
514 bytes

Sorry 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

awasson’s picture

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 checking strlen(). That resolves the issue on my site.

Cheers,
Andrew

awasson’s picture

Chase.’s picture

Thank you awasson. The patch from #9 removes the notice for me.

DamienMcKenna’s picture

Version: 7.x-3.27 » 7.x-3.x-dev
Assigned: awasson » Unassigned
Status: Needs review » Reviewed & tested by the community
Issue tags: +PHP 8.1
Parent issue: » #3311884: Plan for Views 7.x-3.28

That looks simple and reasonable, thank you.

DamienMcKenna’s picture

Status: Reviewed & tested by the community » Needs work

Let's extend this to cover other uses of strlen() where the variable might not be a string.

awasson’s picture

I 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:

if (strlen($value) > 0) {
  switch ($type) {
    . . .

Proposed code:

if (!is_null($value) && strlen($value) > 0) {
  switch ($type) {
  . . .
awasson’s picture

apaderno’s picture

Title: Deprecated function: strlen(): Passing null to parameter #1 ($string) » strlen(): Passing null to parameter #1 ($string) of type string is deprecated
jonnyToomey’s picture

Hello

Would if (!empty($value)) {} be more efficient? Not sure how many checks empty() does behind the scenes

Best wishes
Jonny

DamienMcKenna’s picture

PHP'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.

Anybody’s picture

Status: Needs review » Reviewed & tested by the community

Agree with @DamienMcKenna in #18, so the approach in #15 looks correct to me and is better as #9. So setting this RTBC.

DamienMcKenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks everyone!

  • DamienMcKenna committed 9fc39ca on 7.x-3.x
    Issue #3312595 by awasson, Chase., DamienMcKenna, Anybody: strlen():...
drumm’s picture

(Updating to resolve Drupal.org issue indexing issue, please disregard.)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.