0

I created a module with a Custom Widget. When logging into the admin, going to content->widgets, creating an instance, and setting that instance to appear on the homepage, the widget appears as expected.

I have removed this instance and am now trying to create the widget by going to content->pages, editing the homepage -> content section, and selecting insert widget. I created a new instance of the widget and added some additional text into the homepage -> content section.

When I load the homepage the widget does not appear, though the rest of the text does appear.

The block for the widget is being called ( I tested this with a var_dump and exit).

The template is not being rendered however.

Here is my Block:

<?php

namespace MyNamespace\Slider\Block\Widget;

use Magento\Framework\View\Element\Template;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Block\Product\Context;

class Slider extends Template
{
    protected $_template = "Yamazaki_Slider::widget/slider.phtml";

    /**
     * @var CollectionFactory
     */
    protected $_productCollectionFactory;

    /**
     * @var CollectionFactory
     */
    protected $_imageHelper;

    /**
     * @param Context $context
     * @param CollectionFactory $productCollectionFactory
     */
    public function __construct(Context $context, CollectionFactory $productCollectionFactory)
    {
        $this->_imageHelper = $context->getImageHelper();
        $this->_productCollectionFactory = $productCollectionFactory;
        parent::__construct($context);
    }

    /**
     * Retrieve featured products collection
     */
    public function getProducts()
    {
        $collection = $this->_productCollectionFactory->create();
        return $collection->addAttributeToSelect('*')->addAttributeToFilter('is_featured','1');
    }
}

and my Template:

<?php $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct'); ?>

<?php foreach($this->getProducts() as $product): ?>
    <?php $productImage = $imageBlock->getImage($product, 'product_page_image_large'); ?>
    <?php echo $productImage->toHtml(); ?>
<?php endforeach ?>

Update

If I "Hide the Editor" inside content->pages->edit->content I see:

<p>{{widget type="MyNamespace\Slider\Block\Widget\Slider"}}</p>` 

If I replace this with

{{block class="MyNamespace\Slider\Block\Widget\Slider" template="widget/slider.phtml"}}

The Content appears as expected

2 Answers 2

4

I managed to get the Block to Render

I had to add implements \Magento\Widget\Block\BlockInterface to MyNamespace\Slider\Block\Widget\Slider.

I'm not sure why this rendered, prior to adding the implements, using the first approach and not the second.

Magento is seriously lacking documentation!

0

please check code for get image.

$imagewidth=200;
$imageheight=200;
$imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
$image_url = $imageHelper->init($product, 'product_page_image_small')->setImageFile($product->getFile())->resize($imagewidth, $imageheight)->getUrl();
   echo $image_url;

After do, please flush cache

9
  • is that work for you? Commented Nov 16, 2017 at 20:00
  • This is not the problem, the images are appearing just fine when the widget is added through content->widgets. Commented Nov 16, 2017 at 20:01
  • Forget about the images entirely. If I change the template to be <?php echo 'working?'; exit; ?> It will show working? in approach 1 and not approach 2 Commented Nov 16, 2017 at 20:01
  • enable hint path and check which phtml is called Commented Nov 16, 2017 at 20:03
  • Also, I noticed I must have removed my widget.xml file at somepoint. I had 2 parameters initially. I recreated the xml file without any parameters though the 2 initial parameters still appear when creating the widget. Not sure if this has anything to do with it Commented Nov 16, 2017 at 20:03

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.