Problem/Motivation

After upgrading to Drupal 8.1 I'm seeing this message:

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/html/docroot/core/modules/layout_builder/src/Cache/LayoutBuilderUiCacheContext.php on line 28

Steps to reproduce

Upgrade to PHP 8.1

Proposed resolution

Similar to https://www.drupal.org/project/styleguide/issues/3258816
Basically it casts the first parameter forcing to be a string value.

Issue fork drupal-3313342

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

afsch created an issue. See original summary.

afsch’s picture

afsch’s picture

hhvardan’s picture

@afsch could you add more steps how to reproduce. Thanks.

Prem Suthar’s picture

Plz add Steps to reproduce @afsch

Akram Khan’s picture

Updating patch go through this once and found similar https://www.drupal.org/project/drupal/issues/3312931

Akram Khan’s picture

Status: Active » Needs work
jrochate’s picture

Thanks. It worked for me.

Prem Suthar’s picture

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Rajeshreeputra made their first commit to this issue’s fork.

saidatom made their first commit to this issue’s fork.

claudiu.cristea’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: +PHP 8.1
claudiu.cristea’s picture

For me it happens when I run certain Drush commands. I guess the route matcher cannot determine the route (?)

xjm’s picture

Thanks folks for working on this.

Removing credit for an apparently unnecessary MR and rerolls, and hiding incorrect MRs and patches. (Right now, we have four different approaches in about as many patches and MRs.)

Rather than trying to win the cleverest line of code award with multiple string-manipulation function calls and typecasts, how about we actually just untangle the logic and write out a proper if/else?

Both of these lines trace to #3002608: Remove contextual links not related to layout administration inside layout builder blocks and you can see discussion of them on that issue as well.

Thanks!

xjm’s picture

Version: 10.0.x-dev » 9.5.x-dev

Did not mean to change the branch, though.

xjm’s picture

Adding credit for @claudiu.cristea for providing potential STR.

xjm’s picture

@Rajeshreeputra, you just appear to have merged commits without addressing my feedback in #17.

idimopoulos made their first commit to this issue’s fork.

idimopoulos’s picture

Status: Needs work » Needs review

This patch broke a few contextual links for me. That is because the strpos() === 0 means that the needle is expected in the beginning of the string. Changing this to stropos() === FALSE almost negates entirely the condition.

xjm’s picture

Status: Needs review » Needs work

Please review comment #17, in which I asked us to write this out as an if-else.

No one should push further commits unless they are attempting to implement that feedback. Thanks!

idimopoulos’s picture

Status: Needs work » Needs review

@xjm I am really sorry I missed your comment, I looked into it now. I was just trying to fix the logic error.
I have committed an attempt to untangle it but I am not sure which part do you mean. I tried to make it clearer. If I was wrong, please, let me know what do you need.

claudiu.cristea’s picture

Version: 9.5.x-dev » 10.0.x-dev

The base branch is 10.0.x

claudiu.cristea’s picture

No idea what's the issue with the Composer failure

xjm’s picture

The Composer error is due to some infrastructural issues currently with testing. See here:
https://twitter.com/drupal_infra/status/1613597701215776768

xjm’s picture

Temporary patch of the latest diff to get tests to run.

xjm’s picture

Thanks @idimopoulos, that's on the right track! I'll continue to post comments on the MR, as hopefully we can go back to using it once the testing issues are resolved.

xjm’s picture

Status: Needs review » Needs work

Posted some suggestions to further clarify the lines so we don't get this confusion in the future.

Bhanu951 made their first commit to this issue’s fork.

deepakkm made their first commit to this issue’s fork.

claudiu.cristea’s picture

Version: 10.0.x-dev » 9.5.x-dev

This is a bug and needs to be addressed in 9.5.x

claudiu.cristea’s picture

Status: Needs work » Needs review

Implemented the fix and addressed @xjm remarks in merge request 3587

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

From what I can tell MR 3573 is the same as MR 3572 which might be the same as 3143 before it got borked with D10 changes.

MR 3587 seems to be the correct approach but feels it will need test case.

claudiu.cristea’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Test what? This is just a tiny refactoring because of PHP 8.1 notices

claudiu.cristea’s picture

Re #39, #40: There's already test coverage in \Drupal\Tests\layout_builder\Functional\LayoutSectionTest::testLayoutSectionFormatter()

alexpott’s picture

Afaics some of the fixes being applied here were done in #3324574: Deprecated function: strpos() in layout_builder_entity_view_alter.

There are way too many MRs on this issue but also AFAICS the fix to LayoutBuilderUiCacheContext won't actually fix the problem. I think the fix should be something like;
return 'is_layout_builder_ui.' . (int) !str_starts_with($this->routeMatch->getRouteName() ?? '', 'layout_builder.');

i.e. adding ?? '' to cope with situation where getRouteName() returns a NULL. It'd be interesting to understand when this function is being called at there is no route match.

claudiu.cristea’s picture

@alexpott, The idea of casting to (int) was rejected by @xjm in #17, #21, #24. She insisted to switch to an if (...) block

claudiu.cristea’s picture

Hiding patches

claudiu.cristea’s picture

Version: 9.5.x-dev » 10.1.x-dev

Discussed in Slack with @alexpott and will rebase against 10.1.x

