From 487235f9918b762f9eadcaaa655602da884807a6 Mon Sep 17 00:00:00 2001 From: Tim Kung Date: Fri, 7 Apr 2017 14:41:15 +1200 Subject: [PATCH] adding page id into cmsmain --- code/controllers/CMSMain.php | 56 +++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/code/controllers/CMSMain.php b/code/controllers/CMSMain.php index 9f307bd9..b9b82874 100644 --- a/code/controllers/CMSMain.php +++ b/code/controllers/CMSMain.php @@ -15,6 +15,10 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr private static $url_rule = '/$Action/$ID/$OtherID'; + private static $url_handlers = array( + 'EditForm/$ID' => 'EditForm', + ); + // Maintain a lower priority than other administration sections // so that Director does not think they are actions of CMSMain private static $url_priority = 39; @@ -66,6 +70,13 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr */ private static $enabled_legacy_actions = array(); + /** + * The page id for this request + * + * @var int|null + */ + protected $pageID = null; + public function init() { // set reading lang if(SiteTree::has_extension('Translatable') && !$this->getRequest()->isAjax()) { @@ -592,6 +603,29 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr } } + /** + * Ensuring we set the current page id from the $ID url parameter. + * + * @param SS_HTTPRequest $request + * + * @return Form + */ + public function EditForm($request = null) { + // set page ID from request + if ($request) { + // validate id is present + $id = $request->param('ID'); + + if (!isset($id)) { + return $this->httpError(400); + } + + $this->setCurrentPageID($id); + } + + return $this->getEditForm(); + } + /** * @param int $id * @param FieldList $fields @@ -690,6 +724,9 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $form->setFields($readonlyFields); } + // update form action to include $pageID + $form->setFormAction(Controller::join_links($form->FormAction(), $id)); + $this->extend('updateEditForm', $form); return $form; } else if($id) { @@ -866,8 +903,25 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr return $listview; } + /** + * Set the page id into $pageID rather than into {@link Session}. + * + * @param string|int $id + * + * @return void + */ + public function setCurrentPageID($id) { + $id = (int)$id; + $this->pageID = $id; + } + + /** + * Get the page id from this request + * + * @return int + */ public function currentPageID() { - $id = parent::currentPageID(); + $id = $this->pageID; $this->extend('updateCurrentPageID', $id);