You are here:  » Feed with two image fields, one high res and one low res


Feed with two image fields, one high res and one low res

Submitted by tobyhage on Wed, 2017-07-12 14:45 in

Hello David,

I have a question about a feed which have two image fields.
For easy reference we call them image1 and image2.

image1 contains always an url, but is low res image.
image2 contains not always an url but is high res image. There is also a boolean field in the feed which tells us that image2 contains an url. The latter is maybe handy for you to create a solution?!

How can i import image1, when image2 doesn't contain an url (or boolean field is false).

Hopefully you understand what i mean...

You may change the subject of this topic to make it easier to find for other people.

Submitted by support on Wed, 2017-07-12 15:11

Hi Toby,

This could be done with a small modification to the Search and Replace RegExp filter documented in this comment.

Search and Replace RegExp allows matching an empty value using /^$/ as the Search parameter, and by passing the Replace parameter through the same function that swaps out the %FIELD% placeholders for the Text Before / Text After filters that should do the trick.

To give this a go, add the following code to the end of includes/filter.php, just before the closing PHP tag:

  /*************************************************/
  /* searchReplaceRegExp */
  /*************************************************/
  $filter_names["searchReplaceRegExp"] = "Search and Replace (RegExp)";
  function filter_searchReplaceRegExpConfigure($filter_data)
  {
    widget_textBox("Search","search",TRUE,$filter_data["search"],"",3);
    widget_textBox("Replace","replace",FALSE,$filter_data["replace"],"",3);
  }
  function filter_searchReplaceRegExpValidate($filter_data)
  {
    if (!$filter_data["search"])
    {
      widget_errorSet("search","required field");
    }
  }
  function filter_searchReplaceRegExpExec($filter_data,$text)
  {
    $filter_data["replace"] = filter_recordPlaceholders($filter_data["replace"]);
    return trim(preg_replace($filter_data["search"],$filter_data["replace"],$text));
  }

With that in place, register image2 as the Image URL field. Then from the /admin/ home page, click Filters alongside the feed and add a new Search and Replace RegExp filter to the Image URL field configured as follows;

Search:

/^$/

Replace:

%image1%

...and re-import the feed. With this in place, if there is a value in image2 then it will be used as the Image URL, otherwise the Search and Replace RegExp will match the empty value by way of the ^ (beginning) and $ (end) anchors (the "/" characters are the delimiters of the regular expression) and replace with the value of the image1 field...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by tobyhage on Wed, 2017-07-12 19:53

David,

Thank you very much! The filter functionality is very powerfull :-)