0

When I try to call block method in controller it returns me null value but when I run same block method in view(.phtml) it returns right value.

I'm trying to get number of posts in controller so I can add rel prev and rel next in header. Since I cannot edit header from block i need this data in controller.

Is this possible, can i pass data from block to controller? By calling getPosts function in view i'm getting all posts. I want to do same think but in controllers because i need to edit header of page.

Here is Block class:

 <?php
/**
 * @category    Fishpig
 * @package     Fishpig_Wordpress
 * @license     http://fishpig.co.uk/license.txt
 * @author      Ben Tideswell <help@fishpig.co.uk>
 */

class Fishpig_Wordpress_Block_Post_List extends Fishpig_Wordpress_Block_Post_Abstract
{
  /**
   * Renderer and template information for post types
   *
   * @var array
   */
  protected $_postTypeTemplates = array(
    'post' => 'wordpress/post/list/renderer/default.phtml',
  );

  /**
   * Cache for post collection
   *
   * @var Fishpig_Wordpress_Model_Resource_Post_Collection
   */
  protected $_postCollection = null;

  /**
   * Returns the collection of posts
   *
   * @return Fishpig_Wordpress_Model_Mysql4_Post_Collection
   */
  public function getPosts()
  {
    return $this->_getPostCollection();
  }
  /**
   * Returns the collection of posts
   *
   * @return Fishpig_Wordpress_Model_Mysql4_Post_Collection
   */
  public function getPostsHomepage()
  {
    return $this->_getPostCollectionHomepage();
  }

  /**
   * Generates and returns the collection of posts
   *
   * @return Fishpig_Wordpress_Model_Mysql4_Post_Collection
   */
  protected function _getPostCollection()
  {
    if (is_null($this->_postCollection) && $this->getWrapperBlock()) {
      $this->_postCollection = $this->getWrapperBlock()->getPostCollection();

      if ($this->getPagerBlock() && $this->_postCollection) {
        $this->getPagerBlock()->setCollection($this->_postCollection);
      }
    }
    var_dump(count($this->_postCollection));die();
    return $this->_postCollection;
  }

  /**
   * Generates and returns the collection of posts
   *
   * @return Fishpig_Wordpress_Model_Mysql4_Post_Collection
   */
  protected function _getPostCollectionHomepage()
  {
    if (is_null($this->_postCollection) && $this->getWrapperBlock()) {
      $this->_postCollection = $this->getWrapperBlock()->getPostCollection()->addCategoryAndPostIdFilter(array(), array(2, 80, 75, 1, 12, 79));

      if ($this->getPagerBlock() && $this->_postCollection) {
        $this->getPagerBlock()->setCollection($this->_postCollection);
      }
    }

    return $this->_postCollection;
  }

  /**
   * Sets the parent block of this block
   * This block can be used to auto generate the post list
   *
   * @param Fishpig_Wordpress_Block_Post_List_Wrapper_Abstract $wrapper
   * @return $this
   */
  public function setWrapperBlock(Fishpig_Wordpress_Block_Post_List_Wrapper_Abstract $wrapper)
  {
    return $this->setData('wrapper_block', $wrapper);
  }

  /**
   * Get the pager block
   * If the block isn't set in the layout XML, it will be created and will use the default template
   *
   * @return Fishpig_Wordpress_Post_List_Pager
   */
  public function getPagerBlock()
  {
    if (!$this->hasPagerBlock()) {
      $this->setPagerBlock(false);

      if ($pager = $this->getChild('pager')) {
        $this->setPagerBlock(
          $pager->setPostListBlock($this)
        );
      }
    }

    return $this->_getData('pager_block');
  }

  /**
   * Get the HTML for the pager block
   *
   * @return string
   */
  public function getPagerHtml()
  {
    return $this->getChildHtml('pager');
  }

  /**
   * Retrieve the correct renderer and template for $post
   *
   * @param Fishpig_Wordpress_Model_Post_Abstract $post
   * @return Fishpig_Wordpress_Block_Post_List_Renderer
   */
  public function getPostRenderer(Fishpig_Wordpress_Model_Post_Abstract $post)
  {
    if (!$this->hasPostRenderer()) {
      $this->setPostRenderer(
        $this->getLayout()->createBlock('wordpress/post_list_renderer')
          ->setParentBlock($this)
          ->setExcerptSize($this->getExcerptSize())
      );
    }

    return $this->_getData('post_renderer')
      ->setPost($post)
      ->setTemplate(
        $post->getPostListTemplate() ? $post->getPostListTemplate() : 'wordpress/post/list/renderer/default.phtml'
      );
  }

  /**
   * Ensure that the post list handle is set (adds the pager)
   *
   * @return $this
   */
  protected function _prepareLayout()
  {
    $this->getLayout()
      ->getUpdate()
      ->addHandle('wordpress_post_list');

    return parent::_prepareLayout();
  }

  /**
   * Ensure a valid template is set
   *
   * @return $this
   */
  protected function _beforeToHtml()
  {
    if (!$this->getTemplate()) {
      $this->setTemplate('wordpress/post/list.phtml');
    }

    return parent::_beforeToHtml();
  }
}
  • Please add relevant codes to your post. – Rajeev K Tomy Feb 12 '17 at 10:54
-1

Accessing Block class in controller/Model file is deprecated. You can write the code about to get the numbers posts in Hepler class and make use of it.

  • could you please show the source of deprecation ? – Rajeev K Tomy Feb 12 '17 at 10:57
  • in Mage.php, getBlockSingleton – seeni Feb 12 '17 at 11:02
  • I couldn't find @deprecated tag over there. I looked into 1.9.3.1. I dont think such deprecation does exist in M1 controllers. You can often see lot of codes in core controllers which actually invokes blocks and alter them. – Rajeev K Tomy Feb 12 '17 at 11:14
  • I have added from EE 1.14, in Mage.php line number 525, * Deprecated, use self::helper() * – seeni Feb 12 '17 at 11:33

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.