You can use Token filter which will add a Text format filter for token replacement. They you simply need to make sure the Token filter will run before the Shortcode one. More specifically:
- Install Token filter
- Go to
admin/config/content/formats
- Click "Configure" for the text format you are using.
- Enable the Token replacement and the Shortcodes filters under "Enabled Filters"
- Under "Filter processing order" make sure that Token replacement precedes Shortcodes
This will ensure that the tokens will be replaced before the shortcodes are parsed and will therefore work as expected.
Bonus:
In case you are using any sort of Visual Shortcodes, adding [
or ]
as content will most likely confuse the editor (in my case they were replaced with |LS|
and |RS|
respectively).
In that case you can set up a pre-render hook where you can change back those specific strings (or any other you might use to wrap tokens with), e.g. for blocks it could be something like that (for Drupal 8)
/**
* Implements hook_entity_view_alter().
*/
function my_module_entity_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
$build['#pre_render'][] = 'my_module_replace_token_wrappers';
}
function my_module_replace_token_wrappers(array $build) {
// Get the body
$body = &$build['content']['body'][0]['#text'];
// Replace the values of |LS|/|RS| with [\] respectively
$body = str_replace('|LS|', '[', $body);
$body = str_replace('|RS|', ']', $body);
}