David Kovač David Kovač - 17 days ago 80
Twig Question

How can I print the output of my custom php function in twig?

In my custom module I have the following code:

public function url() {
$current_path = \Drupal::service('path.current')->getPath();
return $current_path;
}


How can I use this function in my twig template?

Note: I know I can get the current path much easier. I only specified this function so I can get the idea of using custom php functions in twig.

Answer Source

You can get current path in Twig (Drupal) like this :

<a href="{{ url('<current>') }}">{{ 'I represent current page URL :)'|t }}</a>

Edit

And as defined in the Drupal doc, you can add your own functions to the twig template following this example which shows how to add a function or a filter to the twig template :

namespace Drupal\twig_extension_test\TwigExtension;

use Drupal\Core\Template\TwigExtension;

/**
 * A test Twig extension that adds a custom function and a custom filter.
 */
class TestExtension extends TwigExtension {

/**
 * 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
 *   {{ testfunc() }}
 *   @endcode
 *
 *   The value is a standard PHP callback that defines what the function does.
 */
 public function getFunctions() {
     return array(
         'testfunc' => new \Twig_Function_Function(array('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|testfilter }}
 *   @endcode
 *
 *   The value is a standard PHP callback that defines what the filter does.
 */
 public function getFilters() {
     return array(
          'testfilter' => new \Twig_Filter_Function(array('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(array('animal'), array('plant'), $string);
  }

  }