Browse Source

Widget content render behavior

master
Egorka 6 years ago
parent
commit
a1c1ca905e
  1. 30
      common/behaviors/WidgetContentBehavior.php
  2. 2
      common/modules/pages/entities/Page.php
  3. 12
      common/modules/pages/views/page/view.php
  4. 8
      frontend/web/themes/start/modules/pages/views/page/view.php

30
common/behaviors/WidgetContentBehavior.php

@ -0,0 +1,30 @@
<?php
/**
* Created by Error202
* Date: 08.08.2018
*/
namespace common\behaviors;
use yii\helpers\Html;
use yii\base\Behavior;
class WidgetContentBehavior extends Behavior
{
/**
* Parse Widgets [?= ... ?]
* @param $text
*
* @return string
*/
public function content($text): string
{
$content = preg_replace_callback('/\[\?= (.*)\((.*)\) \?\]/m', function ($m) {
$array = [];
$string = Html::decode( $m[2] );
eval( "\$array = $string;" );
return $m[1]( $array );
}, $text);
return $content;
}
}

2
common/modules/pages/entities/Page.php

@ -6,6 +6,7 @@ use common\modules\pages\entities\queries\PageQuery;
use paulzi\nestedsets\NestedSetsBehavior;
use core\behaviors\MetaBehavior;
use yii\behaviors\TimestampBehavior;
use common\behaviors\WidgetContentBehavior;
use yii\db\ActiveRecord;
use core\entities\Meta;
use Yii;
@ -81,6 +82,7 @@ class Page extends ActiveRecord
'treeAttribute' => 'tree',
],
TimestampBehavior::class,
WidgetContentBehavior::class,
];
}

12
common/modules/pages/views/page/view.php

@ -29,7 +29,7 @@ $this->params['breadcrumbs'][] = $page->title;
'URI.SafeIframeRegexp'=>'%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%',
])) ?> -->
<?= preg_replace_callback('/\[\?= (.*) \?\]/gm', function ($m) {
<!-- < ?= preg_replace_callback('/\[\?= (.*) \?\]/gm', function ($m) {
return $m[1];
}, Yii::$app->formatter->asHtml($page->content, [
'Attr.AllowedRel' => array('nofollow'),
@ -37,6 +37,14 @@ $this->params['breadcrumbs'][] = $page->title;
'Output.FlashCompat' => true,
'HTML.SafeIframe' => true,
'URI.SafeIframeRegexp'=>'%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%',
])) ?>
])) ?> -->
<?= $page->content(Yii::$app->formatter->asHtml($page->content, [
'Attr.AllowedRel' => array('nofollow'),
'HTML.SafeObject' => true,
'Output.FlashCompat' => true,
'HTML.SafeIframe' => true,
'URI.SafeIframeRegexp'=>'%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%',
]))?>
</article>

8
frontend/web/themes/start/modules/pages/views/page/view.php

@ -21,12 +21,7 @@ $this->params['breadcrumbs'][] = $page->title;
<h1><?= Html::encode($page->title) ?></h1>
<?= preg_replace_callback('/\[\?= (.*)\((.*)\) \?\]/m', function ($m) {
$array = [];
$string = Html::decode($m[2]);
eval("\$array = $string;");
return $m[1]($array);
}, Yii::$app->formatter->asHtml($page->content, [
<?= $page->content(Yii::$app->formatter->asHtml($page->content, [
'Attr.AllowedRel' => array('nofollow'),
'HTML.SafeObject' => true,
'Output.FlashCompat' => true,
@ -34,4 +29,5 @@ $this->params['breadcrumbs'][] = $page->title;
'URI.SafeIframeRegexp'=>'%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%',
])) ?>
</article>

Loading…
Cancel
Save