Create Sub Page For A Custom Template Admin Controller Prestashop

If you already know how to create a custom page for admin controller you can follow this tutorial and I will show you how to create a sub page of the main custom template admin controller, if not you can follow our tutorial for custom template implementation you can find the link at the end of this tutorial. Is not hard to do a sub page for a custom template you only need to create some conditions and detect when somebody try to enter on the edit page (sub page) or if he wants to open the main page. To detect this things we will pass a parameter in the URL and then in controller we will check $_GET parameters to see what page the visitor wants to see.

Let's assume that our controller class name is: AdminMymoduleController.php, we will take as an example the following html anchor:

<a href="{$link->getAdminLink('AdminMymodule')|escape:'htmlall':'utf-8'}" class="btn btn-default">View Page</a>

If somebody click on the view page button, in the controller we will know the visitor wants to access main page, because no parameter was passed in the url, so we will fetch for him the main page template using an if statement in the initContent function, look at the following code to understand better:

public function initContent(){
  parent::initContent();     
  if(isset($_GET['edit_page'])){
    $page_id = $_GET['edit_page'];
    
    $content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'customtemplates/views/templates/admin/edit-template.tpl'); 
    $this->context->smarty->assign(array(
          'content' => $this->content . $content,
      ));
  }else{
    
    $content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'customtemplates/views/templates/admin/main-templates.tpl');
    $this->context->smarty->assign(array(
          'content' => $this->content . $content,
      ));
  }

}

 

In the above function we check the url with $_GET, if edit_page is set we return to the user edit-template.tpl file which is our edit page template, if the parameter is not set, that's mean the user wants to see main page. That's all you need to do if you want to create sub pages for your main custom template. To process the form data from the edit page you need to apply the same mechanism, I will show you this in the future tutorials.

As I said at the beginning of this tutorial, if you don't know how to create a custom template for admin controller in prestashop follow this tutorial. If you have any questions let them in the comments section.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x