Wednesday , December 19 2018
Home >> All tutorial >> Magento Developers' Guide – Part 6: Create a plugin in the backend

Magento Developers' Guide – Part 6: Create a plugin in the backend

After the last tutorial, today we will continue with the next part about “Create a plugin the backend”

1 – Magento and administration

Before starting like a warrior on the code, let’s start by understanding how the administration of magento works.

First of all you have to know that in the admin plugin it is always the same, there will be a « code » part and a « template » part (the principle of MVC … separate the code and the view), the part code is found in the « code » folders of your plugin in a directory called Adminhtml.

Thus, administration Blocks of your plugin will be found in /app/code/local/Pfay/Test/Block/Adminhtml/

The controllers will be found in /app/code/local/Pfay/Test/controllers/Adminhtml/

And model is the same for the admin and the front part, of course we will not create a new one when it already existing)

For the backoffice part, you’ll search your templates and your layout in /app/design/adminhtml/default/Pfay/, once you are here, it’s the same as for the front, you have your file layout and your folder template where you store your layout and your respective templates.

2 – What have to to?

Obviously you are used, we start by editing the config.xml file in our plugin, go to /app/code/local/Pfay/Test/etc/.

After <frontend> and before <global> add:

  1. <admin>
  2.      <routers>
  3.          <test>
  4.             <use>admin</use>
  5.             <args>
  6.                <module>Pfay_Test</module>
  7.                <frontName>admintest</frontName>
  8.             </args>
  9.          </test>
  10.       </routers>
  11.  </admin>
  12.  <adminhtml>
  13.    <layout>
  14.       <updates>
  15.           <test>
  16.               <file>test.xml</file>
  17.            </test>
  18.       </updates>
  19.    </layout>
  20.    <menu>
  21.       <test translate=“title” module=“adminhtml”>
  22.          <title>My plugins</title>
  23.          <sort_order>100</sort_order>
  24.          <children>
  25.              <set_time>
  26.                    <title>Adress book</title>
  27.                    <action>admintest/adminhtml_index</action>
  28.               </set_time>
  29.           </children>
  30.        </test>
  31.     </menu>
  32. </adminhtml>
  1. <admin>i
  2.       <routers>
  3.           <test>
  4.              <use>admin</use>
  5.              <args>
  6.                 <module>Pfay_Test</module>
  7.                 <frontName>admintest</frontName>
  8.              </args>
  9.           </test>
  10.        </routers>
  11.   </admin>
First we create an administration router, the tag is set to use the admin can say that it will be an administration plugin (as opposed to if we had used the standard value).
To access to the admin part of our plugin, we define a shortcut to the url thanks to the attribute frontName that we make as « admintest ». To access our plugin we will therefore go to the URL:
  1. <adminhtml>
  2.    <layout>
  3.       <updates>
  4.           <test>
  5.               <file>test.xml</file>
  6.            </test>
  7.       </updates>
  8.    </layout>
  9.    <menu>
  10.       <test translate=“title” module=“adminhtml”>
  11.          <title>My plugins</title>
  12.          <sort_order>100</sort_order>
  13.          <children>
  14.              <set_time>
  15.                    <title>Adress book</title>
  16.                    <action>admintest/adminhtml_index</action>
  17.               </set_time>
  18.           </children>
  19.        </test>
  20.     </menu>
  21. </adminhtml>

Then, we define that the layout used to load this plugin will be file test.xml who will be in the folder /app/design/adminhtml/default/Pfay/layout/

Then thanks to the the tag menu we’re creating the menu for the administration

The tag is easy to decode, it says:

Title: the title of the column (here « My plugins »)
set_order: the order in the menu (here 100 to put it at the end)

Then each children of this tag is a line of the menu. For each item children, there is a name and a link … In fact, each children menu is just a shortcut, if you will click on it, you are redirected to the page (in our example to )

3 – Create the controller of the admin part

Create the folder appcodelocalPfayTestcontrollersAdminhtml and the file IndexController.php in it which will contain:

  1. class Pfay_Test_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action
  2. {
  3.   public function indexAction()
  4.   {
  5.           $this->loadLayout();
  6.           $this->renderLayout();
  7.   }
  8. }

If you want to find explanation for this file, please see  the previous tutorials

4 – Add the form in the admin interface

Now your plugin works and can be accessed via the menu, even if (for now) it does nothing.

To save time, copy your file that contains your form phtml to the admin of magento, take the file afficher.phtml located in /app/design/frontend/pfay/theme/template/test/ and copy it to /app/design/ adminhtml /default/ Pfay /template/test/ in your form and add the following hidden field (required for security in magento)

  1. <input name=”form_key” type=”hidden” value=”<?php echo Mage::getSingleton(‘core/session’)->getFormKey() ?>”  />
Now, change the layout test.xml in /app/design/adminhtml/default/Pfay/layout/ like this:
  1. <?xml version=“1.0” ?>
  2. <layout version=“0.1.0”>
  3.   <test_adminhtml_index_index>
  4.      <reference name=“content”>
  5.              <block type=“test/monblock”  name=“afficher_monbloc”
  6.                   template=“test/afficher.phtml” />
  7.       </reference>
  8.    </test_adminhtml_index_index>
  9. </layout>
As you have copied the template in your template admin directory, it combines the block from the front with your template admin and it works, you now have your form in the administration.

Here is the library of Magento Download for FREE (all versions)

Your Magento website needs a powerful Magento ERP for stock control.


magento erp

About admin admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics