0

Please help solve this puzzle.
I'm created after plugin, after load category widget. The collection of items must be used in the js. The idea is to simply intercept the collection in the plugin, load it to the front in the .phtml template and process it in it. My plugin code
ParceWidget.php

namespace Vendor\Module\Plugin;

use Magento\Framework\View\Element\Template;

class ParceWidget
{
    protected $blockFactory;

    public function __construct(
        \Magento\Framework\View\Element\BlockFactory $blockFactory
    ) {
        $this->blockFactory = $blockFactory;
    }

    public function afterCreateCollection(
        \Magento\CatalogWidget\Block\Product\ProductsList $subject,
        $itemsCollection
    ) {
        $this->parceWidgetInformation($itemsCollection);

        return $itemsCollection;
    }
    
    protected function parceWidgetInformation($itemsCollection)
    {
        $block = $this->blockFactory->createBlock(
            Template::class,
            [
                'data' => [
                    'widget_items' => $itemsCollection
                ]
            ]
        )
            ->setTemplate('Vendor_Module::widget.phtml');

        $block->setNameInLayout('vendor.module.widget');
        $block->toHtml();
    }
}

widget.phtml

<div>
    <?php if ($items = $this->getData('widget_items')):?>
        <?php //var_dump($items); die();?>
        <?php foreach ($items as $item): ?>
            <ul>
                <li><?= $item->getData()?></li>
            </ul>
        <?php endforeach;?>
    <?php endif;?>
</div>

I have omitted some of the standard points, leaving the gist.
So, if you write any content in widget.phtml, then at the end of loading the page on the front will not be displayed. But if you stop the download page process by doing for example:
<?php var_dump($items')); die();?>
In this case, everything will be in place. Here I have a question, where does the data disappear in the process of loading the page, because you can see that the template is loaded and the data can be obtained in it?

  • please paste widget.phtml – Bilal Usean Nov 23 '20 at 20:44
  • @BilalUsean - thanks for viewed, updated post – Dmitry Zar Nov 23 '20 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.