PHP 8.1 deprecation warning preg_split

Created on 24 November 2022, over 1 year ago
Updated 12 February 2023, over 1 year ago

The search page shows a warning with deprecated function

Deprecated function: preg_split(): Passing null to parameter #2 ($subject) of type string is deprecated in Drupal\search_api\Plugin\search_api\processor\Highlight->highlightField() (line 659 of modules/contrib/search_api/src/Plugin/search_api/processor/Highlight.php).

Steps to reproduce

Search API + Solr + Metatag
I have configured the search view to look into multiple fields (content + keywords & description metatag fields). both metatag fields are set as a fulltext search index.
Searching for a word in the keywords field of an item that has an empty description metatag throws the above warning.

Proposed resolution

The highlightFields method in \Plugin\search_api\processor\Highlight.php should do a check on the field value before calling the highlightField method on the field value.

Remaining tasks

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Plugins

Created by

πŸ‡·πŸ‡΄Romania bogdan.dinu

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

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

    Makes sense, thanks for posting!
    Committed.

  • Status changed to Needs work over 1 year ago
  • πŸ‡©πŸ‡ͺGermany mkalkbrenner πŸ‡©πŸ‡ͺ

    Since the commit of this patch, the search result highlighting integration tests of search_api_solr are failing.

  • πŸ‡©πŸ‡ͺGermany mkalkbrenner πŸ‡©πŸ‡ͺ

    Undefined array key "manu"
    in
    /home/runner/work/search_api_solr/search_api_solr/drupal/modules/contrib/search_api_solr/tests/src/Kernel/SearchApiSolrTechproductsTest.php:54

        // Test processor based highlighting.
        $query = $this->buildSearch('Technology', [], ['manu']);
        $results = $query->execute();
        $this->assertEquals(1, $results->getResultCount(), 'Search for Β»TechnologyΒ« returned correct number of results.');
        /** @var \Drupal\search_api\Item\ItemInterface $result */
        foreach ($results as $result) {
          $this->assertStringContainsString('<strong>Technology</strong>', (string) $result->getExtraData('highlighted_fields', ['manu' => ['']])['manu'][0]);
          $this->assertEmpty($result->getExtraData('highlighted_keys', []));
          $this->assertEquals('… A-DATA <strong>Technology</strong> Inc. …', $result->getExcerpt());
        }
    
  • πŸ‡©πŸ‡ͺGermany mkalkbrenner πŸ‡©πŸ‡ͺ

    I just verified that reverting the patch lets the test pass again and "Technology" is highlighted again.

  • Status changed to Needs review over 1 year ago
  • πŸ‡©πŸ‡ͺGermany mkalkbrenner πŸ‡©πŸ‡ͺ

    In case of "foreign" indexes like Search API Solr Document datasources or Drupal multisite searches, the value could be NULL. In this case the backend is responsible to modify the highlighted_fields extra data later if it allows to use the highlight processor.
    Therefore, we must not remove the information about the field that "should" be highlighted at this point. Converting the value into an empty string is the backward compatible behaviour of this processor.

  • Status changed to Fixed over 1 year ago
  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    Thanks for noticing and posting a patch, and sorry for breaking your stuff again. ;)
    Patch looks good – committed.
    Thanks again!

  • Status changed to Needs review over 1 year ago
  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    Whelp, spoke too soon. Problem is actually that $value can be an object of type \Drupal\search_api\Plugin\search_api\data_type\value\TextValue at this point. While preg_split() doesn’t seem to have any problems accepting that as it’s second parameter (for some reason), it of course fails the is_string() check, letting its value be ignored.
    Instead of all the above, let’s just explicitly cast $text to a string in highlightField(), which should also handle NULL values correctly.

    Patch attached (plus interdiff compared to version before any of the patches here was committed), please test/review!

  • Status changed to Fixed over 1 year ago
  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    Seems to work, so: committed.

  • πŸ‡ΊπŸ‡ΈUnited States ghalusa

    - Installing drupal/search_api (1.28.0): Extracting archive
    - Applying patches for drupal/search_api
    https://www.drupal.org/files/issues/2023-02-11/3323594-13--fix_bc_break_... β†’ (3323594: PHP 8.1 deprecation warning preg_split)
    Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2023-02-11/3323594-13--fix_bc_break_... β†’

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

Production build 0.69.0 2024