I am using entity_lookup plugin at the end of a pipeline. Some of my entries do not have a value.

What I'm finding is that even if I use skip_on_empty or any other process plugins NONE of them even bother to run because the error is tripped during the construct phase, before the pipeline has even had a chance to run.

So essentially this plugin tries to set itself up before it even knows if it will be needed or not.

I found the problem is triggerd in the query method. Fortunately I know if the value is empty at the start of the query so I can simply not build the query object in that case. So I am starting the query method in entity_lookup with the following:

    if (empty($value)) {
      return NULL;
    }

...and now everything in the pipeline works as expected.

Earlier in my debugging process I found that if I did not run parent::__construct that would get me further along, but the above code seems to be a much better for now in my case.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

Ryan Weal created an issue. See original summary.

mikeryan’s picture

Are you sure it's in the constructor? I don't see how the query() would be invoked there... That being said, I think transform() should pass through an empty value instead of calling query().

badrange’s picture

Version: 8.x-3.0-beta1 » 8.x-4.x-dev
Status: Active » Needs review
FileSize
628 bytes

Would this be one way to resolve this?

We started seeing this when I did some updates to our migration job, and this patch helps preventing blank terms from being created.

heddn’s picture

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

I hate to do this, but it also won't be hard to fix. Let's add a quick unit test of this. Setting back to NW for tests.

badrange’s picture

I have later found out that skip_on_empty sometimes gets skipped due to multiple being set when you have an empty array as a value: https://www.drupal.org/node/2905929

Thomas Cys’s picture

For PHP 8.1 this results in a deprecation message being thrown when you run a migration that uses entity_lookup with an empty value.

Deprecated: addcslashes(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/docroot/core/lib/Drupal/Core/Database/Connection.php on line 1483

Thomas Cys’s picture

Version: 8.x-4.x-dev » 8.x-5.3
Status: Needs work » Needs review
FileSize
816 bytes
danflanagan8’s picture

Title: entity_lookup plugin fails during construct if $value is empty » entity_lookup plugin fails if $value is empty
Version: 8.x-5.3 » 6.0.x-dev
Issue tags: -Needs tests
FileSize
887 bytes

Here's the little test requested in #4. I added a couple assertions to the existing Kernel test.

The first new assertion adds missing coverage for passing an empty array. I think we want to make sure not to modify that behavior. The second new assertion triggers the error reported in #6.

I've also updated the title slightly to remove mention of the constructor.

Status: Needs review » Needs work

The last submitted patch, 8: migrate_plus-lookup-2830058-8-FAIL.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

danflanagan8’s picture

Status: Needs work » Needs review
FileSize
1.66 KB
814 bytes

Failure looks perfect. Here's the fix from #7 on top of the fail patch.

My only concern here is whether empty is too strong here. Is there ever a reasonable scenario for passing zero? Or FALSE? I don't think so. The string 'false' is not empty, so that's not a problem.

I also did some testing locally without the patch and the query could not find a user with the name 0 as is. (That's a string zero.) So using empty is probably ok.