1

what is missing in my code block data not shown in phtml template

block page

namespace xxxx\Trails\Block;

use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
use \xxxx\Trails\Model\ResourceModel\Post\Collection as PostCollection;
use \xxx\Trails\Model\ResourceModel\Post\CollectionFactory as PostCollectionFactory;
use \xxx\Trails\Model\Post;

class Users extends Template
{
    /**
     * CollectionFactory
     * @var null|CollectionFactory
     */
    protected $_postCollectionFactory = null;
    public $sortOrder = 0;
    protected $customerSession;

    /**
     * Constructor
     *
     * @param Context $context
     * @param PostCollectionFactory $postCollectionFactory
     * @param \Magento\Customer\Model\Session $customerSession
     * @param array $data
     */
    public function __construct(
        Context $context,
        PostCollectionFactory $postCollectionFactory,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    ) {
        $this->customerSession = $customerSession;
        $this->_postCollectionFactory = $postCollectionFactory;
        parent::__construct($context, $data);
        $this->sortOrder = $this->getRequest()->getParam('sort');
    }

    /**
     * @return Post[]
     */
    public function getUsers()
    {
        /** @var PostCollection $postCollection */
//                $customerId=$this->getLayout()->createBlock('Scaledesk\Trails\Block\Session')->getCustomerId();

        $customerId=$this->customerSession->getCustomer()->getId();

//        var_dump($customerId);


        $postCollection = $this->_postCollectionFactory->create();
        $search = trim($this->getRequest()->getParam('search'));
        $sort = trim($this->getRequest()->getParam('sort'));
        $postCollection->addFieldToSelect('*');
        $postCollection->addFieldToFilter('main_table.entity_id',$customerId);
        $postCollection->getSelect()->join( array('customer_entity'=> $postCollection->getTable('customer_entity')), 'customer_entity.entity_id = main_table.entity_id');
        if($search != '') {
            $postCollection->addFieldToFilter('trails_name',
                array('like' => '%' . $search . '%'));
        }
        if ($sort != '') {

            // $postCollection->setOrder('created_at', ($sort==1 ? 'asc':'desc'));
            $postCollection->setOrder('trails_id', ($sort==1 ? 'asc':'desc'));

        }
//           var_dump($postCollection->getSelect()->__toString());
//        die();

        $postCollection->load();
        var_dump($postCollection->getItems()); die();
        return $postCollection->getItems();



    }

    /**
     * For a given post, returns its url
     * @param Post $post
     * @return string
     */
    public function getPostUrl(
        Post $post
    ) {
        return $this->getUrl().'trails/post/view/id/'. $post->getId();
    }




}

and phtml template

 <?php

//    var_dump(getUserAllTrails());
      var_dump($block->getUsers());

//    foreach ($block->getTrails() as $post){
//    echo $post->getTrailsName();
//    }
//
    ?>

xml file

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">


        <block class="xxx\Trails\Block\Users"
               name="post.users"
               template="xxx_Trails::post/trail.phtml" />
    </referenceContainer>
</body>
</page>

controller page

namespace xxx\Trails\Controller\Post;
use \Magento\Framework\App\Action\Action;
use \Magento\Framework\View\Result\PageFactory;
use \Magento\Framework\View\Result\Page;
use \Magento\Framework\App\Action\Context;
use \Magento\Framework\Exception\LocalizedException;
use \Magento\Framework\Registry;


class Users extends Action
{
    const REGISTRY_KEY_POST_ID = 'xxxx_trails_trails_id';

    /**
     * Core registry
     * @var Registry
     *
     */
    protected $customerSession;


    protected $_coreRegistry;

    /**
     * @var PageFactory
     */
    protected $_resultPageFactory;

    /**
     * @param Context $context
     * @param Registry $coreRegistry
     * @param PageFactory $resultPageFactory
     *
     * @codeCoverageIgnore
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        Context $context,
        Registry $coreRegistry,
        PageFactory $resultPageFactory





    ) {
        parent::__construct(
            $context
        );
        $this->_coreRegistry = $coreRegistry;
        $this->_resultPageFactory = $resultPageFactory;



    }

    /**
     * Saves the blog id to the register and renders the page
     * @return Page
     * @throws LocalizedException
     */

    public function execute()
    {


        $blockInstance = $this->_objectManager->get('xxxx\Trails\Block\Users');
        $blockInstance->getUsers();

            $this->_coreRegistry->register(self::REGISTRY_KEY_POST_ID, (int)$this->_request->getParam('id'));
            $resultPage = $this->_resultPageFactory->create();
            return $resultPage;

    }
0

Suppose if your route is xxxx and controller file name is Index.php and method name is Index then you need to create xxxx_index_index.xml file under this path xxxx\Trails\view\frontend\layout.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

    <body>
        <referenceContainer name="content">
            <block class="xxxx\Trails\Block\Users" name="test-block" template="test.phtml" />
        </referenceContainer>
    </body>
</page>

Or you can call direct block in your file this way also

<?php
$blockObj= $block->getLayout()->createBlock('xxxx\Trails\Block\Users');
echo $blockObj->getUsers();
?>
  • I want to print on a template or not on a controller – Nitesh Kumar Singh May 22 '18 at 10:59
  • <?php var_dump($block->getUsers()); ?> Phtml template – Nitesh Kumar Singh May 22 '18 at 10:59
  • Above code for you can call block methods in phtml file. – Yogesh May 22 '18 at 11:00
  • on controller and block page working but on a template not working – Nitesh Kumar Singh May 22 '18 at 11:02
  • array is empty on template – Nitesh Kumar Singh May 22 '18 at 11:05
0

You did the mistake. You can't render a block using below code from controller

    $blockInstance = $this->_objectManager->get('xxxx\Trails\Block\Users');
    $blockInstance->getUsers()

first ,you need to call layout then render the block via layout file or code.

  • I want to print on a template or not on a controller. – Nitesh Kumar Singh May 22 '18 at 10:58

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.