📜 ⬆️ ⬇️

Creating a module for Magento - part 1

The popularity of Magento is constantly growing. There is a lot of new online stores, written on this wonderful engine.
Unfortunately, there is not enough information for developers, therefore, we will correct the situation :)
My plan is to write several articles on how to create a module for Magento , using the example of a blog module.
The original of this article is on my blog .


Initially, we restrict ourselves to the minimum functionality: we will write the administrative part of the module, with the help of which it will be possible to manage blog articles in the admin panel.

The module is created in a separate namespace. Usually, namespace is the name of the module developer's company.
In this article I will create a blog module for Magento in my namespace, Snowcore.
The module name should not contain underscores. All custom modules are created in the / app / code / local directory
My module will be called Snowcore_Blog
')
Let's get straight to the point. What is needed to write a module for Magento ?

Disable Magento Cache


First of all, disable cache in Magento, otherwise we will not be able to see our changes :) You can disable caching in the admin panel: System - Cache Management - in the first tab of the Cache Control, change the All Cache parameter to Disable, click Save cache settings

Creating directory structure


In the module folder, create the following structure:
/ app / code / local / Snowcore / Blog /

     Block
     controllers
     etc
     Helper
     Model
     sql


Add a directory for templates:
/ app / design / frontend / default / default / template / blog
(here the first default is the interface, the second default is the theme)

Module connection


To connect the module, you need to create an XML configuration file:
/app/etc/modules/Snowcore_Blog.xml

<? xml version ="1.0" ? >
< config >
< modules >
< Snowcore_Blog >
< active > true </ active >
< codePool > local </ codePool >
</ Snowcore_Blog >
</ modules >
</ config >



At this stage, Magento already knows about our module. You can enable or disable it by changing the <active> parameter of the configuration file.
You can also control the module through the admin panel: System -> Configuration -> Advanced .
The local value for the <codePool> node indicates that our module belongs to the custom modules and is located in the / app / code / local folder

Creating XML configuration for module


Create a new xml file: /app/code/local/Snowcore/Blog/etc/config.xml

< config >
< modules >
< Snowcore_Blog >
< version > 0.1.0 </ version >
</ Snowcore_Blog >
</ modules >
< frontend >
< routers >
< blog >
< use > standard </ use >
< args >
< module > Snowcore_Blog </ module >
< frontName > blog </ frontName >
</ args >
</ blog >
</ routers >
</ frontend >
< admin >
< routers >
< blog >
< use > admin </ use >
< args >
< module > Snowcore_Blog </ module >
< frontName > blog </ frontName >
</ args >
</ blog >
</ routers >
</ admin >
< adminhtml >
< menu >
< blog module =“ blog>
< title > Blog </ title >
< sort_order > 77 </ sort_order >
< children >
< article module =“ blog>
< title > Manage Articles </ title >
< sort_order > 0 </ sort_order >
< action > blog/adminhtml_article </ action >
</ article >
</ children >
</ blog >
</ menu >
< acl >
< resources >
< all >
< title > Allow Everything </ title >
</ all >
< admin >
< children >
< blog >
< title > Blog Module </ title >
< sort_order > 200 </ sort_order >
</ blog >
</ children >
</ admin >
</ resources >
</ acl >
< layout >
< updates >
< blog >
< file > blog.xml </ file >
</ blog >
</ updates >
</ layout >
</ adminhtml >
< global >
< models >
< blog >
< class > Snowcore_Blog_Model </ class >
< resourceModel > blog_mysql4 </ resourceModel >
</ blog >
< blog_mysql4 >
< class > Snowcore_Blog_Model_Mysql4 </ class >
< entities >
< article >
< table > blog_articles </ table >
</ article >
</ entities >
</ blog_mysql4 >
</ models >
< resources >
< blog_setup >
< setup >
< module > Snowcore_Blog </ module >
</ setup >
< connection >
< use > core_setup </ use >
</ connection >
</ blog_setup >
< blog_write >
< connection >
< use > core_write </ use >
</ connection >
</ blog_write >
< blog_read >
< connection >
< use > core_read </ use >
</ connection >
</ blog_read >
</ resources >
< blocks >
< blog >
< class > Snowcore_Blog_Block </ class >
</ blog >
</ blocks >
< helpers >
< blog >
< class > Snowcore_Blog_Helper </ class >
</ blog >
</ helpers >
</ global >
</ config >



This file defines the basic settings of the module.
The <adminhtml> section is responsible for setting the menu and accessing different resources in the admin part of the application.
The <global> part stores information about the used models and resources, helpers and blocks.

Creating Helper


The helper contains only a couple of lines of code /app/code/local/Snowcore/Blog/Helper/Data.php

<?php
class Snowcore_Blog_Helper_Data extends Mage_Core_Helper_Abstract
{

}



This helper is needed for the translation system to work.
At this stage, we should have a new item in the main admin menu:
Magento admin menu


Immediately create a helper for articles :
/app/code/local/Snowcore/Blog/Helper/Article.php

<?php
class Snowcore_Blog_Helper_Article extends Mage_Core_Helper_Abstract
{

}



Creating models


Magento uses thin models . There are two types of models:

First we need models for the articles .

Create a regular model:
/app/code/local/Snowcore/Blog/Model/Article.php

