Magento 2.1.1 Invalid block type exception - class not found

user3415805 picture user3415805 · Sep 25, 2016 · Viewed 11.9k times · Source

Trying to load a custom module's edit/create form.

In my Magento 2.1.1 on my production system (linux) I get a invalid block type exception caused by class not found while on my local host xampp / windows all works fine. It occurs when I want to open the form to create or edit a custom entity while the grid works fine.

2 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: Brainworx\Medipimsync\Block\Adminhtml\Sync\Edit\Form
Exception #1 (ReflectionException): Class Brainworx\Medipimsync\Block\Adminhtml\Sync\Edit\Form does not exist

Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: Brainworx\Medipimsync\Block\Adminhtml\Sync\Edit\Form
#0 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Layout/Generator/Block.php(237): Magento\Framework\View\Layout\Generator\Block->getBlockInstance('Brainworx\\Medip...', Array)
#1 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Layout.php(760): Magento\Framework\View\Layout\Generator\Block->createBlock('Brainworx\\Medip...', 'medipimsync_syn...', Array)
#2 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Layout.php(743): Magento\Framework\View\Layout->_createBlock('Brainworx\\Medip...', 'medipimsync_syn...', Array)
#3 /data/sites/web/apovitabe/www/var/generation/Magento/Framework/View/Layout/Interceptor.php(349): Magento\Framework\View\Layout->createBlock('Brainworx\\Medip...', 'medipimsync_syn...', Array)
#4 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Element/AbstractBlock.php(382): Magento\Framework\View\Layout\Interceptor->createBlock('Brainworx\\Medip...', 'medipimsync_syn...', Array)
#5 /data/sites/web/apovitabe/www/vendor/magento/module-backend/Block/Widget/Form/Container.php(108): Magento\Framework\View\Element\AbstractBlock->addChild('form', 'Brainworx\\Medip...')
#6 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Element/AbstractBlock.php(264): Magento\Backend\Block\Widget\Form\Container->_prepareLayout()
#7 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Layout/Generator/Block.php(139): Magento\Framework\View\Element\AbstractBlock->setLayout(Object(Magento\Framework\View\Layout\Interceptor))
#8 /data/sites/web/apovitabe/www/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))
#9 /data/sites/web/apovitabe/www/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))
#10 /data/sites/web/apovitabe/www/var/generation/Magento/Framework/View/Layout/Interceptor.php(89): Magento\Framework\View\Layout->generateElements()
#11 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Layout/Builder.php(129): Magento\Framework\View\Layout\Interceptor->generateElements()
#12 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Page/Builder.php(55): Magento\Framework\View\Layout\Builder->generateLayoutBlocks()
#13 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Layout/Builder.php(65): Magento\Framework\View\Page\Builder->generateLayoutBlocks()
#14 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Layout.php(244): Magento\Framework\View\Layout\Builder->build()
#15 /data/sites/web/apovitabe/www/vendor/magento/framework/View/Layout.php(859): Magento\Framework\View\Layout->build()
#16 /data/sites/web/apovitabe/www/var/generation/Magento/Framework/View/Layout/Interceptor.php(414): Magento\Framework\View\Layout->getBlock('menu')
#17 /data/sites/web/apovitabe/www/vendor/magento/module-backend/Model/View/Result/Page.php(59): Magento\Framework\View\Layout\Interceptor->getBlock('menu')
#18 /data/sites/web/apovitabe/www/var/generation/Magento/Backend/Model/View/Result/Page/Interceptor.php(24): Magento\Backend\Model\View\Result\Page->setActiveMenu('Brainworx_Medip...')
#19 /data/sites/web/apovitabe/www/app/code/brainworx/medipimsync/Controller/Adminhtml/Sync/Edit.php(52): Magento\Backend\Model\View\Result\Page\Interceptor->setActiveMenu('Brainworx_Medip...')
#20 /data/sites/web/apovitabe/www/app/code/brainworx/medipimsync/Controller/Adminhtml/Sync/Edit.php(87): brainworx\medipimsync\Controller\Adminhtml\Sync\Edit->_initAction()
#21 /data/sites/web/apovitabe/www/var/generation/brainworx/medipimsync/Controller/Adminhtml/Sync/Edit/Interceptor.php(24): brainworx\medipimsync\Controller\Adminhtml\Sync\Edit->execute()
#22 /data/sites/web/apovitabe/www/vendor/magento/framework/App/Action/Action.php(102): brainworx\medipimsync\Controller\Adminhtml\Sync\Edit\Interceptor->execute()
#23 /data/sites/web/apovitabe/www/vendor/magento/module-backend/App/AbstractAction.php(226): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#24 /data/sites/web/apovitabe/www/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Backend\App\AbstractAction->dispatch(Object(Magento\Framework\App\Request\Http))
#25 /data/sites/web/apovitabe/www/vendor/magento/framework/Interception/Chain/Chain.php(70): brainworx\medipimsync\Controller\Adminhtml\Sync\Edit\Interceptor->___callParent('dispatch', Array)
#26 /data/sites/web/apovitabe/www/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('brainworx\\medip...', 'dispatch', Object(brainworx\medipimsync\Controller\Adminhtml\Sync\Edit\Interceptor), Array, 'adminAuthentica...')

