3 namespace Drupal\devel\Twig\Extension;
5 use Drupal\devel\DevelDumperManagerInterface;
8 * Provides the Devel debugging function within Twig templates.
10 * NOTE: This extension doesn't do anything unless twig_debug is enabled.
11 * The twig_debug setting is read from the Twig environment, not Drupal
12 * Settings, so a container rebuild is necessary when toggling twig_debug on
15 class Debug extends \Twig_Extension {
18 * The devel dumper service.
20 * @var \Drupal\devel\DevelDumperManagerInterface
25 * Constructs a Debug object.
27 * @param \Drupal\devel\DevelDumperManagerInterface $dumper
28 * The devel dumper service.
30 public function __construct(DevelDumperManagerInterface $dumper) {
31 $this->dumper = $dumper;
37 public function getName() {
44 public function getFunctions() {
47 foreach (['devel_dump', 'kpr'] as $function) {
48 $functions[] = new \Twig_SimpleFunction($function, [$this, 'dump'], [
49 'is_safe' => ['html'],
50 'needs_environment' => TRUE,
51 'needs_context' => TRUE,
52 'is_variadic' => TRUE,
56 foreach (['devel_message', 'dpm', 'dsm'] as $function) {
57 $functions[] = new \Twig_SimpleFunction($function, [$this, 'message'], [
58 'is_safe' => ['html'],
59 'needs_environment' => TRUE,
60 'needs_context' => TRUE,
61 'is_variadic' => TRUE,
65 foreach (['devel_breakpoint'] as $function) {
66 $functions[] = new \Twig_SimpleFunction($function, [$this, 'breakpoint'], [
67 'needs_environment' => TRUE,
68 'needs_context' => TRUE,
69 'is_variadic' => TRUE,
77 * Provides debug function to Twig templates.
79 * Handles 0, 1, or multiple arguments.
81 * @param \Twig_Environment $env
82 * The twig environment instance.
83 * @param array $context
84 * An array of parameters passed to the template.
86 * An array of parameters passed the function.
89 * String representation of the input variables.
91 * @see \Drupal\devel\DevelDumperManager::dump()
93 public function dump(\Twig_Environment $env, array $context, array $args = []) {
94 if (!$env->isDebug()) {
100 // No arguments passed, display full Twig context.
102 $context_variables = $this->getContextVariables($context);
103 $this->dumper->dump($context_variables, 'Twig context');
106 foreach ($args as $variable) {
107 $this->dumper->dump($variable);
111 return ob_get_clean();
115 * Provides debug function to Twig templates.
117 * Handles 0, 1, or multiple arguments.
119 * @param \Twig_Environment $env
120 * The twig environment instance.
121 * @param array $context
122 * An array of parameters passed to the template.
124 * An array of parameters passed the function.
128 * @see \Drupal\devel\DevelDumperManager::message()
130 public function message(\Twig_Environment $env, array $context, array $args = []) {
131 if (!$env->isDebug()) {
135 // No arguments passed, display full Twig context.
137 $context_variables = $this->getContextVariables($context);
138 $this->dumper->message($context_variables, 'Twig context');
141 foreach ($args as $variable) {
142 $this->dumper->message($variable);
149 * Provides XDebug integration for Twig templates.
151 * To use this features simply put the following statement in the template
155 * {{ devel_breakpoint() }}
158 * When the template is evaluated is made a call to a dedicated method in
159 * devel twig debug extension in which is used xdebug_break(), that emits a
160 * breakpoint to the debug client (the debugger break on the specific line as
161 * if a normal file/line breakpoint was set on this line).
162 * In this way you'll be able to inspect any variables available in the
163 * template (environment, context, specific variables etc..) in your IDE.
165 * @param \Twig_Environment $env
166 * The twig environment instance.
167 * @param array $context
168 * An array of parameters passed to the template.
170 * An array of parameters passed the function.
172 public function breakpoint(\Twig_Environment $env, array $context, array $args = []) {
173 if (!$env->isDebug()) {
177 if (function_exists('xdebug_break')) {
183 * Filters the Twig context variable.
185 * @param array $context
189 * An array Twig context variables.
191 protected function getContextVariables(array $context) {
192 $context_variables = [];
193 foreach ($context as $key => $value) {
194 if (!$value instanceof \Twig_Template) {
195 $context_variables[$key] = $value;
198 return $context_variables;