Skip to content

Add a method for widget template to static cache the template and prevent multiple calls #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions src/widgets/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,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');
}
Expand All @@ -292,8 +292,27 @@ private function renderWidgets()
}

/**
* @return string
* Widget template id as key and name as value.
*/
protected function widgetTemplateItems(): array
{
// Static cache for template to prevent multiple calls
static $templates;
if (!is_array($templates)) {
$templates = WidgetTemplate::find()
->indexBy('id')
->select('name')
->andWhere(['php_class' => TwigTemplate::class])
->andWhere(['hide_in_list_selection' => WidgetTemplate::IS_VISIBLE_IN_LIST])
->orderBy('name')
->column();
}
return $templates;
}

/**
* @throws \Exception
* @return string
*/
private function generateCellControls()
{
Expand All @@ -302,21 +321,16 @@ private function generateCellControls()
['label' => $this->id]
];

$templates = WidgetTemplate::find()
->where(['php_class' => TwigTemplate::class])
->andWhere(['hide_in_list_selection' => WidgetTemplate::IS_VISIBLE_IN_LIST])
->orderBy('name')
->all();
foreach ($templates as $template) {
foreach (self::widgetTemplateItems() as $templateId => $templateName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method widgetTemplateItems() is not static and should therefore be called here via $this->widgetTemplateItems(), or am I missing something?

$items[] = [
'label' => $template->name,
'label' => $templateName,
'url' => [
'/' . $this->moduleName . '/crud/widget/create',
'WidgetContent' => [
'route' => $this->getDefaultRoute(),
'container_id' => $this->id,
'request_param' => \Yii::$app->request->get($this->requestParam),
'widget_template_id' => $template->id,
'widget_template_id' => $templateId,
],
],
'linkOptions' => [
Expand Down Expand Up @@ -405,7 +419,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
]
],
Expand All @@ -425,7 +439,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('')
]
Expand All @@ -444,7 +458,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('')
]
Expand Down