2012-11-03 17:36:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Handles integration of Twig templates with the Drupal theme system.
|
|
|
|
*/
|
|
|
|
use Drupal\Core\Template\TwigReference;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_theme().
|
|
|
|
*/
|
|
|
|
function twig_theme($existing, $type, $theme, $path) {
|
2013-01-30 03:11:25 +00:00
|
|
|
return drupal_find_theme_templates($existing, '.html.twig', $path);
|
2012-11-03 17:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_extension().
|
|
|
|
*/
|
|
|
|
function twig_extension() {
|
2013-01-30 03:11:25 +00:00
|
|
|
return '.html.twig';
|
2012-11-03 17:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_init().
|
|
|
|
*/
|
|
|
|
function twig_init($template) {
|
|
|
|
$file = dirname($template->filename) . '/template.php';
|
|
|
|
if (file_exists($file)) {
|
|
|
|
include_once DRUPAL_ROOT . '/' . $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render twig templates.
|
|
|
|
*
|
|
|
|
* This retrieves the Twig_Environment from the Drupal Injection container and
|
|
|
|
* renders the template.
|
|
|
|
*
|
|
|
|
* @param $template_file
|
|
|
|
* The filename of the template to render.
|
|
|
|
* @param $variables
|
|
|
|
* A keyed array of variables that will appear in the output.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The output generated by the template.
|
|
|
|
*/
|
|
|
|
function twig_render_template($template_file, $variables) {
|
2013-01-30 03:11:25 +00:00
|
|
|
$variables['_references'] = array();
|
2012-11-03 17:36:10 +00:00
|
|
|
return drupal_container()->get('twig')->loadTemplate($template_file)->render($variables);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around render() for twig printed output.
|
|
|
|
*
|
|
|
|
* If an object is passed that has no __toString method an exception is thrown;
|
|
|
|
* other objects are casted to string. However in the case that the object is an
|
|
|
|
* instance of a Twig_Markup object it is returned directly to support auto
|
|
|
|
* escaping.
|
|
|
|
*
|
|
|
|
* If an array is passed it is rendered via render() and scalar values are
|
|
|
|
* returned directly.
|
|
|
|
*
|
|
|
|
* @param mixed $arg
|
|
|
|
* String, Object or Render Array
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The rendered output or an Twig_Markup object.
|
|
|
|
*
|
|
|
|
* @see render
|
|
|
|
* @see TwigNodeVisitor
|
|
|
|
*/
|
2012-12-29 08:54:13 +00:00
|
|
|
function twig_render_var($arg) {
|
2012-11-03 17:36:10 +00:00
|
|
|
if ($arg instanceof TwigReference) {
|
|
|
|
$arg = &$arg->getReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
// == is true also for empty arrays
|
|
|
|
if ($arg == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep Twig_Markup objects intact to prepare for later autoescaping support
|
|
|
|
if ($arg instanceOf Twig_Markup) {
|
|
|
|
return $arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_scalar($arg)) {
|
|
|
|
return $arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_object($arg)) {
|
|
|
|
if (method_exists($arg, '__toString')) {
|
|
|
|
return (string) $arg;
|
|
|
|
}
|
|
|
|
throw new Exception(t('Object of type "@class" cannot be printed.', array('@class' => get_class($arg))));
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a normal render array.
|
|
|
|
return render($arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around hide() that does not return the content.
|
|
|
|
*
|
|
|
|
* @see hide
|
|
|
|
*/
|
|
|
|
function twig_hide($element) {
|
|
|
|
if ($element instanceof TwigReference) {
|
|
|
|
$element = &$element->getReference();
|
|
|
|
hide($element);
|
|
|
|
}
|
|
|
|
// @todo Add warning in else case
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper around show() that does not return the content.
|
|
|
|
*
|
|
|
|
* @see show
|
|
|
|
*/
|
|
|
|
function twig_show($element) {
|
|
|
|
if ($element instanceof TwigReference) {
|
|
|
|
$element = &$element->getReference();
|
|
|
|
show($element);
|
|
|
|
}
|
|
|
|
// @todo Add warning in else case
|
|
|
|
}
|