0

I don't understand what is wrong with this Renderer code:

<?php
namespace Darsh\Banner\Block\Adminhtml\Grid\Renderer;
use Magento\Framework\DataObject as DObject;
use Magento\Framework\Data\Form\Element\Renderer\RendererInterface as RI;
use Magento\Framework\Data\Form\Element\AbstractElement as AE;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer as AR;

class ImageView extends AE implements RI
{
        ## public function render(\Magento\Framework\Object $row)
        public function render(DObject $row)
        {
                echo "<pre/>";print_r($row);die;
        }
}

Whenever I compile my code with this in "Darsh_Banner" module, it generates fatal error in "Repositories Code Generation:" step as below:

PHP Fatal error:  Declaration of Darsh\Banner\Block\Adminhtml\Grid\Renderer\ImageView::render() must be compatible with Magento\Framework\Data\Form\Element\Renderer\RendererInterface::render(Magento\Framework\Data\Form\Element\AbstractElement $element) in /var/www/html/magento-2/app/code/Darsh/Banner/Block/Adminhtml/Banner/Grid/Renderer/ImageView.php on line 9

Grid.php of Darsh-Banner module:

<?php
namespace Darsh\Banner\Block\Adminhtml\Banner;

class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
    /**
     * @var \Magento\Framework\Module\Manager
     */
    protected $moduleManager;

    /**
     * @var \Darsh\Banner\Model\bannerFactory
     */
    protected $_bannerFactory;

    /**
     * @var \Darsh\Banner\Model\Status
     */
    protected $_status;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Darsh\Banner\Model\bannerFactory $bannerFactory
     * @param \Darsh\Banner\Model\Status $status
     * @param \Magento\Framework\Module\Manager $moduleManager
     * @param array $data
     *
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Darsh\Banner\Model\BannerFactory $bannerFactory,
        \Darsh\Banner\Model\Status $status,
        \Magento\Framework\Module\Manager $moduleManager,
        array $data = []
    ) {
        $this->_bannerFactory = $bannerFactory;
        $this->_status = $status;
        $this->moduleManager = $moduleManager;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('postGrid');
        $this->setDefaultSort('banner_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
        $this->setVarNameFilter('post_filter');
    }

    /**
     * @return $this
     */
    protected function _prepareCollection()
    {
        $collection = $this->_bannerFactory->create()->getCollection();
        $this->setCollection($collection);

        parent::_prepareCollection();
        return $this;
    }

    /**
     * @return $this
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    protected function _prepareColumns()
    {
        $this->addColumn(
            'banner_id',
            [
                'header' => __('ID'),
                'type' => 'number',
                'index' => 'banner_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );
        $this->addColumn(
            'title',
            [
                'header' => __('Title'),
                'index' => 'title',
                'class' => 'xxx'
            ]
        );

        $this->addColumn(
            'image',
            [
                'header' => __('Image'),
                'index' => 'image',
                'class' => 'xxx',
                ## 'renderer' => 'Darsh\Banner\Block\Adminhtml\Banner\Grid\Renderer\ImageView'
            ]
        );


        $this->addColumn(
            'is_active',
            [
                'header' => __('Status'),
                'index' => 'is_active',
                'type' => 'options',
                'options' => $this->_status->getOptionArray()
            ]
        );


        $this->addColumn(
            'edit',
            [
                'header' => __('Edit'),
                'type' => 'action',
                'getter' => 'getId',
                'actions' => [
                    [
                        'caption' => __('Edit'),
                        'url' => [
                            'base' => '*/*/edit'
                        ],
                        'field' => 'banner_id'
                    ]
                ],
                'filter' => false,
                'sortable' => false,
                'index' => 'stores',
                'header_css_class' => 'col-action',
                'column_css_class' => 'col-action'
            ]
        );

        $block = $this->getLayout()->getBlock('grid.bottom.links');
        if ($block) {
            $this->setChild('grid.bottom.links', $block);
        }

        return parent::_prepareColumns();
    }

    /**
     * @return $this
     */
    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('banner_id');
        $this->getMassactionBlock()->setTemplate('Darsh_Banner::banner/grid/massaction_extended.phtml');
        $this->getMassactionBlock()->setFormFieldName('banner');

        $this->getMassactionBlock()->addItem(
            'delete',
            [
                'label' => __('Delete'),
                'url' => $this->getUrl('banner/*/massDelete'),
                'confirm' => __('Are you sure?')
            ]
        );

        $statuses = $this->_status->getOptionArray();

        $this->getMassactionBlock()->addItem(
            'status',
            [
                'label' => __('Change status'),
                'url' => $this->getUrl('banner/*/massStatus', ['_current' => true]),
                'additional' => [
                    'visibility' => [
                        'name' => 'status',
                        'type' => 'select',
                        'class' => 'required-entry',
                        'label' => __('Status'),
                        'values' => $statuses
                    ]
                ]
            ]
        );


        return $this;
    }

    /**
     * @return string
     */
    public function getGridUrl()
    {
        return $this->getUrl('banner/*/grid', ['_current' => true]);
    }

    /**
     * @param \Darsh\Banner\Model\banner|\Magento\Framework\Object $row
     * @return string
     */
    public function getRowUrl($row)
    {
        return $this->getUrl(
            'banner/*/edit',
            ['banner_id' => $row->getId()]
        );
    }
}

