0

When get block using controller got fatal error like

Fatal error: Call to a member function getPostUrl() on null in /app/design/frontend/theme/Vendor_Module/templates/post/list/item.phtml on line 16

Here is controller.

<?php

namespace Vendor\Module\Controller\Category;

class Index extends \Magento\Framework\App\Action\Action
{

    public function execute()
    {
        $id = $this->getRequest()->getParam('id');

        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
        $category_postdata = $objectManager->get('\Vendor\Module\Model\PostFactory');

        $layout = $this->_view->loadLayout();
        $this->_view->renderLayout();

        $_postCollection = $category_postdata->create()->getCollection();
        $_postCollection->addCategoryFilter(['in' => $id]);

        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
        $resultJsonFactory = $objectManager->get('\Magento\Framework\Controller\Result\JsonFactory')->create();

        $resultPage = $objectManager->get('\Magento\Framework\View\Result\PageFactory')->create();
        $block_set = $resultPage->getLayout()->createBlock('Vendor\Module\Block\Index')->setTemplate('post/list.phtml');
        $block = $resultPage->getLayout()->createBlock('Vendor\Module\Block\Post\PostList\Item')
                ->setTemplate('post/list/item.phtml')
                ->setChild('test.posts.list.item',$block_set)
                ->setPost($_postCollection)
                ->toHtml($_postCollection);

        $response = $resultJsonFactory->setData($block);

        return $response;
    }
}

Here is item.phtml

<?php
$_post = $this->getPost();
$_postUrl = $_post->getPostUrl(); // line num 16
$_postName = $block->escapeHtml($_post->getTitle(), null, true);
?>

<li class="post-holder col-sm-4 col-xs-12   post-holder-<?php echo $_post->getId() ?>">

    <div class="">
        <div class="post-images">
            <a href="<?php echo $_post->getPostUrl() ?>">
                <img src="<?php echo $block->resize($_post->getThumbnailimage(),335,280); ?>"/>
            </a>
            <h2 class="post-title-hover ">
                <a class="post-item-link"
                   href="<?php echo $_postUrl ?>">
                    <?php echo $_postName; ?>
                </a>
            </h2>
        </div>
        <div class="post-title-holder clearfix">
            <h2 class="post-title">
                <a class="post-item-link"
                   href="<?php echo $_postUrl ?>">
                    <?php echo $_postName; ?>
                </a>
            </h2>
        </div>

    </div>

</li>

Here is block

<?php

namespace Vendor\Module\Block\Post;

class PostList extends \Vendor\Module\Block\Post\PostList\AbstractList
{

    public function getPostHtml($post)
    {
        return $this->getChildBlock('test.posts.list.item')->setPost($post)->toHtml();
    }

}
  • try to debug variable $_postCollection in your controller file after line $_postCollection->addCategoryFilter(['in' => $id]);. it is getting desired data or not. If it will output array of objects if it works well the you need to handle array. – Shashank Kumrawat Nov 1 '18 at 13:48
  • @ShashankKumrawat Yes you are right, but in this case isn't working. So i try to call new template and in block after collection get with encode and decode in template file. Now work for me. – Payal Patel Nov 2 '18 at 7:32
  • sounds good that it is fixed. please input your fix as answer so some other buddy might get help . thanks – Shashank Kumrawat Nov 2 '18 at 8:50
  • Sure, I will input my answer as soon as possible. – Payal Patel Nov 3 '18 at 7:07
0

Please replace,

$_postCollection = $category_postdata->create()->getCollection();
$_postCollection->addCategoryFilter(['in' => $id]);

With below, Please replace,

$_postCollection = $category_postdata->create()->getCollection();
$_postCollection->addCategoryFilter(['in' => $id]);
$_postCollection = $_postCollection->getFirstItem();
0

I got the result using collection in template file, please check below.

../post/list/item.phtml

<?php

$blockObject= $block->getLayout()->createBlock('Vendor\Module\Block\Post\PostList\Item');

$id = $blockObject->getRequest()->getParam('id'); 

$_post = $this->getPost();

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$postFactory = $objectManager->get('\Vendor\Module\Model\PostFactory');
$catFactory = $objectManager->get('\Vendor\Module\Model\CategoryFactory')->create()->getCollection();
$_postCollection = $postFactory->create()->getCollection();

$json = json_encode($_postCollection->getData());
$array = json_decode($json, true); ?>
<ol class="post-list" id="cat_post">
    <?php foreach ($array as $arr) {

    $postdata = $objectManager->create('\Vendor\Module\Model\Post')->load($arr['post_id']);
    $catId = $postdata->getCategories();
    if(in_array($id, $catId)){?>
        <li>
            <?php // Put your code here. ?>
        </li>
    <?php } } ?>
</ol>

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.