commit 00a7a29dde6c689feac037e17b9b0eb00cc35407 Author: Chi Date: Sun Jan 3 22:58:44 2016 +0300 Initial commit. diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..f533c5a --- /dev/null +++ b/README.txt @@ -0,0 +1,6 @@ +-- SUMMARY -- + +Twig Tweak module provides developers a Twig extension with some addition +functions and filters. + +See src/TwigExtension.php for details. diff --git a/src/TwigExtension.php b/src/TwigExtension.php new file mode 100644 index 0000000..6ec3165 --- /dev/null +++ b/src/TwigExtension.php @@ -0,0 +1,161 @@ +blockBuilder = \Drupal::entityTypeManager() + ->getViewBuilder('block'); + + $this->entityTypeManager = $entity_type_manager; + $this->token = $token; + $this->configFactory = $config_factory; + } + + /** + * {@inheritdoc} + */ + public function getFunctions() { + return [ + new \Twig_SimpleFunction('drupal_view', [$this, 'drupalView']), + new \Twig_SimpleFunction('drupal_block', [$this, 'drupalBlock']), + new \Twig_SimpleFunction('drupal_token', [$this, 'drupalToken']), + new \Twig_SimpleFunction('drupal_entity', [$this, 'drupalEntity']), + new \Twig_SimpleFunction('drupal_config', [$this, 'drupalConfig']) + ]; + } + + /** + * {@inheritdoc} + */ + public function getFilters() { + return [ + new \Twig_SimpleFilter('php', [$this, 'phpFilter']), + new \Twig_SimpleFilter('token_replace', [$this, 'tokenReplaceFilter']), + new \Twig_SimpleFilter('preg_replace', [$this, 'pregPeplaceFilter']), + ]; + } + + /** + * {@inheritdoc} + */ + public function getName() { + return 'twig_tweak'; + } + + /** + * Embeds a view. + */ + public function drupalView($name, $display_id = 'default') { + $args = func_get_args(); + return views_embed_view( + $name, + $display_id, + isset($args[2]) ? $args[2] : NULL, + isset($args[3]) ? $args[3] : NULL, + isset($args[4]) ? $args[2] : NULL + ); + } + + /** + * Builds the render array for the provided block. + */ + public function drupalBlock($id) { + $block = Block::load($id); + return $this->entityTypeManager->getViewBuilder('block')->view($block); + } + + /** + * Replaces a given tokens with appropriate value. + */ + public function drupalToken($token) { + return $this->token->replace("[$token]"); + } + + /** + * Returns the render array for an entity. + */ + public function drupalEntity($entity_type, $id, $view_mode = NULL, $langcode = NULL) { + $entity = $this->entityTypeManager->getStorage($entity_type)->load($id); + $render_controller = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId()); + return $render_controller->view($entity, $view_mode, $langcode); + } + + /** + * Gets data from this configuration. + */ + public function drupalConfig($name, $key) { + return $this->configFactory->get($name)->get($key); + } + + /** + * Evaluates a string of PHP code. + */ + public function phpFilter($code) { + ob_start(); + print eval($code); + $output = ob_get_contents(); + ob_end_clean(); + return $output; + } + + /** + * Replaces all tokens in a given string with appropriate values. + */ + public function tokenReplaceFilter($text) { + return $this->token->replace($text); + } + + /** + * Performs a regular expression search and replace. + */ + public function pregPeplaceFilter($text, $pattern, $replacement) { + return preg_replace('/' . preg_quote($pattern, '/') . '/', $replacement, $text); + } + +} diff --git a/twig_tweak.info.yml b/twig_tweak.info.yml new file mode 100644 index 0000000..79d8162 --- /dev/null +++ b/twig_tweak.info.yml @@ -0,0 +1,6 @@ +name: Twig tweak +type: module +description: Provides some extra twig functions. +package: custom +version: 8.x-1.0-dev +core: 8.x diff --git a/twig_tweak.services.yml b/twig_tweak.services.yml new file mode 100644 index 0000000..f6fb5ca --- /dev/null +++ b/twig_tweak.services.yml @@ -0,0 +1,6 @@ +services: + twig_tweak.twig_extension: + class: Drupal\twig_tweak\TwigExtension + arguments: ['@entity_type.manager', '@token', '@config.factory'] + tags: + - { name: twig.extension }