claudiu.cristea’s picture

Re #42

I think the fix should be something like;
return 'is_layout_builder_ui.' . (int) !str_starts_with($this->routeMatch->getRouteName() ?? '', 'layout_builder.');

i.e. adding ?? '' to cope with situation where getRouteName() returns a NULL. It'd be interesting to understand when this function is being called at there is no route match.

Discussed with @alexpott on Slack and we agreed that the fix, currently in merge request 3587, is correct and fulfills also the requirement from #17.

This is ready for a final review.

acbramley’s picture

Status: Needs review » Reviewed & tested by the community

Following the discussion in slack, it seems like this doesn't need a test so the fix looks good to me.

alexpott’s picture

Version: 10.1.x-dev » 9.5.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 341af8bf30 and pushed to 10.1.x. Thanks!
Committed and pushed 9dd37aa3ee to 10.0.x and 067b544dfd to 9.5.x. Thanks!

  • alexpott committed 341af8bf on 10.1.x
    Issue #3313342 by Rajeshreeputra, idimopoulos, claudiu.cristea, deepakkm...

  • alexpott committed 9dd37aa3 on 10.0.x
    Issue #3313342 by Rajeshreeputra, idimopoulos, claudiu.cristea, deepakkm...

  • alexpott committed 067b544d on 9.5.x
    Issue #3313342 by Rajeshreeputra, idimopoulos, claudiu.cristea, deepakkm...
larowlan’s picture

Version: 9.5.x-dev » 10.1.x-dev
Status: Fixed » Reviewed & tested by the community
Related issues: +#2972776: [policy, no patch] Better scoping for bug fix test coverage

Discussed whether this needs tests with @catch and @alexpott
Catch said if it were a task to use str_starts_with we wouldn't need it, so the only question is the extra if.
@alexpott wondered if this is resolved by #3324574: Deprecated function: strpos() in layout_builder_entity_view_alter

The consensus was probably doesn't need a test

larowlan’s picture

Version: 10.1.x-dev » 9.5.x-dev
Status: Reviewed & tested by the community » Fixed

crosspost, sorry

alexpott’s picture

Somehow this got unpublished

jmester13’s picture

Hey All,

I've got a instance where this merge request is failing upon Composer update and I'm wondering is anyone else is running into that issue. Below is the error I'm getting from composer, running on Acquia.

    https://git.drupalcode.org/project/drupal/-/merge_requests/2601.patch (3301770 - Passing null to parameter to strpos() is deprecated with PHP8.1)
   Could not apply patch! Skipping. The error was: Cannot apply patch https://git.drupalcode.org/project/drupal/-/merge_requests/2601.patch
In PluginManager.php(274) : eval()'d code line 331:
                                                                                                              
  Cannot apply patch 3301770 - Passing null to parameter to strpos() is deprecated with PHP8.1 (https://git.  
  drupalcode.org/project/drupal/-/merge_requests/2601.patch)!     

I'm just running composer install or composer update and getting this issue.

Thanks all.

claudiu.cristea’s picture

@jmester13, there are new releases for 9.5, 10.0 and 10.1. You don't need the patch anymore, just use the latest Drupal version.

jmester13’s picture

@claudiu.cristea

Thanks for the quick reply. I am already on 9.5.3.

I've ensured I don't have this patch in my composer file and have removed the composer.lock file and vendor directory.

I've actually tried removing the few core patches I had and am still getting this error.

Then I removed all my patches, vendor, composer.lock and removed the composer caches.

Still getting the error and seeing that the composer.lock that is generated contains the core patch.

Seems I have an oddity, any chance anyone else has been in this position. If not I'll try and check in with the folks at Acquia and see if it's something that is system related. But it almost seems that core is failing to patch itself or something?

Latest Composer log below.

Thanks all.

- Installing phpunit/phpunit (9.6.5): Extracting archive
- Applying patches for drupal/core
https://www.drupal.org/files/issues/2019-12-28/3082690-80.patch (3059955 - It is possible to overflow the number of items allowed in Media Library)
https://www.drupal.org/files/issues/2022-11-11/3222107-2.patch (3222107 - jQuery UI library order is incorrect when a large number of javascript files is loaded between two jQuery UI libraries)
https://git.drupalcode.org/project/drupal/-/merge_requests/2598.patch (3301692: Passing null to parameter to mb_strtolower() is deprecated with PHP 8.1)
https://git.drupalcode.org/project/drupal/-/merge_requests/3143.patch (3313342 - [PHP 8.1] Deprecated function: strpos(): Passing null to parameter #1 LayoutBuilderUiCacheContext.php on line 28)
Could not apply patch! Skipping. The error was: Cannot apply patch https://git.drupalcode.org/project/drupal/-/merge_requests/3143.patch

In Patches.php line 331:

Cannot apply patch 3313342 - [PHP 8.1] Deprecated function: strpos(): Passing null to parameter #1
LayoutBuilderUiCacheContext.php on line 28 (https://git.drupalcode.org/project/drupal/-/merge_req
uests/3143.patch)!

claudiu.cristea’s picture

@jmester13, latest is 9.5.5

acbramley’s picture

Removing credit for MR.

claudiu.cristea’s picture

Crediting @acbramley as he contributed with the review

Status: Fixed » Closed (fixed)

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