PHP 8.1 Deprecated Function

Created on 6 September 2022, almost 2 years ago
Updated 28 October 2023, 8 months ago

Problem/Motivation

Deprecated function: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in Drupal\search_api\Utility\Utility::splitPropertyPath() (line 142 of modules/contrib/search_api/src/Utility/Utility.php).

Steps to reproduce

Use PHP8.1 with the latest search_api

Proposed resolution

Patch supplied by Michele Bertani on issue 3253738: https://www.drupal.org/files/issues/2022-08-17/searchapi-php81.patch
https://www.drupal.org/project/search_api/issues/3253738#comment-14655392

Remaining tasks

Incorporate patch into release

🐛 Bug report
Status

Fixed

Version

1.0

Component

General code

Created by

🇬🇧United Kingdom indefinitedevil

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇹Austria drunken monkey Vienna, Austria

    Is anyone running into this able to provide a backtrace?

  • 🇧🇷Brazil kleiton_rodrigues Americana/SP

    The patch #2 looks good!
    +1 RTBC

  • 🇦🇹Austria drunken monkey Vienna, Austria

    As said, please provide a backtrace so we can get to the real root cause here. Just masking the problem is usually not the wisest approach.

  • Status changed to Postponed: needs info over 1 year ago
  • 🇦🇹Austria drunken monkey Vienna, Austria
  • Status changed to Needs work 9 months ago
  • @drunken monkey hope this helps. What I've noticed anytime the cache is cleared this error pops up.

    Deprecated function: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in Drupal\search_api\Utility\Utility::splitPropertyPath() (line 142 of modules/contrib/search_api/src/Utility/Utility.php).
    
    Drupal\search_api\Utility\Utility::splitPropertyPath(NULL, ) (Line: 355)
    Drupal\search_api\Utility\FieldsHelper->retrieveNestedProperty(Array, NULL) (Line: 478)
    Drupal\search_api\Item\Field->getDataDefinition() (Line: 201)
    _search_api_views_get_handlers(Object) (Line: 61)
    search_api_views_data() (Line: 236)
    Drupal\views\ViewsData->Drupal\views\{closure}(Object, 'search_api') (Line: 405)
    Drupal\Core\Extension\ModuleHandler->invokeAllWith('views_data', Object) (Line: 244)
    Drupal\views\ViewsData->getData() (Line: 154)
    Drupal\views\ViewsData->get('search_api_index_dev_content_index') (Line: 85)
    Drupal\views\Plugin\ViewsHandlerManager->getHandler(Array, NULL) (Line: 888)
    Drupal\views\Plugin\views\display\DisplayPluginBase->getHandlers('field') (Line: 1045)
    Drupal\views\ViewExecutable->_initHandler('field', Array) (Line: 903)
    Drupal\views\ViewExecutable->initHandlers() (Line: 2318)
    Drupal\views\Plugin\views\display\DisplayPluginBase->preExecute() (Line: 1697)
    Drupal\views\ViewExecutable->preExecute(Array) (Line: 1632)
    Drupal\views\ViewExecutable->executeDisplay('page_1', Array) (Line: 81)
    Drupal\views\Element\View::preRenderViewElement(Array)
    call_user_func_array(Array, Array) (Line: 101)
    Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 788)
    Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 374)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 242)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 580)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 243)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 174)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
    
  • So looking a bit into it, right after the php error search API I also get:

    Drupal\search_api\SearchApiException while adding Views handlers for field on index Content index dev: Could not retrieve data definition for field '' on index 'Content index dev'. in Drupal\search_api\Item\Field->getDataDefinition() (line 482 of /var/www/html/web/modules/contrib/search_api/src/Item/Field.php).

    Now the weird part is I have no clue where I'm getting a field with an empty string label.

  • Okay, so even more digging it looks like this happens with 3 special fields search_api_id, search_api_language, and search_api_datasource and it could potentially happen because of the backend I'm using Search API Meilisearch. But I'm not sure what the original issue was using, so hard to tell if it's down to the backend.

  • Status changed to Needs review 9 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & sqlite-3.27
    last update 9 months ago
    467 pass, 70 fail
  • 🇮🇳India sarwan

    Hello @indefinitedevil,
    I have fixed this issue PHP 8.1 Deprecated Function , some other issues resolve and also created patch,
    please review and verify.

  • The last submitted patch, 14: search_api-3308184.patch, failed testing. View results
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Status changed to Needs work 9 months ago
  • @sarwan_verma So for some reason the tests are failing, but that said the specific fix meant for this issue is the same as the original patch file, and that does work. But the maintainer wants to fix this problem at the root cause, method docs requires that parameter to already be a string and not NULL.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & sqlite-3.27
    last update 9 months ago
    Composer require-dev failure
  • @sarwan_verma opened merge request.
  • So the NULL check is not a good solution, if you just do that you'll still get this in the dblog:
    Drupal\search_api\SearchApiException while adding Views handlers for field on index Content index dev: Could not retrieve data definition for field '' on index 'Content index dev'. in Drupal\search_api\Item\Field->getDataDefinition() (line 482 of /var/www/html/web/modules/contrib/search_api/src/Item/Field.php).

    While it fixes the depreciation error, you are still getting a SearchApiException that is correctly handled and logged.

    Here I've made a small change in the search_api.views.inc file at least for my case it fixes it, could someone please test the patch file? Thanks.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & sqlite-3.27
    last update 9 months ago
    Patch Failed to Apply
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 9 months ago
    Patch Failed to Apply
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 9 months ago
    543 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & sqlite-3.27
    last update 9 months ago
    544 pass
  • Status changed to Needs review 9 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & sqlite-3.27
    last update 9 months ago
    545 pass
  • 🇦🇹Austria drunken monkey Vienna, Austria

    Thanks for the backtrace and the additional information!
    The problem indeed seems to come from the Meilisearch backend. I don’t think it’s smart to include those special fields there, since it might confuse users.
    That being said, it seems search_api_views_data() already guards against fields with no property path, and _search_api_views_get_handlers() just fails to do the same. So I think this is still also a bug in this module.

    The attached patch should fix it, please test/review! It also adds a check in \Drupal\search_api\Item\Field::getDataDefinition() to avoid calling retrieveNestedProperty() with a NULL property path, which should help debug if this problem ever arises again.

    Note: The patch requires PHP 8.0, but that should really be required at this point. (It apparently even is for Drupal 9, without me noticing it.)

  • Status changed to RTBC 8 months ago
  • The provided patch fixes the deprecation issue, so this can probably be merged. So now with the meilisearch, this error pops up since the special fields are indexed there, so I guess the added debugging code also works:

    Drupal\search_api\SearchApiException while adding Views handlers for field on index Content index: Field 'search_api_language' on index 'Content index' has no property path set. in Drupal\search_api\Item\Field->getDataDefinition() (line 479 of /var/www/html/web/modules/custom/search_api/src/Item/Field.php).
    

    I will be opening an issue on the Search API Meilisearch module about this, but I'm not sure if is it ok to index these fields or not. If it's ok what would be the correct approach?

    That said I'm setting this issue as RTBC.

  • Status changed to Fixed 8 months ago
  • 🇦🇹Austria drunken monkey Vienna, Austria

    Good to hear, thanks for testing!
    Merged. Thanks again!

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

Production build 0.69.0 2024