Magento – Magento 2 : Invalid block type while creating custom grid in backend

admincustom-gridmagento2module

I've been trying to create custom gird in magento 2.0.11 but it keep returning Invalid block type:

This error occurs while executing the file Tomrain/Appcustomizer/Block/Adminhtml/Clipartcategories.php in following code :

$this->setChild(
            'grid',
            $this->getLayout()->createBlock('Tomrain\Appcustomizer\Block\Adminhtml\Clipartcategories\Grid', 'appcustomizer.grid')
        );

But I have block Tomrain\Appcustomizer\Block\Adminhtml\Clipartcategories\Grid

Invalid block type: Tomrain\Appcustomizer\Block\Adminhtml\Clipartcategories\Grid
#0 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Layout/Generator/Block.php(237): Magento\Framework\View\Layout\Generator\Block->getBlockInstance('Tomrain\\Appcust...', Array)
#1 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Layout.php(760): Magento\Framework\View\Layout\Generator\Block->createBlock('Tomrain\\Appcust...', 'appcustomizer.g...', Array)
#2 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Layout.php(743): Magento\Framework\View\Layout->_createBlock('Tomrain\\Appcust...', 'appcustomizer.g...', Array)
#3 /var/www/html/appcustomizer-tshirt-M2/var/generation/Magento/Framework/View/Layout/Interceptor.php(349): Magento\Framework\View\Layout->createBlock('Tomrain\\Appcust...', 'appcustomizer.g...', Array)
#4 /var/www/html/appcustomizer-tshirt-M2/app/code/Tomrain/Appcustomizer/Block/Adminhtml/Clipartcategories.php(41): Magento\Framework\View\Layout\Interceptor->createBlock('Tomrain\\Appcust...', 'appcustomizer.g...')
#5 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Element/AbstractBlock.php(262): Tomrain\Appcustomizer\Block\Adminhtml\Clipartcategories->_prepareLayout()
#6 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Layout/Generator/Block.php(139): Magento\Framework\View\Element\AbstractBlock->setLayout(Object(Magento\Framework\View\Layout\Interceptor))
#7 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Layout/GeneratorPool.php(86): Magento\Framework\View\Layout\Generator\Block->process(Object(Magento\Framework\View\Layout\Reader\Context), Object(Magento\Framework\View\Layout\Generator\Context))
#8 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Layout.php(327): Magento\Framework\View\Layout\GeneratorPool->process(Object(Magento\Framework\View\Layout\Reader\Context), Object(Magento\Framework\View\Layout\Generator\Context))
#9 /var/www/html/appcustomizer-tshirt-M2/var/generation/Magento/Framework/View/Layout/Interceptor.php(89): Magento\Framework\View\Layout->generateElements()
#10 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Layout/Builder.php(129): Magento\Framework\View\Layout\Interceptor->generateElements()
#11 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Page/Builder.php(55): Magento\Framework\View\Layout\Builder->generateLayoutBlocks()
#12 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Layout/Builder.php(65): Magento\Framework\View\Page\Builder->generateLayoutBlocks()
#13 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Layout.php(244): Magento\Framework\View\Layout\Builder->build()
#14 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/View/Layout.php(859): Magento\Framework\View\Layout->build()
#15 /var/www/html/appcustomizer-tshirt-M2/var/generation/Magento/Framework/View/Layout/Interceptor.php(414): Magento\Framework\View\Layout->getBlock('menu')
#16 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/module-backend/Model/View/Result/Page.php(59): Magento\Framework\View\Layout\Interceptor->getBlock('menu')
#17 /var/www/html/appcustomizer-tshirt-M2/var/generation/Magento/Backend/Model/View/Result/Page/Interceptor.php(24): Magento\Backend\Model\View\Result\Page->setActiveMenu('Tomrain_Appcust...')
#18 /var/www/html/appcustomizer-tshirt-M2/app/code/Tomrain/Appcustomizer/Controller/Adminhtml/Clipartcategories/Index.php(36): Magento\Backend\Model\View\Result\Page\Interceptor->setActiveMenu('Tomrain_Appcust...')
#19 /var/www/html/appcustomizer-tshirt-M2/var/generation/Tomrain/Appcustomizer/Controller/Adminhtml/Clipartcategories/Index/Interceptor.php(25): Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index->execute()
#20 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/App/Action/Action.php(102): Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor->execute()
#21 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/module-backend/App/AbstractAction.php(226): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#22 [internal function]: Magento\Backend\App\AbstractAction->dispatch(Object(Magento\Framework\App\Request\Http))
#23 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Interceptor.php(74): call_user_func_array(Array, Array)
#24 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Chain/Chain.php(70): Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor->___callParent('dispatch', Array)
#25 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Tomrain\\Appcust...', 'dispatch', Object(Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor), Array, 'adminAuthentica...')
#26 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/module-backend/App/Action/Plugin/Authentication.php(143): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#27 [internal function]: Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch(Object(Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#28 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Chain/Chain.php(67): call_user_func_array(Array, Array)
#29 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Tomrain\\Appcust...', 'dispatch', Object(Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor), Array, 'designLoader')
#30 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/App/Action/Plugin/Design.php(39): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#31 [internal function]: Magento\Framework\App\Action\Plugin\Design->aroundDispatch(Object(Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#32 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Chain/Chain.php(67): call_user_func_array(Array, Array)
#33 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Tomrain\\Appcust...', 'dispatch', Object(Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor), Array, 'adminMassaction...')
#34 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/module-backend/App/Action/Plugin/MassactionKey.php(33): Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#35 [internal function]: Magento\Backend\App\Action\Plugin\MassactionKey->aroundDispatch(Object(Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#36 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Interceptor.php(142): call_user_func_array(Array, Array)
#37 /var/www/html/appcustomizer-tshirt-M2/var/generation/Tomrain/Appcustomizer/Controller/Adminhtml/Clipartcategories/Index/Interceptor.php(40): Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor->___callPlugins('dispatch', Array, Array)
#38 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/App/FrontController.php(55): Tomrain\Appcustomizer\Controller\Adminhtml\Clipartcategories\Index\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#39 [internal function]: Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#40 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Interceptor.php(74): call_user_func_array(Array, Array)
#41 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#42 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'install')
#43 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Module/Plugin/DbStatusValidator.php(69): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#44 [internal function]: Magento\Framework\Module\Plugin\DbStatusValidator->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#45 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/Interception/Interceptor.php(142): call_user_func_array(Array, Array)
#46 /var/www/html/appcustomizer-tshirt-M2/var/generation/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#47 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/App/Http.php(115): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#48 /var/www/html/appcustomizer-tshirt-M2/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#49 /var/www/html/appcustomizer-tshirt-M2/index.php(42): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))

