3

I want to show the button in my custom module (admin page)

but I am getting an error

I want to add a button and how I add action process for that button

i already tried this answer : (https://magento.stackexchange.com/a/212656/73525 )

GenericButton.php

    <?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Webkul\Mpquotesystem\Block\Adminhtml\Managequotes\Edit;

use Magento\Backend\Block\Widget\Context;
//use FME\News\Api\NewsRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;

/**
 * Class GenericButton
 */
class GenericButton
{
    /**
     * @var Context
     */
    protected $context;

    /**
     * @var BlockRepositoryInterface
     */
    protected $blockRepository;

    /**
     * @param Context $context
     * @param BlockRepositoryInterface $blockRepository
     */
    public function __construct(
        Context $context
    ) {
        $this->context = $context;
        //$this->blockRepository = $blockRepository;
    }

    /**
     * Return CMS block ID
     *
     * @return int|null
     */
    public function getBlockId()
    {
/*
        {
            return $this->blockRepository->getById(
                $this->context->getRequest()->getParam('news_id')
            )->getId();
        }  */
        return null;
    }

    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = [])
    {
        return $this->context->getUrlBuilder()->getUrl($route, $params);
    }
}

SaveandContinuebutton.php

<?php

namespace  Webkul\Mpquotesystem\Block\Adminhtml\Managequotes\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
 * Class SaveAndContinueButton
 */
class SaveAndContinueButton extends GenericButton implements ButtonProviderInterface
{

    public function getButtonData()
    {
        return [
            'label' => __('ADmin pproval'),
            'class' => 'save',
            'on_click' => 'location.reload();',
            'sort_order' => 30
        ];
    }
}

view/adminhtml/uicomponent/custommodule_edit.xml

<?xml version="1.0"?>
<!-- /**
 * Webkul Software
 *
 * @category  Webkul
 * @package   Webkul_Mpquotesystem
 * @author    Webkul
 * @copyright Copyright (c) 2010-2018 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */ -->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">

    <item name="buttons" xsi:type="array">
        <item name="save_and_continue" xsi:type="string">Webkul\Mpquotesystem\Block\Adminhtml\Managequotes\Edit\SaveAndContinueButton</item>
    </item>

    </argument>
</form>

when clicking that admin custom module page :

Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception, caught Error: Call to a member function getRequestFieldName() on null in /var/www/html/equpo2/vendor/magento/module-ui/Component/Wrapper/UiComponent.php on line

2 Answers 2

0

Just change your SaveAndContinueButton file: and chek after clearing generated folder

       namespace Webkul\Mpquotesystem\Block\Adminhtml\Managequotes\Edit;

        use Magento\Customer\Api\AccountManagementInterface;
        use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

        class SaveAndContinueButton  implements ButtonProviderInterface
        {

            public function getButtonData()
            {
                return [
                    'label' => __('Save and Continue Edit'),
                    'class' => 'save',
                    'data_attribute' => [
                        'mage-init' => ['button' => ['event' => 'saveAndContinueEdit']],
                        'form-role' => 'save',
                    ],
                    'sort_order' => 80,
                ];
            }

        }
5
  • Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception, caught Error: Call to a member function getRequestFieldName() on null in /var/www/html/equpo2/vendor/magento/module-ui/Component/Wrapper/UiComponent.php on line 0 i am getting error Mar 14, 2019 at 5:51
  • @prabhakaran7 Don't you have add dataSource code in view/adminhtml/uicomponent/custommodule_edit.xml ? if yes please add in question Mar 14, 2019 at 5:54
  • yes no datasource what i do Mar 14, 2019 at 5:57
  • please follw and check where you did mistake pierrefay.com/magento2-training/… Mar 14, 2019 at 5:59
  • this page only one form Mar 14, 2019 at 6:00
0

Try this and modify the code in the below files

SaveandContinuebutton.php

use Magento\Backend\Block\Widget\Container;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class SaveAndContinueButton extends Container implements ButtonProviderInterface
{

    public function getButtonData()
    {
        return [
            'label' => __('ADmin pproval'),
            'class' => 'save',
            'on_click' => 'location.reload();',
            'sort_order' => 30
        ];
    }
}

view/adminhtml/uicomponent/custommodule_edit.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
    <item name="js_config" xsi:type="array">
        <item name="provider" xsi:type="string">custommodule_edit.custommodule_edit_data_source</item>
        <item name="deps" xsi:type="string">custommodule_edit.custommodule_edit_data_source</item>
    </item>
    <item name="template" xsi:type="string">templates/form/collapsible</item>
    <item name="buttons" xsi:type="array">
        <item name="save_and_continue" xsi:type="string">Webkul\Mpquotesystem\Block\Adminhtml\Managequotes\Edit\SaveAndContinueButton</item>
    </item>
</argument>
<dataSource name="custommodule_edit_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Vendor\ModuleName\Ui\DataProvider\Form\DataProvider</argument>
        <argument name="name" xsi:type="string">custommodule_edit</argument>
        <argument name="primaryFieldName" xsi:type="string">id</argument>
        <argument name="requestFieldName" xsi:type="string">id</argument>
    </argument>
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
        </item>
    </argument>
</dataSource>
</form>

Vendor\ModuleName\Ui\DataProvider\Form\DataProvider

<?php

namespace Vendor\ModuleName\Ui\DataProvider\Form;

use Magento\Ui\DataProvider\AbstractDataProvider;

class DataProvider extends AbstractDataProvider
{
    /**
     * @var array
     */
    private $loadedData;
    /**
     * @var \Vendor\ModuleName\Model\ResourceModel\Model\Collection
     */
    private $collection;

    /**
     * DataProvider constructor.
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        \Vendor\ModuleName\Model\ResourceModel\Model\CollectionFactory $collectionFactory,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->collection = $collectionFactory->create();
    }

    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {
        $this->loadedData = [];
        return $this->loadedData;
    }
}

In the above DataProvider, Replace your Model Collection.

After that run the commands :

rm -rf generated/* var/cache/*
bin/magento setup:di:compile
bin/magento ccache:flush

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.