Skip to content

Commit

Permalink
Fix edit links for referenced modules with route prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MamlukiSn committed Jun 28, 2021
1 parent 9c5f48f commit 81f84c3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
10 changes: 10 additions & 0 deletions config/twill.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,14 @@
'zh-Hans',
'ru',
],
/*
|--------------------------------------------------------------------------
| Admin Module route prefixes
|--------------------------------------------------------------------------
|
| Used to link modules referenced by medias and files and that have route prefixes
|
*/
'module_route_prefixes' => [
],
];
33 changes: 33 additions & 0 deletions src/Helpers/routes_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,36 @@ function isActiveNavigation($navigationElement, $navigationKey, $activeNavigatio
return $urlsAreMatching;
}
}

if (!function_exists('moduleRouteExists')) {
/**
* @param string $moduleName
* @param string $prefix
* @param string $action
* @param array $parameters
* @param bool $absolute
* @return bool
*/
function moduleRouteExists($moduleName, $prefix, $action)
{
// Fix module name case
$moduleName = Str::camel($moduleName);

// Create base route name
$routeName = 'admin.' . ($prefix ? $prefix . '.' : '');

// Prefix it with module name only if prefix doesn't contains it already
if (
config('twill.allow_duplicates_on_route_names', true) ||
($prefix !== $moduleName &&
!Str::endsWith($prefix, '.' . $moduleName))
) {
$routeName .= "{$moduleName}.";
}

// Add the action name
$routeName .= $action;

return Route::has($routeName);
}
}
4 changes: 2 additions & 2 deletions src/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function getOwnerDetails()
'titleKey' => $model->titleKey,
'model'=>$model,
'module'=>$module,
'edit' => moduleRoute($module, config('twill.block_editor.browser_route_prefixes.' . $module), 'edit', $model->id),
'edit' => moduleRouteExists($module,config('twill.module_route_prefixes.' . $module),'edit', $model->id ) ? moduleRoute($module, config('twill.module_route_prefixes.' . $module), 'edit', $model->id) : null,
] : [];

}
Expand All @@ -102,7 +102,7 @@ public function getOwnerDetails()
'titleKey' => $item->titleKey,
'model'=>$item,
'module'=>$module,
'edit' => moduleRoute($module, config('twill.block_editor.browser_route_prefixes.' . $module), 'edit', $item->id),
'edit' => moduleRouteExists($module,config('twill.module_route_prefixes.' . $module),'edit', $item->id ) ? moduleRoute($module, config('twill.module_route_prefixes.' . $module), 'edit', $item->id) : null,
];

})->filter()->values()->toArray();
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function getOwnerDetails()
'titleKey' => $model->titleKey,
'model'=>$model,
'module'=>$module,
'edit' => moduleRoute($module, config('twill.block_editor.browser_route_prefixes.' . $module), 'edit', $model->id),
'edit' => moduleRouteExists($module,config('twill.module_route_prefixes.' . $module),'edit', $model->id ) ? moduleRoute($module, config('twill.module_route_prefixes.' . $module), 'edit', $model->id) : null,
] : [];

}
Expand All @@ -187,7 +187,7 @@ public function getOwnerDetails()
'titleKey' => $item->titleKey,
'model'=>$item,
'module'=>$module,
'edit' => moduleRoute($module, config('twill.block_editor.browser_route_prefixes.' . $module), 'edit', $item->id),
'edit' => moduleRouteExists($module,config('twill.module_route_prefixes.' . $module),'edit', $item->id ) ? moduleRoute($module, config('twill.module_route_prefixes.' . $module), 'edit', $item->id) : null,
];

})->filter()->values()->toArray();
Expand Down

0 comments on commit 81f84c3

Please sign in to comment.