Magento 1. Display the list of articles and main page of articles in frontend. Part 7

July 16, 2017 18 Yehor Rykhnov

Displaying a list of articles on the page of our module and main page of articles.

Articles list and main page of articles

To output data, Magento uses block classes (we already used blocks in the admin panel such as Grid, Edit ...). The block carries business logic and this block can be used in different parts of the page. The controller only initializes the desired block.

To display the information, we need to create:

  1. block of class for listing articles
  2. block of class to display the main page of articles
  3. create xml layout file in template
  4. add information about blocks to the module settings
  5. add the functionality of outputting blocks to the controller
  6. create a template for a list of articles
  7. create a template for the main articles page

Create a block to display a list of articles

A class implements a function that returns a collection of records.

To do this, we need to create a file:

app/code/local/My/Articles/Block/Articles.php

Adding the code:

<?php

class My_Articles_Block_Articles extends Mage_Core_Block_Template
{

   public function getArticlesCollection()
   {
       $collection = Mage::getModel("myarticles/articles")->getCollection();
       $collection->setOrder('created', 'DESC');
       return $collection;
   }

}

Create a class to display the main page of articles

Create a file:

app/code/local/My/Articles/Block/View.php

And add the code:

<?php

class My_Articles_Block_View  extends Mage_Core_Block_Template
{
}

Create an xml layout file in the template

In the catalog of your theme (for me: design/frontend/default/default), create a directory (if there is none):

design/frontend/default/default/layout

and file:

design/frontend/default/default/layout/my_articles.xml

Adding the code:

<layout>
   <marticles_index_index>
       <reference name="content">
           <block type="my_articles_block/articles" template="my_articles/index.phtml" />
       </reference>
   </marticles_index_index>
   <marticles_index_view>
       <reference name="content">
           <block type="my_articles_block/articles" name="my_articles.content" template="my_articles/view.phtml" />
       </reference>
   </marticles_index_view>
</layout>

Add the information about blocks to the module settings

In the configuration file of our module (app/code/local/My/Articles/etc/config.xml)

Here we add parameters (see comments):

  1. Data for layout
  2. Data for the layout of our module

Add the code:

<?xml version="1.0" ?>
<config>
   <modules>
       <My_Articles>
           <version>1.0.0</version>
       </My_Articles>
   </modules>
   <frontend>
    <!-- Data for layout -->
    <layout>
           <updates>
               <!-- Data for the layout of our module -->
               <my_articles_block>
                   <file>my_articles.xml</file>
               </my_articles_block>
           </updates>
       </layout>
       <routers>
           <marticles>
               <use>standard</use>
               <args>
                   <module>My_Articles</module>
                   <frontName>articles</frontName>
               </args>
           </marticles>
       </routers>
   </frontend>
   <global>
       <!-- Code for models -->
       <models>
           <myarticles>
               <!-- We created the class My_Articles_Model_Articles, in
                 In this parameter, we only specify the path to the My_Articles_Model file without
                 File name Articles -->
               <class>My_Articles_Model</class>
               <resourceModel>myarticles_recource</resourceModel>
           </myarticles>
           <!-- The code for the model resources -->
           <myarticles_recource>
               <class>My_Articles_Model_Resource</class>
               <entities>
                   <table_myarticles>
                       <table>my_articles</table>
                   </table_myarticles>
               </entities>
           </myarticles_recource>
       </models>
       <resources>
           <My_Articels_marticles_setup>
               <setup>
                   <module>My_Articles</module>
               </setup>
           </My_Articels_marticles_setup>
       </resources>
        <!-- Code for the block -->
       <blocks>
           <my_articles_block>
               <class>My_Articles_Block</class>
           </my_articles_block>
       </blocks>
       <!-- Helper code -->
       <helpers>
           <myarticles>
               <class>My_Articles_Helper</class>
           </myarticles>
       </helpers>
   </global>
   <!-- Code for admin backend -->
   <admin>
       <routers>
           <myarticles_admin>
               <use>admin</use>
               <args>
                   <module>My_Articles</module>
                   <frontName>myarticles_admin</frontName>
               </args>
           </myarticles_admin>
       </routers>
   </admin>
   <!--Code for the admin menu -->
   <adminhtml>
       <menu>
           <myarticles module="myarticles">
               <title>My Articles</title>
               <sort_order>77</sort_order>
               <action>myarticles_admin/adminhtml_articles</action>
           </myarticles>
       </menu>
   </adminhtml>
</config>

Add the functionality of outputting blocks to the controller

Open the file:

app/code/local/My/Articles/controllers/IndexController.php

Adding the code:

<?php

class My_Articles_IndexController extends Mage_Core_Controller_Front_Action
{

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

   public function viewAction()
   {
       $id = Mage::app()->getRequest()->getParam('id', 0);
       $articles = Mage::getModel('myarticles/articles')->load($id);
       if ($articles->getId() > 0) {
           $this->loadLayout();
           $this->getLayout()->getBlock('my_articles.content')->assign(array(
               "item" => $articles,
           ));
           $this->renderLayout();
       } else {
           $this->_forward('noRoute');
       }
   }

}

Create a template for the list of articles.

In the catalog of your theme (for me: design/frontend/default/default), create a directory (if it is not):

design/frontend/default/default/template

and

design/frontend/default/default/template/my_articles

Create a file:

design/frontend/default/default/template/my_articles/index.phtml

Adding the code:

<?php

$items = $this->getArticlesCollection();
$articleViewUrl = Mage::getUrl('articles/index/view');
$helper = Mage::helper('myarticles');
?>
   <h1>Articles</h1>
   <?php foreach ($items as $item): ?>
       <div>
           <div>
               <a href="<?php echo $articleViewUrl; 
?>?id=<?php echo $item->getId(); ?>" title="<?php echo 
$item->getHeader(); ?>">
                   <?php echo $item->getHeader(); ?>
               </a>
           </div>
           <div style="float: left; width: 100px;">
               <?php if (is_file($helper->getImagePath($item->getId()))): ?>
               <a href="<?php echo $articleViewUrl; 
?>?id=<?php echo $item->getId(); ?>" title="<?php echo 
$item->getHeader(); ?>">
                   <img 
alt="<?php echo $item->getHeader(); ?>" style="width: 90px; 
height: 90px;" src="<?php echo 
$helper->getImageUrl($item->getId())?>">
               </a>
               <?php endif; ?>
           </div>
           <div style="float: left; margin-left: 10px;">
               <?php echo $item->getPreview(); ?>
               <p><a href="<?php echo $articleViewUrl; 
?>?id=<?php echo $item->getId(); ?>" title="<?php echo 
$item->getHeader(); ?>">Read More</a></p>
           </div>
           <div style="clear: both"></div>
       </div>
<?php endforeach; ?>

Create a template for the article page

Create a file:

design/frontend/default/default/template/my_articles/view.phtml

Adding the code:

<?php

$helper = Mage::helper('myarticles');
?>
<h1><?php echo $item->getTitle(); ?></h1>
<div style="float: left; width: 150px; margin-right: 10px;">
   <?php if (is_file($helper->getImagePath($item->getId()))): ?>
           <img alt="<?php echo $item->getHeader(); ?>" 
style="width: 150px; height: 150px; " src="<?php echo 
$helper->getImageUrl($item->getId())?>">
   <?php endif; ?>
</div>
<div class="content" style="float: left;"><?php echo $item->getContent(); ?></div>
<div style="clear: both"></div>

Done.

Article Source: https://кодер.укр/записи/magento-1-вывод-списка-записей-и-страница-статьи-на-frontend-часть-7.