2

I am working on a custom module in magneto 2. i am creating an admin form using UI Component, when i click on save button Page redirects to its self

Vendor\Module\Block\Adminhtml\ProductSliders\Edit\SaveButton

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

\ButtonProviderInterface;

/**
 * Class SaveButton
 * @package Magento\Customer\Block\Adminhtml\Edit
 */
class SaveButton extends GenericButton implements ButtonProviderInterface
{
    /**
     * @return array 
     */
    public function getButtonData()
    {
        return [
            'label' => __('Save'),
            'class' => 'save primary',
            'data_attribute' => [
                'mage-init' => ['button' => ['event' => 'save']],
                'form-role' => 'save',
            ],
            'sort_order' => 90,
        ];
    }
}

i have make buttons using Block here is my Edit.php Class

Vendor\Module\Block\Adminhtml\ProductSliders\Edit

class Edit extends \Magento\Backend\Block\Widget\Form\Container
{

    protected $_coreRegistry = null;


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


    protected function _construct()
    {
        $this->_objectId = 'slider_id';
        $this->_blockGroup = 'FME_ProductSliders';
        $this->_controller = 'adminhtml_productsliders';
        parent::_construct();

        if ($this->_isAllowedAction('FME_ProductSliders::productsliders')) {
            $this->buttonList->update('save', 'label', __('Save'));
            $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');
        }

        if ($this->_isAllowedAction('FME_ProductSliders::productsliders')) {
            $this->buttonList->update('delete', 'label', __('Delete'));
        } else {
            $this->buttonList->remove('delete');
        }


    }

    /**
     * Retrieve text for header element depending on loaded page
     *
     * @return string
     */
    public function getHeaderText()
    {

        if ($this->_coreRegistry->registry('fme_productsliders')->getId()) {
            return __(
                "Edit Sliders '%1'",
                $this->escapeHtml($this->_coreRegistry->registry('fme_productsliders')->getTitle())
            );
        } else {
            return __('New Sliders');
        }
    }

    /**
     * Check permission for passed action
     *
     * @param string $resourceId
     * @return bool
     */
    public 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
     */


    public function _getSaveAndContinueUrl()
    {

        return $this->getUrl(
            'fme_productsliders/*/save',
            ['_current' => true, 
            'back' => 'edit', 
            'active_tab' => '{{tab_id}}']
        );
    }


    /**
     * Prepare layout
     *
     * @return \Magento\Framework\View\Element\AbstractBlock
     */
    protected function _prepareLayout()
    {
        $this->_formScripts[] = "
            function toggleEditor() {
                if (tinyMCE.getInstanceById('overview') == null) {
                    tinyMCE.execCommand('mceAddControl', false, 'overview');
                } else {
                    tinyMCE.execCommand('mceRemoveControl', false, 'overview');
                }
            };
        ";
        return parent::_prepareLayout();
    }
}

Here is my save.php controller

Vendor\Module\Controller\Adminhtml\Module;

class Save extends \Magento\Backend\App\Action
{
            var $sliderFactory;


    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \FME\ProductSliders\Model\ProductSlidersFactory $sliderFactory
    ) {
        parent::__construct($context);
        $this->sliderFactory = $sliderFactory;
    }


    public function execute()
    {
        $data = $this->getRequest()->getPostValue();
        if (!$data) {

            $this->_redirect('fme_productsliders/productsliders/edit');
            return;
        }
        try {
            $rowData = $this->sliderFactory->create();
            $rowData->setData($data);
            if (isset($data['slider_id'])) {
                $rowData->setEntityId($data['slider_id']);
            }
            $rowData->save();
            $this->messageManager->addSuccess(__('Row data has been successfully saved.'));
        } catch (\Exception $e) {
            $this->messageManager->addError(__($e->getMessage()));
        }
        $this->_redirect('fme_productsliders/productsliders/index');
    }

    /**
     * @return bool
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('FME_ProductSliders::save');
    }
}

i Need a Help if anyone can??

1

2 Answers 2

0

You can try to set current to false

public function _getSaveAndContinueUrl()
    {

        return $this->getUrl(
            'fme_productsliders/*/save',
            ['_current' => false, 
            'back' => 'edit', 
            'active_tab' => '{{tab_id}}']
        );
    }

Update: Add a return in execute method

public function execute()
    {
        $data = $this->getRequest()->getPostValue();
        if (!$data) {

            $this->_redirect('fme_productsliders/productsliders/edit');
            return;
        }
        try {
            $rowData = $this->sliderFactory->create();
            $rowData->setData($data);
            if (isset($data['slider_id'])) {
                $rowData->setEntityId($data['slider_id']);
            }
            $rowData->save();
            $this->messageManager->addSuccess(__('Row data has been successfully saved.'));
        } catch (\Exception $e) {
            $this->messageManager->addError(__($e->getMessage()));
        }
        return $this->_redirect('fme_productsliders/productsliders/index');
    }
4
  • not working how can i check this method is called not not
    – Waqar Ali
    Commented Apr 17, 2019 at 5:22
  • 1
    @WaqarAli updated the answer please check
    – fmsthird
    Commented Apr 17, 2019 at 5:49
  • Thank you foyour help but i solved problem by adding <item name="save" xsi:type="array"> <item name="name" xsi:type="string">save</item> <item name="label" xsi:type="string" translate="true">save</item> <item name="class" xsi:type="string">primary</item> </item>
    – Waqar Ali
    Commented Apr 17, 2019 at 12:13
  • 1
    i got help from [magento.stackexchange.com/questions/139639/…
    – Waqar Ali
    Commented Apr 17, 2019 at 12:15
0

Thank you for your help but i solved problem by adding

<item name="save" xsi:type="array"> 
<item name="name" xsi:type="string">save</item> 
<item name="label" xsi:type="string" translate="true">save</item> 
<item name="class" xsi:type="string">primary</item> </item>

i got help from Here

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.