Question

So I need to add a Save & Close button on the product grid. like that It even works, but not quite so. On clicking that button product is saved, and redirect to product grid happens. But with an error "Invalid security or form key. Please refresh the page.": like that. In page code, I see that form keys on the back button and Save & Close button are different.

Button is added via product_form.xml :

<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-close-button" xsi:type="string">Vendor\Module\Block\Adminhtml\Product\Edit\Button\Save</item>
        </item>
    </argument>

Button class:

class Save extends Generic
{
    /**
     * @inheritdoc
     */
    public function getButtonData()
    {
        if ($this->getProduct()->isReadonly()) {
            return [];
        }

        return [
            'id_hard' => 'save_and_close_add',
            'label' => __('Save & Close'),
            'class' => 'save',
            'data_attribute' => [
                'mage-init' => [
                    'buttonAdapter' => [
                        'actions' => [
                            [
                                'targetName' => $this->getSaveTarget(),
                                'actionName' => $this->getSaveAction(),
                                'params' => [
                                    true
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'class_name' => Container::DEFAULT_CONTROL,
        ];
    }

    /**
     * Retrieve target for button.
     *
     * @return string
     */
    protected function getSaveTarget()
    {
        $target = 'product_form.product_form';
        if ($this->isConfigurableProduct()) {
            $target = 'product_form.product_form.configurableVariations';
        }
        return $target;
    }

    /**
     * Retrieve action for button.
     *
     * @return string
     */
    protected function getSaveAction()
    {
        $action = 'save';
        if ($this->isConfigurableProduct()) {
            $action = 'saveFormHandler';
        }
        return $action;
    }

    /**
     * Is configurable product.
     *
     * @return boolean
     */
    protected function isConfigurableProduct()
    {
        return !$this->getProduct()->isComposite() || $this->getProduct()->getTypeId() === ConfigurableType::TYPE_CODE;
    }

Is there a way to fix that? What am I doing wrong?

Was it helpful?

Solution

I found an answer.

return [
            'id_hard' => 'save_and_close_add',
            'label' => __('Save & Close'),
            'class' => 'save',
            'data_attribute' => [
                'mage-init' => [
                    'buttonAdapter' => [
                        'actions' => [
                            [
                                'targetName' => $this->getSaveTarget(),
                                'actionName' => $this->getSaveAction(),
                                'params' => [
                                    true
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'class_name' => Container::DEFAULT_CONTROL,
        ];
    }

should include an on_click key with empty string.

Like that:

 return [
            'id_hard' => 'save_and_close_add',
            'label' => __('Save & Close'),
            'class' => 'save',
            'data_attribute' => [
                'mage-init' => [
                    'buttonAdapter' => [
                        'actions' => [
                            [
                                'targetName' => $this->getSaveTarget(),
                                'actionName' => $this->getSaveAction(),
                                'params' => [
                                    true
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'class_name' => Container::DEFAULT_CONTROL,
            'on_click' => '',
        ];
    }

OTHER TIPS

Fist of all, I don't think you need a Save & Close button. There is already one.
enter image description here
But for the sake of it, let's say you want a new one.
I think the problem is the targetName.
For your configurable products the targetName is product_form.product_form.configurableVariations.
This fieldset does not contain the form_key hidden input so nothing gets sent as form_key. Hence the error message.
I see that for the default Save & Close button the targetName is product_form.product_form. Maybe you should use that one also.
(See Magento\Catalog\Block\Adminhtml\Product\Edit\Button\Save) for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top