I tried to use my custom php function in twig. When I type {{current_url}} in my page.html.twig nothing happens. I also tried {{kint(current_url)}}, it outputs NULL This is my code
<?php
namespace Drupal
extprev\plugin\block\customtwigoutput;
use Drupal\Core\Template\TwigExtension;
use Drupal
extprev
extprevmodule\path;
use \Symfony\Component\HttpFoundation\Request;
class AppExtension extends \Twig_Extension {
public function url() {
$current_path = \Drupal::service('path.current')->getPath();
return $current_path;
}
public function getFilters() {
return array(
'current_url' => new \Twig_Function_Function(array('Drupal
extprev\plugin\block\customtwigoutput\AppExtension', 'url')),
);
}
}
What am I doing wrong?
Did you add services.yml to it? This is a sample;
twig.url:
class: AppBundle\Twig\Extension\AppExtension
tags:
- { twig.extension }
Maybe you should try with a twig function instead :
In your extension :
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('current_url', array($this, 'url')),
);
}
In your view
{{ current_url() }}