I tried changing "implementing from" classes but same error.

Anyone please provide suggestion.

3 Answers 3

2

Re: the specific error you're seeing -- you have a class named

Darsh\Banner\Block\Adminhtml\Grid\Renderer\ImageView

Which defined a render method that looks like this (short class names expanded)

public function render(\Magento\Framework\DataObject $row)
{
    echo "<pre/>";print_r($row);die;
}

Your class implements the Magento\Framework\Data\Form\Element\Renderer\RendererInterface interface, which has an abstract render method

public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element);

Your render method accepts a single Magento\Framework\DataObject parameter.

The interface accepts a Magento\Framework\Data\Form\Element\AbstractElement parameter.

This makes your class incompatible with the interface. To fix this specific error, you need to change the type hint on your class to

public function render(\Magento\Framework\Data\Form\Element\AbstractElement $row)
{
    echo "<pre/>";print_r($row);die;
}
0

Try this: In Grid.php

$this->addColumn(
            'column_id',
            [
                'header'   => __('Column Name'),
                'index'    => 'column_id',
                'type'     => 'column type', //file, number,etc.
                'renderer' => 'PackageName\Modulename\Block\Adminhtml\Modulename\Grid\Renderer\RendererFileName',
            ]
        );

In RendererFileName.php

class ImageView extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer {
    public function render(DataObject $row) {
         echo "Hello world"; exit;  //Your code here
    }
}
7
  • Changed object type of $row as you mentioned but then it doesn't load in admingrid properly doesn't load any value in it's grid page, fatal error is gone though.
    – Vicky Dev
    Commented Dec 9, 2015 at 11:34
  • If I put the echo "Hello" then again page goes blank completely. I am not sure if the error is completely gone or not sometimes fatal error does show up. I think it's related to what class I am "implementing" and what class I am "extending" from, but it's not clear.
    – Vicky Dev
    Commented Dec 9, 2015 at 11:48
  • Coincidence!!! Me too checked the core "renderer" class and just did the same thing as in above answer but now the whole admin page is blank for banner and nothing in exception.log.
    – Vicky Dev
    Commented Dec 9, 2015 at 12:17
  • this works for me. there must be issue in some other related file. for your reference, i have also included my grid.php from where renderer is called. Commented Dec 9, 2015 at 12:26
  • Is type specification necessary in the "Grid.php" file ? Even if I specify type to "file" or "image" it gives blank page in Banner Admin, Grid is not loaded at all, the rest of header and footer is loaded fine. Other than that my "Grid.php" and "ImageView.php(Renderer)" is ditto same as yours.
    – Vicky Dev
    Commented Dec 9, 2015 at 12:51
0

You should use the following render function in your renderer file to fix this error.

public function render(\Magento\Framework\DataObject $row) {

}

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.