Reader small image

You're reading from  PrestaShop Module Development

Product typeBook
Published inNov 2014
Reading LevelBeginner
Publisher
ISBN-139781783280254
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Fabien Serny
Fabien Serny
author image
Fabien Serny

Fabien Serny is a former core developer at PrestaShop. He has 10 years of experience in web development and e-commerce. He has worked for several big e-commerce companies in France, and then created his own company named 23Prod in late 2010. In 2014, along with two other former core developers from PrestaShop, he launched Froggy Commerce, a platform that sells simple and powerful modules for PrestaShop based on the needs of e-tailers. You can visit his websites http://www.23prod.com and http://www.froggy-commerce.com.
Read more about Fabien Serny

Right arrow

Chapter 6. Admin Controllers and Hooks

Our mymodcomments module is almost complete, but we are missing an important part—we can't administrate comments yet.

In this chapter, you will learn about admin controllers and admin hooks. The admin controllers permit you to add new tabs in your back office and hooks allow you to display information or add features to existing tabs.

We will first create an admin controller that will display the list of comments. We will then use hooks to display comments associated with a product (in the admin product tab) and a customer (in the admin customer tab).

In this chapter, we will see how to do the following:

  • Add an admin controller

  • Use back office hooks

Adding an admin controller


An admin controller has some native methods that permit you to administrate a selected ObjectModel class. As you will see in the following section, creating an admin tab that uses main Create, Read, Update, and Delete (CRUD) actions is very easy.

Adding and installing a new tab to your admin panel

Firstly, we will create a new file named AdminMyModCommentsController.php and we will place it in controllers/admin/ of our module's directory. The file will contain a controller named AdminMyModCommentsController that extends ModuleAdminController:

<?php
class AdminMyModCommentsController extends ModuleAdminController
{

}

Next, we will create a new tab in our back office. You could do so using the admin panel by navigating to Administration | Menus; but, it's not really a plug and play module if merchants have to add the tab themselves. This is why we will add a new method named installTab in our module's main class in mymodcomments.php.

We saw what an ObjectModel class...

Using back office hooks


Admin controllers are useful to create a new tab. However, if you need to alter existing tabs, you will need to use hooks in the back office. In this section, we will use only some of the hooks available. It is recommended that you read the exhaustive list of hooks at the end of the book to see all the various possibilities.

Attaching your module to a product hook

The product administration tool is composed of several sub tabs. There is a hook that permits you to add subtabs on this section: displayAdminProductsExtra.

To do so, you just have to register the hook in the constructor of your main class, (mymodcomments.php):

// Register hooks
if (!$this->registerHook('displayProductTabContent') ||
  !$this->registerHook('displayBackOfficeHeader') ||
  !$this->registerHook('displayAdminProductsExtra') ||
  !$this->registerHook('ModuleRoutes'))
    return false;

You will then create the corresponding method using the same controller system as other hooks:

public function...

Summary


In this chapter, we saw how to create and install AdminController with our module. You learned how to use admin hooks and make Ajax requests in the administration panel.

In the next chapter, we will do something completely new: we will create a carrier module.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
PrestaShop Module Development
Published in: Nov 2014Publisher: ISBN-13: 9781783280254
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Fabien Serny

Fabien Serny is a former core developer at PrestaShop. He has 10 years of experience in web development and e-commerce. He has worked for several big e-commerce companies in France, and then created his own company named 23Prod in late 2010. In 2014, along with two other former core developers from PrestaShop, he launched Froggy Commerce, a platform that sells simple and powerful modules for PrestaShop based on the needs of e-tailers. You can visit his websites http://www.23prod.com and http://www.froggy-commerce.com.
Read more about Fabien Serny