Custom transformation commands
When the shipped transformation groups do not make the addresses you need, add a command of your own to a group in transform.ini. The example below reverses the text; a real command might, for example, replace brand names or drop certain words.
1. Write the command
A class with a public static method executeCommand( $text, $command, $charsetName ) that returns the transformed text. $command is the command's name, so one class can serve several commands; $charsetName is the charset of the text (usually utf-8).
<?php
// extension/myextension/transformation/myreverse.php
class MyReverse
{
public static function executeCommand( $text, $command, $charsetName )
{
return strrev( $text );
}
}
2. Register it
In [Extensions] Commands[], as path to the file, a colon, and the class name. The path is relative to the installation root.
# extension/myextension/settings/transform.ini.append.php [Extensions] Commands[my_reverse]=extension/myextension/transformation/myreverse.php:MyReverse
3. Add it to a group
Add the command to the group in use (site.ini [URLTranslator] TransformationGroup), where in the list it should run. A line Commands[]=my_reverse alone appends it at the end. To put it elsewhere, start with an empty Commands[], which empties the list, and repeat the shipped commands in the order you want:
[urlalias] Commands[] Commands[]=normalize Commands[]=transform Commands[]=decompose Commands[]=transliterate Commands[]=diacritical Commands[]=lowercase Commands[]=url_cleanup Commands[]=my_reverse
4. Clear the caches
The transformations are compiled into PHP code and cached. Clear the INI cache and the character transformation cache:
php bin/php/ezcache.php --clear-tag=ini php bin/php/ezcache.php --clear-id=chartrans
New and republished objects get addresses made with the command. To rebuild all aliases, run ./console exp:updateniceurls --update-nodes --allow-root-user.
Limits
- A file that does not exist at the registered path is reported in the debug output, and the command is skipped.
- Run your command before url_cleanup if it may produce characters that are not allowed in addresses; after it, nothing cleans them up.
Contributors
- Julia Shymova
- Geir Arne Waaler
- Xavier dutoit Commenter
- Pike Commenter
- Graham Brookins Updated Maintainer
Comments (2 historical)
Written by readers of the earlier documentation sites, kept as they were.
Could you confirm it handles the collisions ?
Xavier dutoit ·
Hi,
What happends if my transformation generates an already used url ? is the _1, _2 suffix added later in the chain ?
X+
order and inheritance
pike ·
thanks. may I suggest to address the following in the above documentation:
- in the example, in which order would the commands be executed ?
- could you present the example using transform.ini.append.php instead of transform.ini (do you still need to add "url_cleanup_iri" to the commands - wouldnt that be inherited from the global "transform.ini" ?)
$2c,
*-pike