Question

I had created a custom module for the news grid and form in Magento admin. I need to create custom pages with news collection on my custom news list page and a custom news detail page. How could I get the collection on frontend in magento 1.9?

I had created the custom admin grid please check the attached screenshot for the same. enter image description here

This is my custom news frontend page. enter image description here

Here is my custom layout file:

app/design/frontend/MyCustom/Theme/layout/news.xml

<?xml version="1.0"?>
<layout version="0.1.0">
<news_index_index>
    <reference name="content">
        <block type="news/news" name="news" template="custom/news/news.phtml" />
    </reference>
</news_index_index>
</layout>

Here is my Block file:

app/code/local/Custom/News/Block/News.php

<?php class Custom_News_Block_News extends Mage_Core_Block_Template { 
public function __construct()
{
    parent::__construct();
    $news = Mage::getModel('news/news')->getNews();
    $this->setNews($news);
}

protected function _prepareLayout()
{
    parent::_prepareLayout();

    $toolbar = $this->getToolbarBlock();

    // called prepare sortable parameters
    $news = $this->getNews();

    // use sortable parameters
    if ($orders = $this->getAvailableOrders()) {
        $toolbar->setAvailableOrders($orders);
    }
    if ($sort = $this->getSortBy()) {
        $toolbar->setDefaultOrder($sort);
    }
    if ($dir = $this->getDefaultDirection()) {
        $toolbar->setDefaultDirection($dir);
    }
    $toolbar->setNews($news);

    $this->setChild('toolbar', $toolbar);
    $this->getNews()->load();
    return $this;
}

protected function _getNewsCollection()
{
    if (is_null($this->_newsCollection)) {
        $newsCollection = Mage::getModel('news/news')->getNewsCollection();
        $newsCollection
        ->addAttributeToSelect(Mage::getSingleton('news/news')->getProductAttributes())
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents();

        Mage::getSingleton('news/news_status')->addVisibleFilterToCollection($newsCollection);
        Mage::getSingleton('news/news_visibility')->addVisibleInCatalogFilterToCollection($newsCollection);
        $this->_newsCollection = $newsCollection;

    }
    return $this->_newsCollection;
}

public function getDefaultDirection(){
    return 'asc';
}
public function getAvailableOrders(){
    return array('created_time'=> 'Created Time','update_time'=>'Updated Time','id'=>'ID');
}
public function getSortBy(){
    return 'id';
}
public function getToolbarBlock()
{
    $block = $this->getLayout()->createBlock('news/toolbar', microtime());
    return $block;
}
public function getMode()
{
    return $this->getChild('toolbar')->getCurrentMode();
}

public function getToolbarHtml()
{
    return $this->getChildHtml('toolbar');
} }

Here is my template file to get news list:

app/design/frontend/MyCustom/Theme/template/custom/news/list.phtml

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?> 
<?php $newsCollection = $this->getNewsCollection(); ?>
<div class="custom-news">
    <h1><?php echo $this->__('My News Collection') ?></h1>
</div>
<?php echo $this->getToolbarHtml(); ?>
<?php if($collection->getSize()): ?>

<?php if($this->getMode() == 'list'){ ?>
<table class="custom-news-table" id="custom_news">
    <col width="1" />
    <col width="1" />
    <col />
    <col width="1" />
    <col width="1" />
    <col width="1" />
    <thead>
        <tr>
            <th><?php echo $this->__('ID #') ?></th>
            <th><?php echo $this->__('Title') ?></th>
            <th><span class="nobr"><?php echo $this->__('Created') ?></span></th>
        </tr>
    </thead>
    <tbody>
        <?php $_odd = ''; ?>
        <?php foreach ($news as $_obj): ?>
        <tr>
            <td><?php echo $_obj->getId() ?></td>
            <td><span class="nobr"><?php echo $_obj->getTitle(); ?></span></td>
            <td><?php echo $this->formatDate($_obj->getCreatedTime()) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<script type="text/javascript">decorateTable('custom_news');</script>
<?php }else{ ?>
    <!-- List Mode HTML Here -->
<?php } ?>
<?php echo $this->getToolbarHtml(); ?>
<?php else: ?>
    <p><?php echo $this->__('The News collection is empty.'); ?></p>
<?php endif; ?>

Please provide the solution to get the custom collection on the frontend with list and detail pages.

Was it helpful?

Solution 2

I changed my method and got the custom collection.

Removed:

protected function _getNewsCollection()
{
    if (is_null($this->_newsCollection)) {
        $newsCollection = Mage::getModel('news/news')->getNewsCollection();
        $newsCollection
        ->addAttributeToSelect(Mage::getSingleton('news/news')->getProductAttributes())
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents();

        Mage::getSingleton('news/news_status')->addVisibleFilterToCollection($newsCollection);
        Mage::getSingleton('news/news_visibility')->addVisibleInCatalogFilterToCollection($newsCollection);
        $this->_newsCollection = $newsCollection;

    }
    return $this->_newsCollection;
}

Replaced:

public function getCollection()
{
    $newsCollection = Mage::getModel('news/news')->getCollection();
    $newsCollection->prepareForList($this->getCurrentPage());
    return $newsCollection;
}

OTHER TIPS

Call below code instead

Remove:

 <?php $newsCollection = $this->getNewsCollection(); ?>

Replace :

  <?php $newsCollection = $this->_getNewsCollection(); ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top