I had an importer with a lot of string fields and I found myself adding both the "Trim" and "Find replace REGEX" (configured to replace all whitespace with a single space char) plugins to every. single. text. field. For anything more than a handful of text fields this gets old fast. So i created a single plugin that does both.

I'm posting it here for the benefit of others (rename it to remove the '_.patch' added by drupal.org and put it in the 'plugins' directory).

/**
 * @file
 * Trim both sides and replace all whitespace with a single space char.
 */

$plugin = array(
  'form'     => 'feeds_tamper_trim_replace_whitespace_with_single_space_form',
  'callback' => 'feeds_tamper_trim_replace_whitespace_with_single_space_callback',
  'name'     => 'Trim & replace whitespace with a single space',
  'multi'    => 'direct',
  'category' => 'Text',
);

function feeds_tamper_trim_replace_whitespace_with_single_space_form($importer, $element_key, $settings) {
  $form = array();
  $form['help'] = array(
    '#value' => t('This plugin is a combination of the "Trim" plugin and "Find and replace REGEX" plugin configured to replace all whitespace with a single space.'),
  );
  return $form;
}

function feeds_tamper_trim_replace_whitespace_with_single_space_callback($source, $item_key, $element_key, &$field, $settings) {
  $field = preg_replace('/\s{2,}/u' , ' ', trim($field));
}

Feel fee to add it to the module if you like-- seems a very common need for text fields imo.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

twistor’s picture

Status: Needs review » Closed (works as designed)

Thanks, but Feeds Tamper can't provide every plugin for everything. There's actually some intricacies here around unicode. Feeds Tamper provides a plugin API, so this can be added simply enough.

WorldFallz’s picture

That's fine. It's just when you have to do it 50 times it's gets little old, lol. It's here if anyone else needs.

twistor’s picture

I understand. I have some ideas about creating groups of plugins that are reusable across fields/importers that should help with that more generically.

NWOM’s picture

Thank you very much for the patch. It solved my issue when importing data from a CRM that had 100s of entries with double spaces.