The "explode" plugin (and possibly others) will convert the imported field data from a string into an array. You may need to use other plugins to further process the data in this field. In my case I want to explode a simple list of values and then trim each individual value to strip off leading or trailing white space. For this to work, the "trim" plugin needs to be aware that the data to be trimmed is an array and not just a string. The feeds tamper module keeps track of the array-ness of field data so that it can appropriately process the field for each plugin.

With the latest update, Feeds Tamper checks to see if a field is an array or not when the line of data is initially parsed, but does not re-check the data after a plugin executes. In other words, if the field value is a string initially, but is changed to an array via one of the plugins -- such as "explode" -- then subsequent plugins will still think the field is a string. This leads to errors like:

Warning: trim() expects parameter 1 to be string, array given in feeds_tamper_trim_callback() (line 38 ...

Assuming you already have a Feeds import setup, here are steps to reproduce the error:

  1. In the source file for one of your fields, add some simple comma-separated data (e.g. "a, b, c").
  2. In Feeds Tamper, add the "explode" plugin and set the separator to "," (comma).
  3. After the "explode" plugin, add the "trim" plugin to the same field. The default options for "trim" are fine.
  4. Save your Feeds Tamper changes and run the import.
  5. The imported field will be blank and the Watchdog logs should show errors like the one listed above.

I believe the root problem is that line 49

$is_array = is_array($result->items[$item_key][$element_key]);

needs to be moved into the loop below it (line 51)

      foreach ($instances as $instance) {

This would allow the field data to be re-evaluated after each plugin has finished processing the data.

Patch to follow soon.

Comments

illeace’s picture

StatusFileSize
new906 bytes

Here's my patch file that moves the $is_array check into the next loop so it is checked prior to each plugin being executed. I've tested it for my use case only, not for other plugins. If there was a good reason for moving the $is_array check in the latest build, then a more complete bug fix than this will be needed.

tedbow’s picture

Status: Active » Reviewed & tested by the community

@illeace, nice catch.

I just found this same problem. I was trying to explode a list of terms and then trim them.

This patch works find for me also.

tedbow’s picture

Just another note on the importance of this patch. Because plugin can change value from string -> array and array -> string then it the is_array check really needs to be done before each plugin is called.

The "implode" plugin would be an example of a plugin that converts array -> string direction.

Without this patch you can't use any of the string functions after you have run Explode. I think this would be very common use case, where you have a string containing multiple values that need to be split up and then individually processed.

5n00py’s picture

It should be commited!

  • twistor committed afd7720 on 7.x-1.x authored by illeace
    Issue #2119745 by illeace: Fixed Fields converted to arrays during...
twistor’s picture

Version: 7.x-1.0-beta5 » 6.x-1.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Oy. Good catch.

twistor’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Status: Patch (to be ported) » Fixed

This is done differently in 6.x.

Status: Fixed » Closed (fixed)

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