Tomrain/Appcustomizer/Block/Adminhtml/Clipartcategories.php

namespace Tomrain\Appcustomizer\Block\Adminhtml;

class Clipartcategories extends \Magento\Backend\Block\Widget\Container
{
    /**
     * @var string
     */
    protected $_template = 'clipartcategories/entries.phtml';

    /**
     * @param \Magento\Backend\Block\Widget\Context $context
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    /**
     * Prepare button and grid
     *
     * @return \Magento\Catalog\Block\Adminhtml\Product
     */
    protected function _prepareLayout()
    {
        $addButtonProps = [
            'id' => 'add_new_entry_post',
            'label' => __('Add New'),
            'class' => 'add',
            'button_class' => '',
            'class_name' => 'Magento\Backend\Block\Widget\Button\SplitButton',
            'options' => $this->_getAddButtonOptions(),
        ];
        $this->buttonList->add('add_new', $addButtonProps);

        $this->setChild(
            'grid',
            $this->getLayout()->createBlock('Tomrain\Appcustomizer\Block\Adminhtml\Clipartcategories\Grid', 'appcustomizer.grid')
        );
        return parent::_prepareLayout();
    }

    /**
     *
     *
     * @return array
     */
    protected function _getAddButtonOptions()
    {

        $splitButtonOptions[] = [
            'label' => __('Add New'),
            'onclick' => "setLocation('" . $this->_getCreateUrl() . "')"
        ];

        return $splitButtonOptions;
    }

    /**
     *
     *
     * @param string $type
     * @return string
     */
    protected function _getCreateUrl()
    {
        return $this->getUrl(
            'appcustomizer/*/new'
        );
    }

    /**
     * Render grid 
     *
     * @return string
     */
    public function getGridHtml()
    {
        return $this->getChildHtml('grid');
    }
}

Tomrain/Appcustomizer/Block/Adminhtml/Clipartcategories/Grid.php

namespace Tomrain\Appcustomizer\Block\Adminhtml\Clipartcategories;

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

    /**
     * @var \Tomrain\Appcustomizer\Model\ClipartcategoriesFactory
     */
    protected $_clipartcategoriesFactory;

    protected $_status;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Tomrain\Appcustomizer\Model\ClipartcategoriesFactory $clipartcategoriesFactory
     * @param \Tomrain\Appcustomizer\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,
        \Tomrain\Appcustomizer\Model\ClipartcategoriesFactory $clipartcategoriesFactory,
        \Tomrain\Appcustomizer\Model\Status $status,
        \Magento\Framework\Module\Manager $moduleManager,
        array $data = []
    ) {
        $this->_clipartcategoriesFactory = $clipartcategoriesFactory;
        $this->_status = $status;
        $this->moduleManager = $moduleManager;
        parent::__construct($context, $backendHelper, $data);
    }

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

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

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

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

        $this->addColumn(
            'label',
            [
                'header' => __('Category'),
                'index' => 'label',
                'class' => 'xxx',
                'name'=>'label'
            ]
        );

        $this->addColumn(
            'parent_id',
            [
                'header' => __('Parent Category'),
                'index' => 'parent_id',
                'class' => 'xxx',
                'name'=>'parent_id',
                'options' => $this->_status->_getAttributeOptions()
            ]
        );

        $this->addColumn(
            'image',
            array(
                'header' => __('Image'),
                'index' => 'image',
                'renderer'  => '\Tomrain\Appcustomizer\Block\Adminhtml\Inquiry\Grid\Renderer\Clipartcategories',
            )
        );

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


        $this->addColumn(
            'edit',
            [
                'header' => __('Edit'),
                'type' => 'action',
                'getter' => 'getId',
                'actions' => [
                    [
                        'caption' => __('Edit'),
                        'url' => [
                            'base' => '*/*/edit'
                        ],
                        'field' => '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('id');
        $this->getMassactionBlock()->setTemplate('Tomrain_Appcustomizer::clipartcategories/grid/massaction_extended.phtml');
        $this->getMassactionBlock()->setFormFieldName('label');

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

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

        array_unshift($statuses, ['label' => '', 'value' => '']);
        $this->getMassactionBlock()->addItem(
            'status',
            [
                'label' => __('Change status'),
                'url' => $this->getUrl('appcustomizer/*/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('appcustomizer/*/grid', ['_current' => true]);
    }

    public function getRowUrl($row)
    {
        return $this->getUrl(
            'appcustomizer/*/edit',
            ['id' => $row->getId()]
        );
    }
}

Best Answer

$this->getLayout()->createBlock('\Tomrain\Appcustomizer\Block\Adminhtml\Clipartcategories\Grid', 'appcustomizer.grid');

please check with above code.

Related Topic