Browse Source

Added drupal_dump function.

8.x-1.x
Chi 8 years ago
parent
commit
3432ef490a
  1. 15
      composer.json
  2. 24
      src/TwigExtension.php

15
composer.json

@ -0,0 +1,15 @@
{
"name": "drupal/twig_tweak",
"type": "drupal-module",
"description": "A Twig extension with some useful functions and filters for Drupal development.",
"keywords": ["Drupal", "Twig"],
"license": "GPL-2.0+",
"homepage": "https://www.drupal.org/project/twig_tweak",
"suggest": {
"symfony/var-dumper": "Provides a better dump() function that can help you debug Twig variables."
},
"support": {
"issues": "https://www.drupal.org/project/issues/twig_tweak",
"source": "http://cgit.drupalcode.org/twig_tweak"
}
}

24
src/TwigExtension.php

@ -109,6 +109,8 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']), new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']),
new \Twig_SimpleFunction('drupal_menu', [$this, 'drupalMenu']), new \Twig_SimpleFunction('drupal_menu', [$this, 'drupalMenu']),
new \Twig_SimpleFunction('drupal_config', [$this, 'drupalConfig']), new \Twig_SimpleFunction('drupal_config', [$this, 'drupalConfig']),
new \Twig_SimpleFunction('drupal_dump', [$this, 'drupalDump']),
new \Twig_SimpleFunction('dd', [$this, 'drupalDump']),
// Wrap drupal_set_message() because it returns some value which is not // Wrap drupal_set_message() because it returns some value which is not
// suitable for Twig template. // suitable for Twig template.
new \Twig_SimpleFunction('drupal_set_message', [$this, 'drupalSetMessage']), new \Twig_SimpleFunction('drupal_set_message', [$this, 'drupalSetMessage']),
@ -409,4 +411,26 @@ class TwigExtension extends \Twig_Extension {
return $build; return $build;
} }
/**
* Dumps information about variables.
*/
public function drupalDump() {
$var_dumper = '\Symfony\Component\VarDumper\VarDumper';
if (class_exists($var_dumper)) {
call_user_func($var_dumper . '::dump', func_get_args());
}
else {
trigger_error('Could not dump the variable because symfony/var-dumper component is not installed.', E_USER_WARNING);
}
}
/**
* An alias for self::drupalDump().
*
* @see \Drupal\twig_tweak\TwigExtension::drupalDump();
*/
public function dd() {
$this->drupalDump(func_get_args());
}
} }

Loading…
Cancel
Save