From 4a5394b9f1d5019a2858cdfe60ba2a331b88c38c Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Sat, 11 May 2024 19:24:57 +0200 Subject: [PATCH 1/6] added rule for widget content status --- src/models/crud/WidgetContent.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/models/crud/WidgetContent.php b/src/models/crud/WidgetContent.php index 6768c04..b2cf862 100644 --- a/src/models/crud/WidgetContent.php +++ b/src/models/crud/WidgetContent.php @@ -208,7 +208,11 @@ function ($attribute) { ]; } - + $rules['default-status'] = [ + 'status', + 'in', + 'range' => array_keys(self::optsStatus()) + ]; return $rules; } From 6a0c56a61405d9e69fce5d19a904851d29441e89 Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Sat, 11 May 2024 19:25:18 +0200 Subject: [PATCH 2/6] default display if invalid status is in db --- src/views/crud/widget/index.php | 2 +- src/views/crud/widget/view.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/views/crud/widget/index.php b/src/views/crud/widget/index.php index 2d6c195..c024541 100644 --- a/src/views/crud/widget/index.php +++ b/src/views/crud/widget/index.php @@ -150,7 +150,7 @@ 'contentOptions' => ['nowrap' => 'nowrap'], 'filter'=> \hrzg\widget\models\crud\WidgetContent::optsStatus(), 'value' => function($model) { - return $model->status ? $model::optsStatus()[$model->status] : 'n/a'; + return $model::optsStatus()[$model->status] ?? 'n/a'; }, 'filterInputOptions' => [ 'class' => 'form-control', diff --git a/src/views/crud/widget/view.php b/src/views/crud/widget/view.php index 91df0f1..7c80aa8 100644 --- a/src/views/crud/widget/view.php +++ b/src/views/crud/widget/view.php @@ -86,7 +86,7 @@ [ 'attribute' => 'status', 'format' => 'raw', - 'value' => (isset($model->status) ? Html::encode($model::optsStatus()[$model->status]) : 'n/a') + 'value' => (Html::encode($model::optsStatus()[$model->status]) ?? 'n/a') . ($model->getBehavior('translation_meta')->isFallbackTranslation ? ' fallback' : ''), From ba1a2b5239aaf446fe9d5e4d7efb236fe79d8c73 Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Sat, 11 May 2024 19:30:51 +0200 Subject: [PATCH 3/6] allow json schema with only one type --- src/widgets/Cell.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/widgets/Cell.php b/src/widgets/Cell.php index fdf97a6..105b573 100644 --- a/src/widgets/Cell.php +++ b/src/widgets/Cell.php @@ -24,6 +24,7 @@ use yii\helpers\Html; use yii\helpers\Json; use yii\helpers\Url; +use yii\helpers\VarDumper; /** * Class Cell @@ -268,6 +269,9 @@ private function renderWidgets() $class->setView($widget->getViewFile()); if ($properties) { + if (is_string($properties)) { + $properties = [$properties]; + } $class->setProperties($properties); } $visibility = $widget->isVisibleFrontend() ? '' : 'hrzg-widget-widget-invisible-frontend'; From 0400120c2c40b70ff1edc8efe99dc103c47fbbb0 Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Mon, 13 May 2024 09:19:43 +0200 Subject: [PATCH 4/6] reformat code --- src/widgets/Cell.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/widgets/Cell.php b/src/widgets/Cell.php index 105b573..6621e9c 100644 --- a/src/widgets/Cell.php +++ b/src/widgets/Cell.php @@ -24,7 +24,6 @@ use yii\helpers\Html; use yii\helpers\Json; use yii\helpers\Url; -use yii\helpers\VarDumper; /** * Class Cell @@ -117,8 +116,8 @@ public function init() /** * @inheritdoc - * @return string * @throws \yii\base\InvalidConfigException + * @return string */ public function run() { @@ -245,9 +244,9 @@ private function getModuleRoute() } /** - * @return string * @throws \yii\base\InvalidConfigException * @throws \Exception + * @return string */ private function renderWidgets() { @@ -296,8 +295,8 @@ private function renderWidgets() } /** - * @return string * @throws \Exception + * @return string */ private function generateCellControls() { @@ -349,8 +348,8 @@ private function generateCellControls() /** * @param $widget * - * @return string * @throws \Exception + * @return string */ private function generateWidgetControls(WidgetContent $widget) { @@ -497,8 +496,8 @@ function(xhr) { /** * @param $widget * - * @return boolean * @throws \Exception + * @return boolean */ private function checkPublicationStatus($widget) { From 1698f242fdb4f2b0de12238c4f9dc03b1e1de76f Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Mon, 13 May 2024 09:20:18 +0200 Subject: [PATCH 5/6] Make json schemas with only one property more usable --- src/widgets/Cell.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/widgets/Cell.php b/src/widgets/Cell.php index 6621e9c..ec2194e 100644 --- a/src/widgets/Cell.php +++ b/src/widgets/Cell.php @@ -268,8 +268,11 @@ private function renderWidgets() $class->setView($widget->getViewFile()); if ($properties) { - if (is_string($properties)) { - $properties = [$properties]; + // $properties can only be type array, boolean, integer or float (!=0) or a string + // Cast $properties boolean, integer, float or string to an array + if (!is_array($properties)) { + // If there is only one property, make it accessible as `value` + $properties = ['value' => $properties]; } $class->setProperties($properties); } @@ -284,7 +287,7 @@ private function renderWidgets() } $published = $this->checkPublicationStatus($widget); if (\Yii::$app->user->can($this->rbacEditRole, ['route' => true]) || ($widget->status == 1 && $published == true)) { - $html .= Html::beginTag('div', ['class' => [$visibility,'hrzg-widget-content-frontend']]); + $html .= Html::beginTag('div', ['class' => [$visibility, 'hrzg-widget-content-frontend']]); $html .= $class->run(); $html .= Html::endTag('div'); } @@ -408,7 +411,7 @@ function(xhr) { 'aria-label' => \Yii::t('widgets', 'Toggle visibility status'), 'data' => [ 'button' => 'loading', - 'loading-text' => FA::icon(FA::_SPINNER,['class' => 'fa-spin']), + 'loading-text' => FA::icon(FA::_SPINNER, ['class' => 'fa-spin']), 'html' => true ] ], @@ -428,7 +431,7 @@ function(xhr) { 'data' => [ 'method' => 'delete', 'confirm' => \Yii::t('widgets', 'Are you sure to delete this translation?'), - 'pjax'=> '0', + 'pjax' => '0', 'params' => [ 'returnUrl' => Url::to('') ] @@ -447,7 +450,7 @@ function(xhr) { 'data' => [ 'method' => 'delete', 'confirm' => \Yii::t('widgets', 'Are you sure to delete this translation?'), - 'pjax'=> '0', + 'pjax' => '0', 'params' => [ 'returnUrl' => Url::to('') ] From aadb3b9f465c4f6873361c8dbe529981324141a0 Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Mon, 13 May 2024 09:29:09 +0200 Subject: [PATCH 6/6] updated docs --- docs/examples.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/examples.md b/docs/examples.md index 3183292..b44f93b 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -216,4 +216,17 @@ APP CLAUS WIDGET TEMPLATES (JSON) } } -``` \ No newline at end of file +``` + +*Single type schema* +-------------------- + +```json +{ + "type": "string" +} +``` + +```twig +{{ value }} +```