<?php
class Snowcore_Blog_Model_Article extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$ this ->_init('blog/article');
}
}



In it we indicate the resource model with which this model will work.

Create a resource model :
/app/code/local/Snowcore/Blog/Model/Mysql4/Article.php

<?php
class Snowcore_Blog_Model_Mysql4_Article extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$ this ->_init( 'blog/article' , 'article_id' );
}
}



The second parameter of the _init method is the primary key of the articles table.

Create a collection for articles:
/app/code/local/Snowcore/Blog/Model/Mysql4/Article/Collection.php
The grid works with collections, which shows a list of entities (articles).

<?php
class Snowcore_Blog_Model_Mysql4_Article_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$ this ->_init( 'blog/article' );
}
}



SQL installer for module


The installation of the module (the creation of tables in the database) occurs automatically.
Here the main thing is to correctly specify the version for the module, it must match the version that is assigned in the XML configuration.
/app/code/local/Snowcore/Blog/sql/blog_setup/mysql4-install-0.1.0.php

<?php
$installer = $ this ;

$installer->startSetup();

$installer->run( "

– DROP TABLE IF EXISTS {$this->getTable('blog_articles')};
CREATE TABLE {$this->getTable('blog_articles')} (
`article_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(150) NOT NULL,
`slug` varchar(150) NOT NULL,
`content` text,
`meta_keywords` varchar(255) NOT NULL DEFAULT '',
`meta_description` varchar(160) NOT NULL DEFAULT '',
`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`article_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
"
);

$installer->endSetup();



The installation of the module (the creation of the necessary tables) occurs automatically - when accessing any page. If there are problems with the installation of the module (the tables were not created) - look at the base table of the core_resource , there should be an entry with the value code = blog_setup. Delete the entry, the module will be reinstalled.

Creating a Layout Module


Create new XML:
/app/design/adminhtml/default/default/layout/blog.xml

<? xml version ="1.0" ? >
< layout version ="0.1.0" >
< blog_adminhtml_article_index >
< reference name ="content" >
< block type ="blog/adminhtml_article" name ="article" />
</ reference >
</ blog_adminhtml_article_index >
</ layout >



Creating blocks


Blocks are responsible for displaying the module. In the first article create blocks for the admin.
Initially, we will need a block to display the list of existing records - the grid.

Create a container for the grid:
/app/code/local/Snowcore/Blog/Block/Adminhtml/Article.php

<?php
class Snowcore_Blog_Block_Adminhtml_Article extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
$ this ->_controller = 'adminhtml_article' ;
$ this ->_blockGroup = 'blog' ;
$ this ->_headerText = Mage::helper( 'blog/article' )->__( 'Articles Manager' );
$ this ->_addButtonLabel = Mage::helper( 'blog/article' )->__( 'Add Article' );
parent::__construct();
}
}



Next, create the Grid itself:
/app/code/local/Snowcore/Blog/Block/Adminhtml/Article/Grid.php

<?php
class Snowcore_Blog_Block_Adminhtml_Article_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$ this ->setId( 'blogArticleGrid' );
$ this ->setDefaultSort( 'article_id' );
$ this ->setDefaultDir( 'ASC' );
$ this ->setSaveParametersInSession( true );
}

protected function _prepareCollection()
{
$collection = Mage::getModel( 'blog/article' )->getCollection();
$ this ->setCollection($collection);
return parent::_prepareCollection();
}

protected function _prepareColumns()
{
$ this ->addColumn( 'article_id' , array(
'header' => Mage::helper( 'blog/article' )->__( 'ID' ),
'align' => 'right' ,
'width' => '50px' ,
'index' => 'article_id' ,
));

$ this ->addColumn( 'title' , array(
'header' => Mage::helper( 'blog/article' )->__( 'Title' ),
'align' => 'left' ,
'index' => 'title' ,
));

return parent::_prepareColumns();
}

public function getRowUrl($row)
{
return $ this ->getUrl( '*/*/edit' , array( 'id' => $row->getId()));
}
}



Briefly about the grid:


Controller creation



In this article, the controller will only have index action (displaying a table with articles)
/app/code/local/Snowcore/Blog/controllers/Adminhtml/ArticleController.php

<?php
class Snowcore_Blog_Adminhtml_ArticleController extends Mage_Adminhtml_Controller_action
{
protected function _initAction()
{
$ this ->loadLayout()
->_setActiveMenu( 'blog/article' )
->_addBreadcrumb(Mage::helper( 'adminhtml' )->__( 'Articles Manager' ), Mage::helper( 'adminhtml' )->__( 'Articles Manager' ));
return $ this ;
}

public function indexAction()
{
$ this ->_initAction()
->renderLayout();
}
}



Total


Our controller so far contains an index action, in which the layout and breadcrumbs initialize. At this stage, the contents of the articles table is displayed on the Manage Articles page. The table is still empty, but to check if the grid works, you can manually add a couple of records to the database.
Mage_Adminhtml_Block_Widget_Grid provides the ability to use a filter and sort the list by specified fields.

As a result, we got a working table with the ability to filter and sort the data. In the next article we will make the ability to manage table entries .

Stay tuned for updates !

See you soon ;-)

Source: https://habr.com/ru/post/80108/


All Articles