diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8eb8ae0 --- /dev/null +++ b/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" + } +} diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 979676f..25d747b 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -109,6 +109,8 @@ class TwigExtension extends \Twig_Extension { new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']), new \Twig_SimpleFunction('drupal_menu', [$this, 'drupalMenu']), 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 // suitable for Twig template. new \Twig_SimpleFunction('drupal_set_message', [$this, 'drupalSetMessage']), @@ -409,4 +411,26 @@ class TwigExtension extends \Twig_Extension { 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()); + } + }