Setup:

registion.php

\Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'brainworx_medipimsync',
        __DIR__
);

etc/Module.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="brainworx_medipimsync" setup_version="1.0.4" />
    </config>

Form class exists: app\code\brainworx\medipimsync\Block\Adminhtml\Sync\Edit\Form.php

namespace brainworx\medipimsync\Block\Adminhtml\Sync\Edit;

use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;
use Magento\Framework\Data\FormFactory;
use Magento\Store\Model\System\Store;
use Magento\Backend\Model\Auth\Session;

/**
 * Adminhtml sync edit form
 */
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{

    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;
    protected $_loginUser;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
     * @param \Magento\Store\Model\System\Store $systemStore
     * @param array $data
     * @param \Magento\backend\Model\Auth\Session $authSession
     */
    public function __construct(
            Context $context,Registry $registry,FormFactory $formFactory,Store $systemStore,
            array $data = [],Session $authSession
    ) {
        $this->_systemStore = $systemStore;
        $this->_loginUser = $authSession->getUser()->getUsername();
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Init form
     *
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('sync_form');
        $this->setTitle(__('Sync Information'));
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {       
        /** @var \brainworx\medipimsync\Model\Sync $model */
        $model = $this->_coreRegistry->registry('medipimsync_sync');

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create(
                ['data' => ['id' => 'edit_form','action' => $this->getData('action'), 'method' => 'post']]
        );

        $form->setHtmlIdPrefix('sync_');

        $fieldset = $form->addFieldset(
                'base_fieldset',
                ['legend' => __('General Information'), 'class' => 'fieldset-wide']
        );

        $fieldset->addField(
                'title',
                'text',
                ['name' => 'user', 'label' => __('User'), 'title' => __('User'), 'required' => true,
                        'value' => $this->_loginUser , 'readonly' => true
                ]
        );

        $fieldset->addField(
                'entity',
                'select',
                [
                        'label' => __('Entity'),
                        'title' => __('Entity'),
                        'name' => 'entity',
                        'required' => true,
                        'options' => ['PROD' => __('Products'), 'CAT' => __('Categories')]
                ]
        );

        //$form->setValues($model->getData());
        $form->setUseContainer(true);
        $this->setForm($form);

        return parent::_prepareForm();
    }
}

edit class Also the edit class containing the form app\code\brainworx\medipimsync\Block\Adminhtml\Sync\Edit.php

namespace brainworx\medipimsync\Block\Adminhtml\Sync;

class Edit extends \Magento\Backend\Block\Widget\Form\Container
{
    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;

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

    /**
     * Initialize blog post edit block
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_objectId = 'sync_id';
        $this->_blockGroup = 'brainworx_medipimsync';
        $this->_controller = 'adminhtml_sync';

        parent::_construct();

        if ($this->_isAllowedAction('Brainworx_Medipimsync::save')) {
            $this->buttonList->update('save', 'label', __('Creatae new'));
            $this->buttonList->add(
                    'saveandcontinue',
                    [
                            'label' => __('Save and Continue Edit'),
                            'class' => 'save',
                            'data_attribute' => [
                                    'mage-init' => [
                                            'button' => ['event' => 'saveAndContinueEdit', 'target' => '#edit_form'],
                                    ],
                            ]
                    ],
                    -100
            );
        } else {
            $this->buttonList->remove('save');
        }
    }

    /**
     * Retrieve text for header element depending on loaded post
     *
     * @return \Magento\Framework\Phrase
     */
    public function getHeaderText()
    {
        if ($this->_coreRegistry->registry('medipim_sync')->getId()) {
            return __("Edit Sync '%1'", $this->escapeHtml($this->_coreRegistry->registry('medipimsync_sync')->getTitle()));
        } else {
            return __('New Sync');
        }
    }

    /**
     * Check permission for passed action
     *
     * @param string $resourceId
     * @return bool
     */
    protected function _isAllowedAction($resourceId)
    {
        return $this->_authorization->isAllowed($resourceId);
    }

    /**
     * Getter of url for "Save and Continue" button
     * tab_id will be replaced by desired by JS later
     *
     * @return string
     */
    protected function _getSaveAndContinueUrl()
    {
        return $this->getUrl('medipimsync/*/save', ['_current' => true, 'back' => 'edit', 'active_tab' => '']);
    }
}

Layout file And the layout file loading the edit class: view/adminhtml/layout/medipimsync_sync_edit.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="editor"/>
    <body>
        <referenceContainer name="content">
            <block class="brainworx\medipimsync\Block\Adminhtml\Sync\Edit" name="medipimsync_sync_edit"/>
        </referenceContainer>
    </body>
</page>

Thanks for the help!

Answer

user3415805 picture user3415805 · Sep 25, 2016

Already found the solution after a night and day of searching.

For theses who like to know: You should make the directories and module name in capital case. Changing all to Brainworx\Medipimsync did the trick! So both the directories under app/code as the module name and namespaces.