Skip to content

Instantly share code, notes, and snippets.

@michaelhagedon
Created May 23, 2013 22:47
Show Gist options
  • Select an option

  • Save michaelhagedon/5640064 to your computer and use it in GitHub Desktop.

Select an option

Save michaelhagedon/5640064 to your computer and use it in GitHub Desktop.
Test script for Drupal Twig issue: http://drupal.org/node/1922304
<?php
use Drupal\Core\Template\TwigReferenceFunction;
use Drupal\Core\Template\TwigTemplate;
use Drupal\Core\Template\TwigNodeExpressionGetAttrReference;
use Drupal\Core\Template\TwigNodeExpressionNameReferenceEnd;
use Drupal\Core\Template\TwigNodeExpressionNameReference;
use Drupal\Core\Template\TwigReferenceNodeVisitor;
function twig_change(&$context, $template, $val) {
// Unwrap reference if twig_reference key is set
if (isset($val['#twig_reference']) && $val['#twig_reference'] === TRUE) {
$val = &$template->unwrapReference($context, $val);
}
// Add some key to verify the value could be modified
if (is_array($val)) {
$val['#printed'] = TRUE;
}
var_dump($val);
}
$twig = Drupal::service('twig');
//$loader = new Twig_Loader_String();
//$twig = new Twig_Environment($loader, array(
// 'debug' => TRUE,
// 'strict_variables' => FALSE,
// 'autoescape' => FALSE,
// 'cache' => false,
// 'base_template_class' => 'Drupal\Core\Template\TwigTemplate',
//));
$twig->addFunction('change', new TwigReferenceFunction('twig_change', array('needs_context' => TRUE)));
$twig->addFunction('change_val', new Twig_Function_Function('twig_change', array('needs_context' => TRUE)));
//$twig->addNodeVisitor(new TwigReferenceNodeVisitor());
//$twig->addExtension(new Twig_Extension_Debug());
$vars = array('x' => array('foo'), 'y' => array());
$name = 'test-twig-reference-node-visitor.html.twig';
//print $twig->parse($twig->tokenize($code));
print $twig->render($name, array('vars' => $vars));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment