mirror of
https://github.com/silverstripe/silverstripe-widgets
synced 2024-10-22 17:05:54 +02:00
Making widgets able to be used in a non page controller
This commit is contained in:
parent
17c94ff9ce
commit
0f207180b4
@ -50,15 +50,37 @@ class WidgetController extends Controller {
|
||||
*/
|
||||
public function Link($action = null) {
|
||||
$id = ($this->widget) ? $this->widget->ID : null;
|
||||
|
||||
$segment = Controller::join_links('widget', $id, $action);
|
||||
|
||||
if($page = Director::get_current_page()) {
|
||||
|
||||
$page = Director::get_current_page();
|
||||
if($page && !($page instanceof WidgetController)) {
|
||||
return $page->Link($segment);
|
||||
}
|
||||
|
||||
return Controller::curr()->Link($segment);
|
||||
}
|
||||
|
||||
if ($controller = $this->getParentController()) {
|
||||
return $controller->Link($segment);
|
||||
}
|
||||
|
||||
return $segment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Cycles up the controller stack until it finds a non-widget controller
|
||||
* This is needed because Controller::curr returns the widget controller,
|
||||
* which means any Link function turns into endless loop.
|
||||
*
|
||||
* @return Controller
|
||||
*/
|
||||
public function getParentController() {
|
||||
foreach(Controller::$controller_stack as $controller) {
|
||||
if (!($controller instanceof WidgetController)) {
|
||||
return $controller;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Widget
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user