Check if the field is empty, if it is then use another field(s) value or even a default value.
Rewrite plugin has the flexibility to use multiple replacement patters, custom string, or a mix of both. But it was lacking the conditional to do the rewrite only if the field values is empty or it is not present in the source.

Comments

Blanca.Esqueda created an issue. See original summary.

blanca.esqueda’s picture

Rewrite only if empty plugin patch

blanca.esqueda’s picture

Status: Active » Needs review
drupalaar’s picture

Thanks! Lifesaver!
Too bad it looks good, but does not work for me :-(

blanca.esqueda’s picture

@drupalar, why is not working?
Did you have an issue applying the patch? Did you check the checkbox option to do the replace only if the field is empty?

dobe’s picture

I like the idea. I am needing this in my install and not ready to get the feeds tamper php module out. I however am not satisfied with the logic in the patch in #2

I think it would be cleaner if we just wrapped the field in an if statement

if (empty($settings['only_if_empty']) || (!empty($settings['only_if_empty']) && empty($field))) {
  $field = strtr($settings['text'], $trans);
}

Don't have time to reroll this patch. Will do when I have a moment.

drupalaar’s picture

I checked the option "only if empty", but the rewrite is done on all imported nodes.

dobe’s picture

@drupalaar does this patch work? Or were you commenting on #5?

hartsak’s picture

The patch in #8 worked for me. Thanks!

My use case was a feeds user importer in which the source material has a lot of empty email addresses. I used the rewrite tamper plugin with patch #8 and I was able to generate a unique email address using tokens only if their email was empty. Nice!

blanca.esqueda’s picture

Hi @Dove,

You right, the patch needed some re-work on the logic.
But I think it would be more cleaner to have the if statement even before the foreach or anything else is done on the feeds_tamper_rewrite_callback.
It doesn't make sense to have the $trans array created and filled if it is not gonna be used.

Attached is the new patch.

@hartsak If you review it and it works maybe you can set the status as reviewed by the community.

Thanks!

hartsak’s picture

Status: Needs review » Reviewed & tested by the community

Patch #10 works fine for me! Thanks! I tried it again with email field.

jimmynash’s picture

Just chiming in. Patch in #10 worked for me as well.

Thanks!

Collins405’s picture

This is awesome! Lets get this committed!

matthieu_collet’s picture

Very usefull, thank you !

linl’s picture

Thanks! +1 to RTBC. Patch in #10 works well for me. Similar use case to #9, replacing empty email addresses.

agerard’s picture

Thanks! This was a lifesaver (or at least, brain-saver!). Hope it will be incorporated soon.

efrainh’s picture

I needed this too! Thank you!

kurt.j’s picture

Patch in #10 worked for my situation. Thanks Blanca.Esqueda!

steva1982’s picture

Great patch! I hope it will be committed in a stable release.

megachriz’s picture

Issue tags: +Needs tests

It would be great if this would have automated tests.

nwom’s picture

Status: Reviewed & tested by the community » Needs work

Setting to Needs Work as indicated in #20.

anybody’s picture

Thank you very much for this great feature. #10 works well!

proweb.ua’s picture

#10 works

kopeboy’s picture

Version: 7.x-1.x-dev » 8.x-2.x-dev
Status: Needs work » Reviewed & tested by the community

Please include this in a Drupal 10 release!
Relevant issue already exists: https://www.drupal.org/project/feeds_tamper/issues/3324724

kopeboy’s picture

Status: Reviewed & tested by the community » Needs work
kopeboy’s picture

Version: 8.x-2.x-dev » 7.x-1.x-dev
Status: Needs work » Reviewed & tested by the community
kopeboy’s picture

anybody’s picture

Assigned: blanca.esqueda » Unassigned
Status: Reviewed & tested by the community » Needs work

@kopeboy see #20. Still needs tests. If someone finishes them, this can be reviewed and RTBC'd afterward. Thanks!