1

I am getting following error:

Fatal error: Call to a member function getCsvFile() on boolean.

I have tried to implement a export CSV in a custom module but it is showing an error.

suyati_scontact_contacts_grid_block.xml

 <?xml version="1.0" encoding="UTF-8"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="admin.block.scontact.contacts.grid.container">
            <block class="Magento\Backend\Block\Widget\Grid" name="admin.block.scontact.contacts.grid" as="grid">

                <arguments>
                    <argument name="id" xsi:type="string">contactsGrid</argument>
                    <argument name="dataSource" xsi:type="object">Suyati\Scontact\Model\Resource\Contacts\Collection</argument>
                    <argument name="default_sort" xsi:type="string">contacts_id</argument>
                    <argument name="default_dir" xsi:type="string">desc</argument>
                    <argument name="save_parameters_in_session" xsi:type="boolean">true</argument>
                    <argument name="use_ajax" xsi:type="boolean">true</argument>
                </arguments>
                <block class="Magento\Backend\Block\Widget\Grid\Export" name="admin.block.scontact.contacts.grid.export" as="grid.export">
                    <arguments>
                        <argument name="exportTypes" xsi:type="array">
                            <item name="csv" xsi:type="array">
                                <item name="urlPath" xsi:type="string">*/*/exportCsv</item>
                                <item name="label" xsi:type="string" translate="true">CSV</item>
                            </item>
                            <item name="excel" xsi:type="array">
                                <item name="urlPath" xsi:type="string">*/*/exportXml</item>
                                <item name="label" xsi:type="string" translate="true">Excel XML</item>
                            </item>
                        </argument>
                    </arguments>
                </block>
                <block class="Magento\Backend\Block\Widget\Grid\ColumnSet" name="admin.block.scontact.contacts.grid.columnSet" as="grid.columnSet">

                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="contacts_id">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">ID</argument>
                            <argument name="type" xsi:type="string">number</argument>
                            <argument name="id" xsi:type="string">contacts_id</argument>
                            <argument name="index" xsi:type="string">contacts_id</argument>
                        </arguments>
                    </block>
                   <block class="Magento\Backend\Block\Widget\Grid\Column" as="name">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Name</argument>
                            <argument name="type" xsi:type="string">string</argument>
                            <argument name="id" xsi:type="string">name</argument>
                            <argument name="index" xsi:type="string">name</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="email">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Email</argument>
                            <argument name="type" xsi:type="string">string</argument>
                            <argument name="id" xsi:type="string">email</argument>
                            <argument name="index" xsi:type="string">email</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="mobile">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Mobile</argument>
                            <argument name="type" xsi:type="string">string</argument>
                            <argument name="id" xsi:type="string">mobile</argument>
                            <argument name="index" xsi:type="string">mobile</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="location">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Location</argument>
                            <argument name="type" xsi:type="string">string</argument>
                            <argument name="id" xsi:type="string">location</argument>
                            <argument name="index" xsi:type="string">location</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="pincode">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Pincode</argument>
                            <argument name="type" xsi:type="string">string</argument>
                            <argument name="id" xsi:type="string">pincode</argument>
                            <argument name="index" xsi:type="string">pincode</argument>
                        </arguments>
                    </block>
                     <block class="Magento\Backend\Block\Widget\Grid\Column" as="comment">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Comment</argument>
                            <argument name="type" xsi:type="string">string</argument>
                            <argument name="id" xsi:type="string">comment</argument>
                            <argument name="index" xsi:type="string">comment</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="created_date">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Created Date</argument>
                            <argument name="type" xsi:type="string">string</argument>
                            <argument name="id" xsi:type="string">created_date</argument>
                            <argument name="index" xsi:type="string">created_date</argument>
                        </arguments>
                    </block>
                </block>
            </block>
        </referenceBlock>
    </body>
</page>

Suyati\Scontact\Controller\Adminhtml\Contacts

<?php
    namespace Suyati\Scontact\Controller\Adminhtml\Contacts;

    use Magento\Framework\App\ResponseInterface;
    use Magento\Framework\App\Filesystem\DirectoryList;

    class ExportCsv extends \Suyati\Scontact\Controller\Adminhtml\Contacts
    {
        /**
         * Export contacts grid to CSV format
         *
         * @return ResponseInterface
         */
        public function execute()
        {
            $this->_view->loadLayout();
            $fileName = 'contacts.csv';
            $content = $this->_view->getLayout()->getChildBlock('admin.block.scontact.contacts.grid', 'grid.export');

            return $this->_fileFactory->create(
                $fileName,
                $content->getCsvFile($fileName),
                DirectoryList::VAR_DIR
            );
        }
    }

Controller/Adminhtml/Contacts.php

<?php

namespace Suyati\Scontact\Controller\Adminhtml;

abstract class Contacts extends \Magento\Backend\App\Action
{
    protected $resultPageFactory;
    protected $resultForwardFactory;
    protected $resultRedirectFactory;
    /**
     * @var \Magento\Framework\App\Response\Http\FileFactory
     */
    protected $_fileFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory
    )
    {
        $this->resultPageFactory = $resultPageFactory;
        $this->_fileFactory = $fileFactory;
        $this->resultForwardFactory = $resultForwardFactory;
        $this->resultRedirectFactory = $context->getResultRedirectFactory();
        parent::__construct($context);
    }

    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Suyati_Scontact::contact_manage');
    }

    protected function _initAction()
    {
        $this->_view->loadLayout();
        $this->_setActiveMenu(
            'Suyati_Scontact::contact_manage'
        )->_addBreadcrumb(
            __('Scontact'),
            __('Contacts')
        );
        return $this;
    }
}
2

This error occurs when it fails to get the layout file. It seems like you have missed the layout xml file where you should specify the grid that has to be exported. In your case you should have

suyati_scontact_contacts_exportcsv.xml

with the below contents within the layout:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="suyati_scontact_contacts_grid_block"/>
    <body>
        <block class="Magento\Backend\Block\Widget\Grid\Container" name="admin.block.scontact.contacts.grid.container" template="Magento_Backend::widget/grid/container/empty.phtml"/>
    </body>
</page>
2

Following line in your code:

$content = $this->_view->getLayout()->getChildBlock('admin.block.scontact.contacts.grid', 'grid.export');

must be your grid block (Which extended \Magento\Backend\Block\Widget\Grid\Extended) like

$content = $this->_view->getLayout()->createBlock('VENDOR\NAMESPACE\Block\Adminhtml\Product\View\Grid');

When you look at

\Magento\Backend\Block\Widget\Grid\Extended

there is function getCsvFile()

Hope you get the some clue from this

  • i have tried this but it seems to be my grid has some issue – Naveenbos Jul 28 '16 at 8:12
  • Ok, try to solve your grid issue, After that try this, It will work for you. @Naveen – Keyur Shah Jul 28 '16 at 8:48
  • $content = $this->_view->getLayout()->createBlock('VENDOR\NAMESPACE\Block\Adminhtml\Product\View\Grid');in which file i want to add this code – Naveenbos Jul 28 '16 at 8:59
  • Just replace your line with my line and make sure about your gid class @Naveen – Keyur Shah Jul 28 '16 at 9:22
  • No luck means ? Any error ? Post your full code here, so someone can helps you on this @Naveen – Keyur Shah Jul 28 '16 at 9:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.