Problem/Motivation

When using PHP 8.1 - Deprecated function: trim(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\feeds\Plugin\Field\FieldType\FeedsItem->preSave() (line 132 of /modules/contrib/feeds/src/Plugin/Field/FieldType/FeedsItem.php)

Proposed resolution

Patch preSave() by null-coalescing the trim() argument to an empty string.

Remaining tasks

Convert to patch or merge request.

User interface changes

None.

API changes

None.

Data model changes

None.

Issue fork feeds-3267244

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:

Comments

jmcerda created an issue. See original summary.

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

megachriz’s picture

I think using is_string($this->guid) would fix the issue as well. See branch "3267244-pass-null-to-trim-deprecated".

tmaiochi’s picture

Assigned: Unassigned » tmaiochi

I'll review this!

tmaiochi’s picture

Assigned: tmaiochi » Unassigned
Status: Needs review » Reviewed & tested by the community

The code prevent the message error about trim function, and the error wasn't showed any more as far as I can test!

  • MegaChriz committed 2d2dfda on 8.x-3.x
    Issue #3267244 by jmcerda, MegaChriz, tmaiochi: Fixed passing null to...
megachriz’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for testing! Changes from 3267244-pass-null-to-trim-deprecated are merged.

Status: Fixed » Closed (fixed)

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

bwoods’s picture

Since upgrading to PHP 8.1, I've noticed a fair number of these pesky trim functions hiding out in the FieldTargetBase classes. At least in my situation, it seems that I'm hitting these because I have null values for some fields being passed, so I can run an isset check. But I guess I'm questioning whether there's a better/earlier way to do it if others are not seeing the same depreciation. Probably the most basic example is the Integer class. If we need a patch to fix all of these, I could start another ticket. But if I shouldn't actually have null values get this far, maybe I should change something in my API calls.

drupgirl’s picture

Version: 8.x-3.x-dev » 8.x-3.0-beta2

Sorry to reopen. The patch doesn't apply cleanly to 3.0-beta2.

megachriz’s picture

@drupgirl
That is because the patch is already committed.

@bwoods
It is possible that in files other than FeedsItem.php a similar issue exists. One new issue is opened importing date values: #3306211: Deprecated function: trim(): Passing null is deprecated as of PHP 8. To fix/catch these we need to have your particular case covered by an automated test. Tests are useful to prevent a bug from reappearing when making changes to the Feeds code base.

I think Feeds should be able to handle incoming null values well, so you should not have to change your API.

Whenever you encounter such an error, provide the feed type configuration file + the source file you were trying to import. Then I (or someone else) can write a test for that particular configuration and source data and have the bug covered.