Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
trim_replace_whitespace_with_single_space.inc_.patch | 925 bytes | WorldFallz | |
Comments
Comment #1
twistor CreditAttribution: twistor commentedThanks, 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.
Comment #2
WorldFallz CreditAttribution: WorldFallz commentedThat'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.
Comment #3
twistor CreditAttribution: twistor commentedI understand. I have some ideas about creating groups of plugins that are reusable across fields/importers that should help with that more generically.
Comment #4
NWOM CreditAttribution: NWOM commentedThank you very much for the patch. It solved my issue when importing data from a CRM that had 100s of entries with double spaces.