From 894bd7199cf2cc1618bf572185517f6c6bdc4584 Mon Sep 17 00:00:00 2001
From: Gordon Heydon <gordon@heydon.com.au>
Date: Fri, 17 May 2013 12:34:59 +1000
Subject: [PATCH 2/2] Add new Twig filter

---
 core/modules/filter/filter.module                  | 15 ++++++++
 .../lib/Drupal/filter/Plugin/Filter/FilterTwig.php | 42 ++++++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 core/modules/filter/lib/Drupal/filter/Plugin/Filter/FilterTwig.php

diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module
index ca4ec2b..8711fc2 100644
--- a/core/modules/filter/filter.module
+++ b/core/modules/filter/filter.module
@@ -1416,6 +1416,21 @@ function _filter_html_image_secure_process($text) {
 }
 
 /**
+ * Preprocessing for the Twig Syntax
+ */
+function _filter_twig($text, $context = array()) {
+  $loader = new Twig_Loader_String();
+  $twig = new Twig_Environment($loader);
+
+  try {
+    return $twig->render($text, $context);
+  }
+  catch (Twig_Error_Syntax $e) {
+    return $e->getMessage();
+  }
+}
+
+/**
  * Formats an image DOM element that has an invalid source.
  *
  * @param DOMElement $image
diff --git a/core/modules/filter/lib/Drupal/filter/Plugin/Filter/FilterTwig.php b/core/modules/filter/lib/Drupal/filter/Plugin/Filter/FilterTwig.php
new file mode 100644
index 0000000..8e353d2
--- /dev/null
+++ b/core/modules/filter/lib/Drupal/filter/Plugin/Filter/FilterTwig.php
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\php\Plugin\Filter\Php.
+ */
+
+namespace Drupal\filter\Plugin\Filter;
+
+use Drupal\filter\Annotation\Filter;
+use Drupal\Core\Annotation\Translation;
+use Drupal\filter\Plugin\FilterBase;
+
+/**
+ * Provides PHP code filter. Use with care.
+ *
+ * @Filter(
+ *   id = "filter_twig",
+ *   module = "filter",
+ *   title = @Translation("Twig filter"),
+ *   description = @Translation("Filters the context using the Twig theming engine"),
+ *   type = FILTER_TYPE_MARKUP_LANGUAGE,
+ *   cache = TRUE
+ * )
+ */
+class FilterTwig extends FilterBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function process($text, $context, $langcode, $cache, $cache_id) {
+    return _filter_twig($text, $context);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function tips($long = FALSE) {
+    return t('Use Twig templating engine to render the text, see <a href="http://twig.sensiolabs.org/documentation">http://twig.sensiolabs.org/documentation</a> for more information.');
+  }
+
+}
-- 
1.7.12.4 (Apple Git-37)

