1

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.

0

Call below code instead

Remove:

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

Replace :

  <?php $newsCollection = $this->_getNewsCollection(); ?>
  • Hii @gaurang I tried your solution but it didn't work. My Template is not generated on frontend. Please check the attached updated screenshot of frontend. – MOHAMMED UBED SHAIKH Dec 2 at 12:02
  • call url news/index/index not custom_news – padhiyar gaurang Dec 2 at 12:40
  • The news/index/index takes me to the 404 page. The url should run with custom_news only, as I had mentioned the front router name in my module's config.xml file. – MOHAMMED UBED SHAIKH Dec 2 at 12:54
  • check your logs – padhiyar gaurang yesterday
  • Hello @gaurang, I had checked my logs and it shows this error: "Error in file: "/home/ubuntu/magento1943/app/code/local/Custom/News/sql/news_setup/mysql4-install-0.1.0.php" - SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'custom_news' already exists" – MOHAMMED UBED SHAIKH yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.