Skip to content

Commit

Permalink
dashboard object variables for template (#690)
Browse files Browse the repository at this point in the history
* dashboard object variables for template

* changelog ref

* add merge
  • Loading branch information
nadar authored Oct 28, 2021
1 parent ce053f4 commit a198b86
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE

## 4.2.0

+ [#690](https://github.com/luyadev/luya-module-admin/pull/690) Option to pass additional variables to the dashboard objects.
+ [#676](https://github.com/luyadev/luya-module-admin/issues/676) Fixed hidden NgRest attributes issue in the group-by-field select.
+ [#678](https://github.com/luyadev/luya-module-admin/pull/678) Added `icon` property to any NgRest attribute. It allows setting additional icons in CRUD table header and CRUD edit form.
+ [#668](https://github.com/luyadev/luya-module-admin/issues/668) Fixed misleading use of the `note_add` icon.
Expand Down
47 changes: 45 additions & 2 deletions src/dashboard/BaseDashboardObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

use Yii;
use luya\admin\base\DashboardObjectInterface;
use luya\helpers\StringHelper;
use yii\base\BaseObject;

/**
* Base Implementation of an Dashboard Object.
*
* This provides the setters and getters from the {{luya\admin\base\DashboardObjectInterface}}.
*
* @property string $template
* @property string $outerTemplateContent
* @property string $dataApiUrl
* @property string $title
* @author Basil Suter <[email protected]>
* @since 1.0.0
*/
Expand All @@ -25,6 +30,28 @@ abstract class BaseDashboardObject extends BaseObject implements DashboardObject
* @return string Returns the outer template string which can contain the {{template}} variable, but don't have to.
*/
abstract public function getOuterTemplateContent();

/**
* Option content parser varaibles
*
* Pass additional variables into the template.
*
* ```
* 'variables' => [
* 'foo' => 'bar',
* 'time' => function() {
* return time();
* },
* 'title' => ['Key', 'Value'] // equals to: Yii::t('Key', 'Value')
* ]
* ```
*
* The variables can be used as {{foo}} and {{time}} in the template.
*
* @var array An array with key and value, where the key is what is available in the template.
* @since 4.2.0
*/
public $variables = [];

private $_template;

Expand All @@ -47,14 +74,30 @@ public function getTemplate()
}

/**
* Parse the content will replace {{dataApiUrl}}, {{title}}, {{template}} with the content from the object.
* Parse the content will replace {{dataApiUrl}}, {{title}}, {{template}} variables with the content from the object.
*
* @param string $content The content to parse.
* @return string
*/
public function contentParser($content)
{
return str_replace(['{{dataApiUrl}}', '{{title}}', '{{template}}'], [$this->getDataApiUrl(), $this->getTitle(), $this->_template], $content);
$customVars = [];
foreach ($this->variables as $key => $value) {
if (is_array($value)) {
list($category, $message) = $value;
$customVars[$key] = Yii::t($category, $message);
} else {
$customVars[$key] = is_callable($value) ? call_user_func($value, $content, $this) : $value;
}
}

$vars = [
'dataApiUrl' => $this->getDataApiUrl(),
'title' => $this->getTitle(),
'template' => StringHelper::template($this->_template, $customVars, false),
];

return StringHelper::template($content, array_merge($vars, $customVars), true);
}

private $_dataApiUrl;
Expand Down
42 changes: 42 additions & 0 deletions tests/admin/dashboard/DashboardObjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace admintests\admin\dashboard;

use admintests\AdminTestCase;
use luya\admin\dashboard\ListDashboardObject;
use luya\admin\dashboard\TableDashboardObject;

class ListDashboardObjectTest extends AdminTestCase
{
public function testListObject()
{
$o = new ListDashboardObject();
$o->variables = [
'foo' => 'bar',
'date' => time(),
'eval' => function() { return time(); },
'tt' => ['app', 'Value'],
];
$o->setTemplate('{{foo}} - {{ date }} - {{eval}} - {{tt}} - {{item.user.firstname}}');
$s = $o->getTemplate();

$this->assertStringContainsString('Value - {{item.user.firstname}}', $s);
$this->assertStringContainsString('bar', $s);
}

public function testTableObject()
{
$o = new TableDashboardObject();
$o->variables = [
'foo' => 'bar',
'date' => time(),
'eval' => function() { return time(); },
'tt' => ['app', 'Value'],
];
$o->setTemplate('{{foo}} - {{ date }} - {{eval}} - {{tt}} - {{item.user.firstname}}');
$s = $o->getTemplate();

$this->assertStringContainsString('Value - {{item.user.firstname}}', $s);
$this->assertStringContainsString('bar', $s);
}
}

0 comments on commit a198b86

Please sign in to comment.