Tamper
Generic plugin system for transforming and modifying data during import or processing operations.
tamper
Overview
Tamper is a generic plugin system designed to transform and modify data. It provides a comprehensive set of data transformation plugins that can be chained together to manipulate values during data processing operations.
The module serves as a foundation for data transformation workflows, most commonly used in conjunction with the Feeds Tamper module for data imports. Tamper provides over 40 built-in plugins covering text manipulation, type conversion, array operations, date/time formatting, filtering, encoding/decoding, and more.
Each Tamper plugin accepts an input value, applies its specific transformation logic, and returns an output value. Plugins can be configured individually and chained together for complex data processing pipelines. The plugin architecture supports both legacy annotations and modern PHP 8 attributes for plugin discovery.
Features
- Provides 41+ built-in data transformation plugins organized by category (Text, HTML, List, Number, Date/time, Filter, Other)
- Plugin-based architecture using Drupal's plugin system with support for both PHP 8 attributes and legacy annotations
- Text manipulation plugins including case conversion, find/replace, regex replacement, trimming, truncation, URL encoding/decoding, HTML entity encoding/decoding, transliteration, and strip tags
- Array/list operations including explode (split string to array), implode (join array to string), unique values, and array filtering
- Number operations including math calculations (add, subtract, multiply, divide), number formatting, and aggregation functions (min, max, sum, average, median, count)
- Date/time conversion between Unix timestamps and formatted date strings with custom format support
- Encoding/decoding support for JSON, PHP serialize, Base64, and YAML formats
- Advanced features like Twig template processing, token-based rewriting, entity finder by field value, and value copying between sources
- Filtering plugins to skip processing based on empty values, required fields, or keyword matching
- Geographic utilities for converting country names to ISO codes and US state names to abbreviations
- Exception handling system with SkipTamperDataException and SkipTamperItemException for conditional processing flow control
- ItemUsage system to specify whether plugins require, optionally use, or ignore the tamperable item context
Use Cases
Data Import Cleanup
When importing data from external sources (CSV, XML, JSON), use Tamper plugins to clean and normalize the data. For example, trim whitespace from text fields, convert dates to proper formats, or standardize country names to ISO codes.
Text Transformation Pipeline
Chain multiple Tamper plugins to perform complex text transformations: first strip HTML tags, then convert to lowercase, then trim whitespace, and finally truncate to a maximum length.
Entity Reference Resolution
Use the Entity Finder plugin to convert imported text values (like usernames or taxonomy term names) into entity IDs that can be used for entity reference fields.
Data Validation and Filtering
Use the Required and Keyword Filter plugins to skip importing items that don't meet certain criteria, such as missing required fields or containing unwanted keywords.
Boolean Field Mapping
Convert various text representations of boolean values (yes/no, true/false, 1/0, on/off) to proper boolean values for checkbox fields.
URL Processing
Use the Absolute URL plugin to convert relative URLs in imported HTML content to absolute URLs, ensuring links work correctly after import.
Custom Value Generation
Use the Twig or Rewrite plugins to generate computed values from multiple source fields, such as creating a full name from first and last name fields or generating slugs from titles.
Date Format Standardization
Convert various date string formats from external sources to Unix timestamps or ISO format dates using strtotime and timetodate plugins.
Tips
- Chain Tamper plugins in order of operation - earlier plugins' output becomes later plugins' input
- Use the Skip on Empty plugin early in your chain to avoid processing empty values
- The Entity Finder plugin requires proper entity type and field configuration to work correctly
- For complex transformations, consider using the Twig plugin which provides full access to all source data
- When using Find Replace REGEX, test your patterns carefully and consider using the limit option to prevent runaway replacements
- The Copy plugin requires the tamperable item context and is useful for populating one field from another
- Aggregate functions (min, max, sum, etc.) expect array input - use Explode first if your source is a string
- Use Encode/Decode plugins to work with serialized data from external sources
Technical Details
Hooks 1
hook_tamper_info_alter
Allows modules to alter the Tamper plugin definitions discovered by the plugin manager.
Troubleshooting 5
Ensure the data being passed to the plugin is of the expected type. Many text manipulation plugins expect string input. Use type conversion plugins like Cast to Integer first if needed, or check your data source.
Verify the entity type, bundle, and field configuration. Check that the field column is correctly specified for complex field types. Ensure the search value exactly matches the stored value.
This is expected behavior for filter plugins like Required and Keyword Filter. Review your filter configuration to ensure the criteria match your intentions.
Ensure the regex pattern includes delimiters (e.g., /pattern/) and is valid PCRE syntax. Test patterns using PHP's preg_replace function separately.
For strtotime, ensure the input date string is in a recognizable English format. Consider using the custom date format option for non-standard formats. Be aware of timezone implications.
Security Notes 3
- The Twig plugin executes Twig templates which could potentially expose sensitive data if not configured properly
- Regex patterns in Find Replace REGEX should be validated to prevent ReDoS (Regular Expression Denial of Service) attacks
- The Entity Finder plugin bypasses access checks (uses accessCheck(FALSE)) when querying entities - be aware of this when processing sensitive data