0

I have a set of HTML pages that I'm importing (regularly) into a Drupal site using Feeds and Feeds Tamper.

One of the elements in the feed can take one of two forms:

Birth 1869 Location Merthyr Tydfil, Glamorgan, Wales<a class="gs-pl" href="https://maps.google.com/?t=m&amp;q=51.7479,-3.37779" target="_top" title="Google">G</a>

or

Marriage Calculated 1894 (i.e. without a Location section).

The feed contains an unlimited number of these elements which will be imported into a multivalued Field Collection attached to a node.

I need to extract part of the Location section (a latitude) or if there is no Location provide a default value of 0.

I'm using the Find Replace Regex plugin to extract the latitude and add a pipe character (to allow me to Explode the results into an array).

My find regex currently looks like:

.*?Location.*?"gs-pl".*;q=(.*?),(.*?)".*|.*

which either identifies the latitude or returns the full string if there is no Location element.

My replace regex looks like:

\1| which works fine for the case where the Location element exists.

However, I can't see how to replace any empty string returned (i.e. when the capturing group is emtpy) with a figure 0.

Is this possible using the Find Replace Regex plugin, on its own or in combinations of other tampers as well? I wondered if I could process the original element to add a Location element with the values required as a default if it was missing but that still involves a conditional replacement so it's essentially the same problem.

I can't set insert missing Location elements in the HTML before the import, and setting the default value in the String Collection doesn't work because of the way field collections are imported (basically the method I'm using puts the data in the wrong field collection item if there are any empty fields)

1

You can add a Tamper plugin of type "Set value or default value". Set "Value" to "0" and enable the "Only if empty" option (see image below). This way, the value "0" will be set only if the "Find replace REGEX" returned an empty result.

I tested with your example data if this would work and in my case it did. Birth 1869 Location Merthyr Tydfil, Glamorgan, Wales<a class="gs-pl" href="https://maps.google.com/?t=m&amp;q=51.7479,-3.37779" target="_top" title="Google">G</a> resulted into 51.7479 and Marriage Calculated 1894 resulted into 0.

Set value or default value example

| improve this answer | |
  • That's certainly doing something because the import now fails. I've put the 'Set value or default value' after the extraction of the latitude and before the pipe is added to enable the Eplode plugin to work. My error is "Cannot construct Point. x and y should be numeric" which suggests GeoField can't cope with the data it's receiving. I need to do more work on this but this must be the right solution... – ColeValleyGirl May 29 '17 at 16:01
  • I believe the problem is in the Regex I'm using to extract the co-ordinates from the parsed text -- will work on that and then come back to confirm this is defitely the right answer. – ColeValleyGirl May 29 '17 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.