1

When I open my admin grid form and click the Save button, nothing happens, the button just displays as clicked (no network activity, no errors in console or logs, doesnt reach Index/Save.php). When I comment out line 37 'class_name' => Container::SPLIT_BUTTON, from SaveButton.php everything works fine (except the button is not split as it should be), which leads me to believe that the root of the issue is the button / connecting it to the form. enter image description here

Vendor/Faq/view/adminhtml/ui_component/mb_faq_questions_form.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">mb_faq_questions_form.mb_faq_questions_form_data_source</item>
            <item name="deps" xsi:type="string">mb_faq_questions_form.mb_faq_questions_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Edit Question</item>
        <item name="layout" xsi:type="array">
            <item name="type" xsi:type="string">tabs</item>
        </item>
    </argument>

    <settings>
        <buttons>
            <button name="save" class="Vendor\Faq\Block\Adminhtml\Questions\Edit\SaveButton"/>
            <button name="back" class="Vendor\Faq\Block\Adminhtml\Questions\Edit\BackButton"/>
        </buttons>
        <namespace>mb_faq_questions_form</namespace>
        <dataScope>data</dataScope>
        <deps>
            <dep>mb_faq_questions_form.mb_faq_questions_form_data_source</dep>
        </deps>
    </settings>

    <dataSource name="mb_faq_questions_form_data_source">
        <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>
        <settings>
            <submitUrl path="mb_faq/index/save"/>
        </settings>
        <dataProvider class="Vendor\Faq\Model\Questions\DataProvider" name="mb_faq_questions_form_data_source">
            <settings>
                <requestFieldName>id</requestFieldName>
                <primaryFieldName>entity_id</primaryFieldName>
            </settings>
        </dataProvider>
    </dataSource>

    <fieldset name="question">
        <settings>
            <label/>
        </settings>

        <!-- This field represents form id and is hidden -->
        <field name="entity_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="visible" xsi:type="boolean">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">questions</item>
                </item>
            </argument>
        </field>

        <field name="status" sortOrder="10" formElement="checkbox">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">questions</item>
                    <item name="default" xsi:type="number">1</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
                <dataType>boolean</dataType>
                <label translate="true">Enable Question</label>
                <dataScope>status</dataScope>
            </settings>
            <formElements>
                <checkbox>
                    <settings>
                        <valueMap>
                            <map name="false" xsi:type="number">0</map>
                            <map name="true" xsi:type="number">1</map>
                        </valueMap>
                        <prefer>toggle</prefer>
                    </settings>
                </checkbox>
            </formElements>
        </field>

        <field name="question">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Question</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">questions</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
            </settings>
        </field>

        <field name="answer">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Answer</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">textarea</item>
                    <item name="source" xsi:type="string">questions</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
            </settings>
        </field>

    </fieldset>
</form>

Vendor/Faq/Block/Adminhtml/Questions/Edit/GenericButton.php

<?php

declare(strict_types=1);

namespace Vendor\Faq\Block\Adminhtml\Questions\Edit;

use Magento\Backend\Block\Widget\Context;
use Vendor\Faq\Api\QuestionsRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;

/**
 * Class GenericButton
 */
class GenericButton
{
    protected $context;
    protected $questionRepository;

    public function __construct(
        Context $context,
        QuestionsRepositoryInterface $questionRepository
    ) {
        $this->context = $context;
        $this->questionRepository = $questionRepository;
    }

    /**
     * Return FAQ question ID
     *
     * @return int|null
     */
    public function getQuestionId()
    {
        try {
            return $this->questionRepository->getById(
                $this->context->getRequest()->getParam('id')
            )->getId();
        } catch (NoSuchEntityException $e) {
        }
        return null;
    }

    public function getUrl($route = '', $params = []): string
    {
        return $this->context->getUrlBuilder()->getUrl($route, $params);
    }
}

Vendor/Faq/Block/Adminhtml/Questions/Edit/SaveButton.php

<?php

declare(strict_types=1);

namespace Vendor\Faq\Block\Adminhtml\Questions\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magento\Ui\Component\Control\Container;

class SaveButton extends GenericButton implements ButtonProviderInterface
{
    /**
     * Get button data
     *
     * @return array
     */
    public function getButtonData(): array
    {
        return [
            'label' => __('Save'),
            'class' => 'save primary',
            'data_attribute' => [
                'mage-init' => [
                    'buttonAdapter' => [
                        'actions' => [
                            [
                                'targetName' => 'mb_faq_questions_form.mb_faq_questions_form',
                                'actionName' => 'save',
                                'params' => [
                                    false
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'class_name' => Container::SPLIT_BUTTON,
            'options' => $this->getOptions(),
            'sort_order' => 90,
            'dropdown_button_aria_label' => __('Save options'),
        ];
    }

    /**
     * Retrieve options
     *
     * @return array
     */
    private function getOptions(): array
    {
        $options = [
            [
                'id_hard' => 'save_and_close',
                'label' => __('Save & Close'),
                'data_attribute' => [
                    'mage-init' => [
                        'buttonAdapter' => [
                            'actions' => [
                                [
                                    'targetName' => 'mb_faq_questions_form.mb_faq_questions_form',
                                    'actionName' => 'save',
                                    'params' => [
                                        true,
                                        [
                                            'back' => 'close',
                                        ],
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
            ]
        ];

        return $options;
    }
}
2
  • just a shot in the dark here, but what happens if you replace false with true in the params section ?
    – Marius
    Jun 13, 2023 at 13:02
  • No changes, which is weird cause I used the implementation in Magento/module-cms @Marius ♦ Jun 14, 2023 at 4:20

1 Answer 1

0

I managed to get it fixed by changing 'class_name' => Container::SPLIT_BUTTON, to 'class_name' => SplitButton::class,. Don't forget to use Magento\Backend\Block\Widget\Button\SplitButton;

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.