From a36e179ae2578d98a2d5a23c80cd12c2462a8002 Mon Sep 17 00:00:00 2001 From: Basil Suter Date: Mon, 17 Apr 2017 12:52:09 +0200 Subject: [PATCH] Add raw option for html block in order to render the html output in admin view. closes #1269 --- CHANGELOG.md | 1 + modules/cms/composer.json | 3 +- modules/cms/composer.lock | 81 ++++++++++++++++--- modules/cms/src/frontend/blocks/HtmlBlock.php | 17 +++- modules/cms/src/frontend/messages/de/cms.php | 3 +- modules/cms/src/frontend/messages/el/cms.php | 3 +- modules/cms/src/frontend/messages/en/cms.php | 3 +- modules/cms/src/frontend/messages/es/cms.php | 3 +- modules/cms/src/frontend/messages/fr/cms.php | 3 +- modules/cms/src/frontend/messages/it/cms.php | 4 +- modules/cms/src/frontend/messages/pt/cms.php | 3 +- modules/cms/src/frontend/messages/ru/cms.php | 3 +- modules/cms/src/frontend/messages/ua/cms.php | 3 +- modules/cms/tests/BlockTestCase.php | 16 ++++ .../src/frontend/blocks/HtmlBlockTest.php | 14 ++++ 15 files changed, 138 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09c09f473..f269451f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The changelog contains informations about bug fixes, new features or bc breaking - [#1268](https://github.com/luyadev/luya/issues/1268) Module block strict render ability in order to strict render the given action and controller paths instead of parse them trough the request component. - [#1227](https://github.com/luyadev/luya/issues/1227) Added preloadModels() method for the Menu Query in order to collect all models for the given request. This can strongly reduce the sql count when working with properties or models. - [#1266](https://github.com/luyadev/luya/issues/1266) render() method for the mailer component in order to provide controller template files. +- [#1269](https://github.com/luyadev/luya/issues/1269) Add raw option for html block in order to render the html output in admin view. ### Fixed diff --git a/modules/cms/composer.json b/modules/cms/composer.json index b94ce4c25..6e730177f 100644 --- a/modules/cms/composer.json +++ b/modules/cms/composer.json @@ -19,7 +19,8 @@ "phpunit/phpunit" : "~5.0", "luyadev/luya" : "^1.0@dev", "luyadev/luya-module-admin" : "^1.0@dev", - "satooshi/php-coveralls": "~1.0" + "satooshi/php-coveralls": "~1.0", + "twig/twig" : "^2.0" }, "autoload" : { "psr-4" : { diff --git a/modules/cms/composer.lock b/modules/cms/composer.lock index 8f0af73ba..3d5fc4626 100644 --- a/modules/cms/composer.lock +++ b/modules/cms/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "be663a77f7043a293a92b7112d8af437", + "content-hash": "d29a8bd69d464532d461b3f9c923836b", "packages": [], "packages-dev": [ { @@ -587,12 +587,12 @@ "source": { "type": "git", "url": "https://github.com/luyadev/luya.git", - "reference": "0237d5f06acfb439cabcbddc282582705a943a61" + "reference": "7ba94759de07e35422a4959bb2d401fbf3672224" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/luyadev/luya/zipball/0237d5f06acfb439cabcbddc282582705a943a61", - "reference": "0237d5f06acfb439cabcbddc282582705a943a61", + "url": "https://api.github.com/repos/luyadev/luya/zipball/7ba94759de07e35422a4959bb2d401fbf3672224", + "reference": "7ba94759de07e35422a4959bb2d401fbf3672224", "shasum": "" }, "require": { @@ -647,7 +647,7 @@ "yii2", "yii2-wrapper" ], - "time": "2017-04-13 16:27:14" + "time": "2017-04-16 11:16:06" }, { "name": "luyadev/luya-module-admin", @@ -655,12 +655,12 @@ "source": { "type": "git", "url": "https://github.com/luyadev/luya-module-admin.git", - "reference": "c68d880a4225e027425c00c41a9b1e7a75d2dc8e" + "reference": "f2ea3a6361a41eca3c974fdceb85d53648a46717" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/luyadev/luya-module-admin/zipball/c68d880a4225e027425c00c41a9b1e7a75d2dc8e", - "reference": "c68d880a4225e027425c00c41a9b1e7a75d2dc8e", + "url": "https://api.github.com/repos/luyadev/luya-module-admin/zipball/f2ea3a6361a41eca3c974fdceb85d53648a46717", + "reference": "f2ea3a6361a41eca3c974fdceb85d53648a46717", "shasum": "" }, "require": { @@ -710,7 +710,7 @@ "yii", "yii2" ], - "time": "2017-04-12 14:12:31" + "time": "2017-04-13 16:35:21" }, { "name": "myclabs/deep-copy", @@ -2495,6 +2495,69 @@ "homepage": "https://symfony.com", "time": "2017-03-20T09:45:15+00:00" }, + { + "name": "twig/twig", + "version": "v2.3.0", + "source": { + "type": "git", + "url": "https://github.com/twigphp/Twig.git", + "reference": "9718186a5df85a4f7917e78d3ffcabc204c75d25" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/9718186a5df85a4f7917e78d3ffcabc204c75d25", + "reference": "9718186a5df85a4f7917e78d3ffcabc204c75d25", + "shasum": "" + }, + "require": { + "php": "^7.0", + "symfony/polyfill-mbstring": "~1.0" + }, + "require-dev": { + "psr/container": "^1.0", + "symfony/debug": "~2.7", + "symfony/phpunit-bridge": "~3.3@dev" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.3-dev" + } + }, + "autoload": { + "psr-0": { + "Twig_": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + }, + { + "name": "Armin Ronacher", + "email": "armin.ronacher@active-4.com", + "role": "Project Founder" + }, + { + "name": "Twig Team", + "homepage": "http://twig.sensiolabs.org/contributors", + "role": "Contributors" + } + ], + "description": "Twig, the flexible, fast, and secure template language for PHP", + "homepage": "http://twig.sensiolabs.org", + "keywords": [ + "templating" + ], + "time": "2017-03-22T15:41:51+00:00" + }, { "name": "webmozart/assert", "version": "1.2.0", diff --git a/modules/cms/src/frontend/blocks/HtmlBlock.php b/modules/cms/src/frontend/blocks/HtmlBlock.php index 4a3a18a97..493103903 100644 --- a/modules/cms/src/frontend/blocks/HtmlBlock.php +++ b/modules/cms/src/frontend/blocks/HtmlBlock.php @@ -54,8 +54,11 @@ public function config() { return [ 'vars' => [ - ['var' => 'html', 'label' => Module::t('block_html_html_label'), 'type' => 'zaa-textarea'], + ['var' => 'html', 'label' => Module::t('block_html_html_label'), 'type' => self::TYPE_TEXTAREA], ], + 'cfgs' => [ + ['var' => 'raw', 'label' => Module::t('block_html_cfg_raw_label'), 'type' => self::TYPE_CHECKBOX] + ], ]; } @@ -64,6 +67,16 @@ public function config() */ public function admin() { - return '{% if vars.html is empty %}' . Module::t('block_html_no_content') . '{% else %}

