Version 1
[yaffs-website] / web / core / modules / system / tests / modules / twig_extension_test / src / TwigExtension / TestExtension.php
1 <?php
2
3 namespace Drupal\twig_extension_test\TwigExtension;
4
5
6 /**
7  * A test Twig extension that adds a custom function and a custom filter.
8  */
9 class TestExtension extends \Twig_Extension {
10
11   /**
12    * Generates a list of all Twig functions that this extension defines.
13    *
14    * @return array
15    *   A key/value array that defines custom Twig functions. The key denotes the
16    *   function name used in the tag, e.g.:
17    *   @code
18    *   {{ testfunc() }}
19    *   @endcode
20    *
21    *   The value is a standard PHP callback that defines what the function does.
22    */
23   public function getFunctions() {
24     return [
25       'testfunc' => new \Twig_Function_Function(['Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFunction']),
26     ];
27   }
28
29   /**
30    * Generates a list of all Twig filters that this extension defines.
31    *
32    * @return array
33    *   A key/value array that defines custom Twig filters. The key denotes the
34    *   filter name used in the tag, e.g.:
35    *   @code
36    *   {{ foo|testfilter }}
37    *   @endcode
38    *
39    *   The value is a standard PHP callback that defines what the filter does.
40    */
41   public function getFilters() {
42     return [
43       'testfilter' => new \Twig_Filter_Function(['Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFilter']),
44     ];
45   }
46
47   /**
48    * Gets a unique identifier for this Twig extension.
49    *
50    * @return string
51    *   A unique identifier for this Twig extension.
52    */
53   public function getName() {
54     return 'twig_extension_test.test_extension';
55   }
56
57   /**
58    * Outputs either an uppercase or lowercase test phrase.
59    *
60    * The function generates either an uppercase or lowercase version of the
61    * phrase "The quick brown fox jumps over the lazy dog 123.", depending on
62    * whether or not the $upperCase parameter evaluates to TRUE. If $upperCase
63    * evaluates to TRUE, the result will be uppercase, and if it evaluates to
64    * FALSE, the result will be lowercase.
65    *
66    * @param bool $upperCase
67    *   (optional) Whether the result is uppercase (true) or lowercase (false).
68    *
69    * @return string
70    *   The generated string.
71    *
72    * @see \Drupal\system\Tests\Theme\TwigExtensionTest::testTwigExtensionFunction()
73    */
74   public static function testFunction($upperCase = FALSE) {
75     $string = "The quick brown box jumps over the lazy dog 123.";
76     if ($upperCase == TRUE) {
77       return strtoupper($string);
78     }
79     else {
80       return strtolower($string);
81     }
82   }
83
84   /**
85    * Replaces all instances of "animal" in a string with "plant".
86    *
87    * @param string $string
88    *   The string to be filtered.
89    *
90    * @return string
91    *   The filtered string.
92    *
93    * @see \Drupal\system\Tests\Theme\TwigExtensionTest::testTwigExtensionFilter()
94    */
95   public static function testFilter($string) {
96     return str_replace(['animal'], ['plant'], $string);
97   }
98
99 }