Twig can be explanded in many ways, you can add extra tags, filters, tests, oprators, global variables, and functions. You can event extend the parser itself with node visitors.
I am going to show you how to create new custom twig filter in drupal. For example we are going to create a filter to remove a numbers from string, will explain with hello_world module.
Create hello_world folder in modules/custom/folder with following files,
hello_world.info.ymlit would contains normal module.info.ymlfile values. Check here for more detailes.hello_world.services.ymlit would contain following line
services:
hello_world.twig_extension:
arguments: ['@renderer']
class: Drupal\hello_world\TwigExtension\RemoveNumbers
tags:
- { name: twig.extension }
src/TwigExtension/RemoveNumbers.phpit would contain following in that.
namespace Drupal\hello_world\TwigExtension;
class RemoveNumbers extends \Twig_Extension {
/**
* Generates a list of all Twig filters that this extension defines.
*/
public function getFilters() {
return [
new \Twig_SimpleFilter('removenum', array($this, 'removeNumbers')),
];
}
/**
* Gets a unique identifier for this Twig extension.
*/
public function getName() {
return 'hello_world.twig_extension';
}
/**
* Replaces all numbers from the string.
*/
public static function removeNumbers($string) {
return preg_replace('#[0-9]*#', '', $string);
}
}
Enable hello_world module and clear the cache, then you could use the removenum filters in your twig file.
``
It would remove the all numbers from the string, enjoy with your custom filters!