{{ vars.html | escape }}

{% endif %}'; + $message = Module::t('block_html_no_content'); + return <<{$message}{% else %} + {% if cfgs.raw == 1 %} + {{ vars.html | raw }} + {% else %} +
{{ vars.html | escape }}
+ {% endif %} + {% endif %} +EOT; } } diff --git a/modules/cms/src/frontend/messages/de/cms.php b/modules/cms/src/frontend/messages/de/cms.php index 05a760485..46746e1ed 100644 --- a/modules/cms/src/frontend/messages/de/cms.php +++ b/modules/cms/src/frontend/messages/de/cms.php @@ -179,6 +179,7 @@ // 1.0.0 'block_module_strictrender' => 'Strict Render', - 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.' + 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.', + 'block_html_cfg_raw_label' => 'Render HTML in Admin', ]; diff --git a/modules/cms/src/frontend/messages/el/cms.php b/modules/cms/src/frontend/messages/el/cms.php index 8bfb505fc..cb87a5d31 100644 --- a/modules/cms/src/frontend/messages/el/cms.php +++ b/modules/cms/src/frontend/messages/el/cms.php @@ -178,6 +178,7 @@ // 1.0.0 'block_module_strictrender' => 'Strict Render', - 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.' + 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.', + 'block_html_cfg_raw_label' => 'Render HTML in Admin', ]; diff --git a/modules/cms/src/frontend/messages/en/cms.php b/modules/cms/src/frontend/messages/en/cms.php index 43244cf6b..7bf113cd0 100644 --- a/modules/cms/src/frontend/messages/en/cms.php +++ b/modules/cms/src/frontend/messages/en/cms.php @@ -179,5 +179,6 @@ // 1.0.0 'block_module_strictrender' => 'Strict Render', - 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.' + 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.', + 'block_html_cfg_raw_label' => 'Render HTML in Admin', ]; diff --git a/modules/cms/src/frontend/messages/es/cms.php b/modules/cms/src/frontend/messages/es/cms.php index 33fa0ec47..cdae71acf 100644 --- a/modules/cms/src/frontend/messages/es/cms.php +++ b/modules/cms/src/frontend/messages/es/cms.php @@ -179,6 +179,7 @@ // 1.0.0 'block_module_strictrender' => 'Strict Render', - 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.' + 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.', + 'block_html_cfg_raw_label' => 'Render HTML in Admin', ]; diff --git a/modules/cms/src/frontend/messages/fr/cms.php b/modules/cms/src/frontend/messages/fr/cms.php index b7c3569fa..17f1a04c5 100644 --- a/modules/cms/src/frontend/messages/fr/cms.php +++ b/modules/cms/src/frontend/messages/fr/cms.php @@ -179,6 +179,7 @@ // 1.0.0 'block_module_strictrender' => 'Strict Render', - 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.' + 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.', + 'block_html_cfg_raw_label' => 'Render HTML in Admin', ]; diff --git a/modules/cms/src/frontend/messages/it/cms.php b/modules/cms/src/frontend/messages/it/cms.php index 96f3516ed..4f36a00f3 100644 --- a/modules/cms/src/frontend/messages/it/cms.php +++ b/modules/cms/src/frontend/messages/it/cms.php @@ -178,6 +178,6 @@ // 1.0.0 'block_module_strictrender' => 'Strict Render', - 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.' - + 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.', + 'block_html_cfg_raw_label' => 'Render HTML in Admin', ]; diff --git a/modules/cms/src/frontend/messages/pt/cms.php b/modules/cms/src/frontend/messages/pt/cms.php index 83d0eb43a..a1fe0127f 100644 --- a/modules/cms/src/frontend/messages/pt/cms.php +++ b/modules/cms/src/frontend/messages/pt/cms.php @@ -179,6 +179,7 @@ // 1.0.0 'block_module_strictrender' => 'Strict Render', - 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.' + 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.', + 'block_html_cfg_raw_label' => 'Render HTML in Admin', ]; diff --git a/modules/cms/src/frontend/messages/ru/cms.php b/modules/cms/src/frontend/messages/ru/cms.php index 467c50343..ebe991eb5 100644 --- a/modules/cms/src/frontend/messages/ru/cms.php +++ b/modules/cms/src/frontend/messages/ru/cms.php @@ -179,6 +179,7 @@ // 1.0.0 'block_module_strictrender' => 'Strict Render', - 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.' + 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.', + 'block_html_cfg_raw_label' => 'Render HTML in Admin', ]; diff --git a/modules/cms/src/frontend/messages/ua/cms.php b/modules/cms/src/frontend/messages/ua/cms.php index 7c3f78aec..475d6ba64 100644 --- a/modules/cms/src/frontend/messages/ua/cms.php +++ b/modules/cms/src/frontend/messages/ua/cms.php @@ -179,6 +179,7 @@ // 1.0.0 'block_module_strictrender' => 'Strict Render', - 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.' + 'block_module_strictrender_help' => 'When strict render is enabled, the module will only run the provided route (module, controller, action, params) without listening to action and controller routes.', + 'block_html_cfg_raw_label' => 'Render HTML in Admin', ]; diff --git a/modules/cms/tests/BlockTestCase.php b/modules/cms/tests/BlockTestCase.php index 2ee63e632..647df0316 100644 --- a/modules/cms/tests/BlockTestCase.php +++ b/modules/cms/tests/BlockTestCase.php @@ -2,6 +2,8 @@ namespace cmstests; +use Twig_Loader_String; + class BlockTestCase extends CmsFrontendTestCase { public $blockClass = null; @@ -30,6 +32,20 @@ public function renderFrontend() $this->assertNotNull($this->block->getFieldHelp()); return $this->block->renderFrontend(); } + + public function renderAdmin() + { + $twig = new \Twig_Environment(new \Twig_Loader_Filesystem()); + $temp = $twig->createTemplate($this->block->renderAdmin()); + return $temp->render(['cfgs' => $this->block->getCfgValues(), 'vars' => $this->block->getVarValues()]); + } + + public function renderAdminNoSpace() + { + $text = trim(preg_replace('/\s+/', ' ', $this->renderAdmin())); + + return str_replace(['> ', ' <'], ['>', '<'], $text); + } public function renderFrontendNoSpace() { diff --git a/modules/cms/tests/src/frontend/blocks/HtmlBlockTest.php b/modules/cms/tests/src/frontend/blocks/HtmlBlockTest.php index 0f3161852..37ee0bf97 100644 --- a/modules/cms/tests/src/frontend/blocks/HtmlBlockTest.php +++ b/modules/cms/tests/src/frontend/blocks/HtmlBlockTest.php @@ -21,4 +21,18 @@ public function testInputHtml() $this->assertSame('
Hello World
', $this->renderFrontendNoSpace()); } + + public function testAdminRawOutput() + { + $this->block->setVarValues([ + 'html' => '

hello

', + ]); + + $this->assertSame('
<p>hello</p>
', $this->renderAdminNoSpace()); + + $this->block->setCfgValues(['raw' => 1]); + + $this->assertSame('

hello

', $this->renderAdminNoSpace()); + + } }