class TestExtension

Same name in this branch
  1. 10 core/tests/Drupal/Tests/Core/Extension/ExtensionListTest.php \Drupal\Tests\Core\Extension\TestExtension
Same name in other branches
  1. 9 core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php \Drupal\twig_extension_test\TwigExtension\TestExtension
  2. 9 core/tests/Drupal/Tests/Core/Extension/ExtensionListTest.php \Drupal\Tests\Core\Extension\TestExtension
  3. 8.9.x core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php \Drupal\twig_extension_test\TwigExtension\TestExtension
  4. 8.9.x core/tests/Drupal/Tests/Core/Extension/ExtensionListTest.php \Drupal\Tests\Core\Extension\TestExtension
  5. 11.x core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php \Drupal\twig_extension_test\TwigExtension\TestExtension
  6. 11.x core/tests/Drupal/Tests/Core/Extension/ExtensionListTest.php \Drupal\Tests\Core\Extension\TestExtension

A test Twig extension that adds a custom function and a custom filter.

Hierarchy

  • class \Drupal\twig_extension_test\TwigExtension\TestExtension extends \Twig\Extension\AbstractExtension

Expanded class hierarchy of TestExtension

1 file declares its use of TestExtension
TwigExtensionTest.php in core/modules/system/tests/src/Functional/Theme/TwigExtensionTest.php
1 string reference to 'TestExtension'
twig_extension_test.services.yml in core/modules/system/tests/modules/twig_extension_test/twig_extension_test.services.yml
core/modules/system/tests/modules/twig_extension_test/twig_extension_test.services.yml
1 service uses TestExtension
twig_extension_test.twig.test_extension in core/modules/system/tests/modules/twig_extension_test/twig_extension_test.services.yml
Drupal\twig_extension_test\TwigExtension\TestExtension

File

core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php, line 12

Namespace

Drupal\twig_extension_test\TwigExtension
View source
class TestExtension extends AbstractExtension {
    
    /**
     * Generates a list of all Twig functions that this extension defines.
     *
     * @return array
     *   A key/value array that defines custom Twig functions. The key denotes the
     *   function name used in the tag, e.g.:
     *   @code
     *   {{ custom_function() }}
     *   @endcode
     *
     *   The value is a standard PHP callback that defines what the function does.
     */
    public function getFunctions() {
        return [
            'custom_function' => new TwigFunction('custom_function', [
                'Drupal\\twig_extension_test\\TwigExtension\\TestExtension',
                'testFunction',
            ]),
        ];
    }
    
    /**
     * Generates a list of all Twig filters that this extension defines.
     *
     * @return array
     *   A key/value array that defines custom Twig filters. The key denotes the
     *   filter name used in the tag, e.g.:
     *   @code
     *   {{ foo|test_filter }}
     *   @endcode
     *
     *   The value is a standard PHP callback that defines what the filter does.
     */
    public function getFilters() {
        return [
            'test_filter' => new TwigFilter('test_filter', [
                'Drupal\\twig_extension_test\\TwigExtension\\TestExtension',
                'testFilter',
            ]),
        ];
    }
    
    /**
     * Gets a unique identifier for this Twig extension.
     *
     * @return string
     *   A unique identifier for this Twig extension.
     */
    public function getName() {
        return 'twig_extension_test.test_extension';
    }
    
    /**
     * Outputs either an uppercase or lowercase test phrase.
     *
     * The function generates either an uppercase or lowercase version of the
     * phrase "The quick brown fox jumps over the lazy dog 123.", depending on
     * whether or not the $upperCase parameter evaluates to TRUE. If $upperCase
     * evaluates to TRUE, the result will be uppercase, and if it evaluates to
     * FALSE, the result will be lowercase.
     *
     * @param bool $upperCase
     *   (optional) Whether the result is uppercase (true) or lowercase (false).
     *
     * @return string
     *   The generated string.
     *
     * @see \Drupal\system\Tests\Theme\TwigExtensionTest::testTwigExtensionFunction()
     */
    public static function testFunction($upperCase = FALSE) {
        $string = "The quick brown box jumps over the lazy dog 123.";
        if ($upperCase == TRUE) {
            return strtoupper($string);
        }
        else {
            return strtolower($string);
        }
    }
    
    /**
     * Replaces all instances of "animal" in a string with "plant".
     *
     * @param string $string
     *   The string to be filtered.
     *
     * @return string
     *   The filtered string.
     *
     * @see \Drupal\system\Tests\Theme\TwigExtensionTest::testTwigExtensionFilter()
     */
    public static function testFilter($string) {
        return str_replace([
            'animal',
        ], [
            'plant',
        ], $string);
    }

}

Members

Title Sort descending Modifiers Object type Summary
TestExtension::getFilters public function Generates a list of all Twig filters that this extension defines.
TestExtension::getFunctions public function Generates a list of all Twig functions that this extension defines.
TestExtension::getName public function Gets a unique identifier for this Twig extension.
TestExtension::testFilter public static function Replaces all instances of "animal" in a string with "plant".
TestExtension::testFunction public static function Outputs either an uppercase or lowercase test phrase.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.