0

I'm attempting to output the wishlist block as an ajax request. I've attempted to do it in the controller using

$block = $resultPage->getLayout()
->createBlock('Magento\Wishlist\Block\Customer\Wishlist','miniwishlist')            
->setTemplate('Company_Module::miniwishlist.phtml')
->toHtml();

But as the block contains several child blocks and I didn't have any success in using the setChild method programmatically.

I've since created a layout file that returns the block by itself. The problem is that output contains html, head, body tags, css and javascript.

I want to return only the block and nothing else. How can I achieve that?

5
  • I suspect you will need to create a new page layout, or use empty and remove after.body.start and page.wrapper.
    – Ben Crook
    Oct 11, 2016 at 12:12
  • I don't think it's that I used the template hints and only have two other blocks - require js and container which are above the wishlist block. In the html source code the block I want sits between the body tag. I want to get rid of everything else - html, body, head tags and all the css/javascript inside. I could parse the output through domdocument - but that seems hacky.
    – Oliver
    Oct 11, 2016 at 12:46
  • 1
    Hmm <html> and <body> are added via root.phtml. Setting that page to use a new empty template (only including the code you require) instead of root.phtml may work? You can see this template being set in vendor/magento/magento2-base/app/etc/di.xml line 563 (for me at least). The root template can be seen in vendor/magento/module-theme/view/base/templates/root.phtml. I don't have the time to actually try it and provide a full answer sorry, if no one answer by the time I do I will give it a try and reply with a full answer.
    – Ben Crook
    Oct 11, 2016 at 13:36
  • In the block customer/Wishlist.php in the function _toHtml() if I replace "return parent::_toHtml()" with "die(parent::_toHtml());" I get the content I need using an unmodified layout in wishlist_index_index.xml. I may investigate going down that route.
    – Oliver
    Oct 11, 2016 at 13:49
  • 1
    You can read my answer here: magento.stackexchange.com/questions/121731/…
    – hiperboreo
    Nov 17, 2016 at 2:29

1 Answer 1

0

I went about it differently, but got the result I was after, with very little modification.

I access the contents of the wishlist block using a modified url http://domain.com/wishlist/index/index/ajax/1

I've appended the 'ajax/1' to the normal wishlist request

In my module I overwrote the block \Magento\Wishlist\Block\Customer\Wishlist and modified the function _toHtml as follows:

protected function _toHtml()
{
    if ($this->currentCustomer->getCustomerId()) {
        if ($this->getRequest()->getParam('ajax')) {
            die(parent::_toHtml());
        }
        else return parent::_toHtml();
    } else {
        return '';
    }
}

I also had to add the template file View.phtml to my module, which works nicely as I am able to modify that to get the desired output for my ajax call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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