Skip to content
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

make requestParamName used in WidgetContent::getFrontendRoute configurable #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
36 changes: 35 additions & 1 deletion src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,46 @@ class Module extends \yii\base\Module
public $timezone = 'UTC';

/**
* @var array mappings for links
* mappings for links
*
* can be used to map route and requestParam attributes from WidgetContent
* Models to frontend URLs
*
* the elements can be
* - simple string to string mappings
* - string to array mappings where route and requestParam Name can be defined
*
* example:
* ```php
* [
* 'app/site/index' => '/',
* 'pages/default/page' => 'pages/default/page',
* 'frontend/tag/detail' => [
* 'route' => 'frontend/tag/detail',
* 'requestParamName' => 'tagId',
* ],
* ]
* ```
*
* @see \hrzg\widget\models\crud\WidgetContent::getFrontendRoute
* @var array
*/
public $frontendRouteMap = [
'app/site/index' => '/',
];

/**
* default name used as RequestParamName when generating frontend URLs
* can be overwritten for each route in self::$frontendRouteMap
*
* BC: define 'pageId' as default
*
* @see self::$frontendRouteMap
* @see \hrzg\widget\models\crud\WidgetContent::getFrontendRoute
* @var string
*/
public $frontendDefaultRequestParamName = 'pageId';

/**
* set ajax option for JsonEditor
*
Expand Down
32 changes: 25 additions & 7 deletions src/models/crud/WidgetContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,32 @@ public function getViewFile()
public function getFrontendRoute()
{
$mapping = \Yii::$app->controller->module->frontendRouteMap[$this->route] ?? false;

if ($mapping) {
return Url::to(
[
'/' . $mapping,
'pageId' => $this->request_param,
'#' => 'widget-' . $this->domain_id
]);
// init params
$route = null;
// BC for <= 2.7.4: define 'pageId' as default paramName if not defined in Modul or in frontendRouteMap
$requestParamName = \Yii::$app->controller->module->frontendDefaultRequestParamName ?? 'pageId';
// simple route mapping
if (is_string($mapping)) {
$route = ltrim($mapping, '/');
}
// route and (optional) requestParamName mapping via array
if (is_array($mapping)) {
if (!empty($mapping['route']) && is_string($mapping['route'])) {
$route = ltrim($mapping['route'], '/');
}
if (!empty($mapping['requestParamName']) && is_string($mapping['requestParamName'])) {
$requestParamName = $mapping['requestParamName'];
}
}
if ($route) {
return Url::to(
[
'/' . $route,
$requestParamName => $this->request_param,
'#' => 'widget-' . $this->domain_id
]);
}
}

return false;
Expand Down