1

I am trying to use sections.xml to return some data(customerData) upon user login. What i want to do is to return customer navigation links in customerData. And i thought that i can achieve this with sections.xml. But i face the problem when trying to load the customer_account_navigation block that actually has those links.

Ok, so i made a module AccountNavigationLinks which has CustomerData directory with .php file. This file has getSectionData method.

public function __construct(
    Session $session,
    \Magento\Framework\View\LayoutFactory $layoutFactory,
    \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory

)
{
    $this->session =$session;
    $this->resultLayoutFactory = $resultLayoutFactory;
    $this->layoutFactory = $layoutFactory;
}

public function getSectionData()
{
    $layout = $this->layoutFactory->create();
    $layout->getUpdate()->addHandle('customer_account')->load(['default']); // i think i need to add this handle. but still unsure of load which handle should i pass into load method
    $layout->generateXml();
    $layout->generateElements();
    $data = $layout->getBlock('customer_account_navigation')->toHtml();
    return ['links'=> 'dome data']; // here i want to return an array of links or whatever i can get from account_navigation_links

}

So, i want to have that customer_account_navigation block in my response and then i will, hopefully, get those links. But i can't get that block in my $layout. How can i get those links returned anyway?

Hope to find some help.

Thanks.

  • Have you managed to do this? I try to do the same thing – Vlad Patru Feb 8 '18 at 15:32
  • @VladPatru i posted answer to my question. – Asadov Kamran Feb 13 '18 at 11:01
1

I was using the wrong classes in my constructor. Instead of using \Magento\Framework\View\LayoutFactory, \Magento\Framework\View\Result\LayoutFactory in constructor,

use \Magento\Framework\View\Result\PageFactory.

Then add handles you need and fetch the block:

$resultPage = $this->_resultPageFactory->create();
$resultPage->addHandle(['default', 'customer_account_index']);
$block = $resultPage->getLayout()->getBlock('customer_account_navigation');
$data = $block->getBlockHtml('customer_account_navigation');
  • Good work man, thanks. Works like a charm – Vlad Patru Feb 15 '18 at 10:41

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.