Problem/Motivation

Using the feeds tamper to convert text to boolean, I set the "If No Match" to false, but blank fields in the CSV get imported as true.

The config for the tamper within the feed is:

      c4b26c8f-bf74-4ead-b53e-4084e464ed7f:
        true_value: 'y'
        false_value: 'n'
        match_case: false
        no_match_value: 'false'
        other_text: ''
        uuid: c4b26c8f-bf74-4ead-b53e-4084e464ed7f
        plugin: convert_boolean
        source: cataloged
        weight: -9
        label: 'Convert to Boolean'

Steps to reproduce

  1. Create a csv feed with a column to import as boolean that has blank items in it.
  2. Setup a feed using the tamper "convert to boolean" and set the "If No Match" value to false
  3. Import items. The blank items import as true instead of false.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#2 3280233-if-no-match-return-boolean.patch648 bytesmmaranao

Issue fork tamper-3280233

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

kwfinken created an issue. See original summary.

mmaranao’s picture

StatusFileSize
new648 bytes

I ran into this issue as well and basically, what's being returned is a string instead of an actual boolean if you set the no match to either true or false. Although, it's being saved and converted into a boolean value, the schema for the no_match_value type is a string.

I wrote the attached patch to fix this issue and it works for what I need. Hope it does for you too.

interlated’s picture

I ran into this and ended up with

return (int) filter_var($this->getSetting(self::SETTING_NO_MATCH), FILTER_VALIDATE_BOOLEAN);

As the converter.

Based on web/modules/contrib/feeds/src/Feeds/Target/Boolean.php returning an integer.

thomasmurphy’s picture

Version: 8.x-1.0-alpha2 » 8.x-1.x-dev
Priority: Major » Normal

I found a similar behaviour with core 11.1.6, feeds 3.0.0, tamper 2.0.0-beta4. Importing a csv value to a boolean, with the convert to boolean tamper, and the no match set to "null" doesn't behave that way. It's defaults to false instead.

I verified this by creating a test node with the "N/A" value set via the checkbox/radio buttons, and a test view renders the boolean value as blank. The only two outcomes with the tamper "convert to boolean" plug-in is default to true or false. For context I set a test csv value to "NULL" to verify that this wasn't a problem related to an empty string.

megachriz’s picture

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

If you can write a test that demonstrates the bug, that would be great! The test should be added to \Drupal\Tests\tamper\Unit\Plugin\Tamper\ConvertBooleanTest.

Since there is a patch, I set the issue status to "Needs work". "active" indicates that there's only a report and no existing code.

megachriz’s picture

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

After this issue was posted, some changes were made to the boolean plugin in #3322750: Some plugins do not implement submitConfigurationForm() correctly - Add UI tests.

I found one case where the plugin did not work as expected: when the "If no match" was set to "True". In this case the value was returned as is instead of a TRUE value. That's because PHP evaluates the following condition to true when $this->getSetting(self::SETTING_NO_MATCH) is TRUE:

$this->getSetting(self::SETTING_NO_MATCH) == 'pass'

And this resulted into the original value being returned instead of TRUE.

Changing this to

$this->getSetting(self::SETTING_NO_MATCH) === 'pass'

fixes that issue.

I have reworked the tests to find this bug. I believe the issue reported originally in this issue got fixed by the changes in #3322750: Some plugins do not implement submitConfigurationForm() correctly - Add UI tests.

@thomasmurphy
I could not reproduce your issue. When I set the "If no match" to "Null" and I import something that does not match the "Truth" or "False" setting, I see in the Feeds log that the item's field really equals null:

{"title":"Nothing","bool":null}

So I assume the value gets modified later in the process. Either by a FeedsTarget plugin or by the field that the data gets written to.

megachriz’s picture

Status: Needs review » Fixed

I tested this again manually and added some comments to the tests. I scheduled this to merge.

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

  • megachriz committed a7ba3e1d on 8.x-1.x
    Issue #3280233 by megachriz: Fixed set value to TRUE if there is no...

Status: Fixed » Closed (fixed)

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