3

Hi i'm new at magento2 and i want to put a PHP function in an "on click" attribute of a button on ui component form.

Specifically on this "Save Header" button. button

So, actually i want to use this "Save Header" button to just save two fields of the form, the fields (Nome Header, Valor Header).

form

The "on click" attribute. In this "on click" i want to call a php function...

on click

routes.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="admin">
    <route id="api" frontName="api">
        <module name="Hub_Api" />
    </route>
</router>

directories

------------------------------EDIT---------------------------------

So now my "SaveHeader" button is like that, but still don't working, maybe the url is passed wrong: new saveheader button

SaveHeader.php (Controller):

namespace Hub\Api\Controller\Adminhtml\Data;

class SaveHeader extends \Magento\Framework\App\Action\Action { protected $logger;

public function __construct(
    \Psr\Log\LoggerInterface $logger
) {
    $this->logger = $logger;
    parent::__construct();
}

/**
 * View  page action
 *
 * @return void
 */
public function execute()
{
    $txt = 'HUDSON SAVEHEADER : ';
    $this->logger->log('DEBUG', $txt);

}

}

7
  • you want to save button click and call the controller ??? Commented Jun 16, 2020 at 18:25
  • Yes, actually i want to use this "Save Header" button to save just one field of the form. I will update the post with the entire form. Commented Jun 16, 2020 at 18:27
  • i am not sure but may be help you Save Header button controller path set this type magento.stackexchange.com/a/274310/85907 Commented Jun 16, 2020 at 18:30
  • check other link --- magento.stackexchange.com/questions/91071/… Commented Jun 16, 2020 at 18:36
  • Actually i created the button, but i want to put an php function on the "on click" attribute Commented Jun 16, 2020 at 19:23

3 Answers 3

1

try this

public function getButtonData()
{
    $message ='Are you sure you want to do this?';
    $url = $this->getUrl("api/data/deleteheader"); 
    return
        [
            'label' => __('Save Header'),
            'class' => 'myclass',
            'on_click' => 'confirmSetLocation('{$message}', '{$url}')',
            'sort_order' => 100
    ];
}

You can write you php code into this controller action file.

I Hope This Helps You.

8
  • Would be something like this? $url = 'Hub_Api/Controller/SaveHeader/id/' . $this->getDataId(); I don't know, i think i'm passing the url wrong. Commented Jun 18, 2020 at 12:29
  • give screenshot of your router.xml file for admin and controller action code syntax Commented Jun 18, 2020 at 12:48
  • hey, i put the code for routes.xml and the directories. The "SaveHeader" controller is in "Controller\Adminhtml\Data" directory Commented Jun 18, 2020 at 14:06
  • Please put like this adminroutename/data/saveheader Commented Jun 19, 2020 at 4:12
  • please check my updated answer and update me Commented Jun 19, 2020 at 4:15
1

hello you can change your code with following code

$fieldset->addField('registered', 'button', array(
            'label' => Mage::helper('core')->__('Send e-mail to all registered customers'),
            'value' => Mage::helper('core')->__('Button Caption'),
            'name'  => 'registered',
            'class' => 'form-button',
            'onclick' => "setLocation('your url')",
        ));
3
  • In 'your url' you mean the Controller url? Commented Jun 17, 2020 at 18:01
  • you can also write location.reload Commented Jun 18, 2020 at 5:41
  • So the url would be like $url = 'Hub_Api/Controller/SaveHeader/id/' . $this->getDataId(); ? i think i'm passing the url wrong. Commented Jun 18, 2020 at 12:47
1

This is my idea hope it help. In getButtonData() function add on_click to javascript function something like 'saveHeader()'

add your template in layout call to UI form: sample we have MODULE-NAME_CONTROLLER-NAME-ACTION.xml this content like

<body>
   <referenceContainer name="content">
       <uiComponent name="your_ui_form"/>
       <block class="Magento\Framework\View\Element\Template" name="XXX"  template="NAMESPACE_MODULENAME::html/js.phtml"/>
   </referenceContainer>
</body>

In js.phtml file create new saveHeader() function call ajax to submit form element to your custom module controller (php).

This sample for js.phtml file content:

require([
        'jquery',
        'jquery/ui',
        'Magento_Ui/js/modal/modal'
    ], function($){
            function saveHeader() {
                 $form = $('#formId');
                 $.ajax({
                        type: 'POST',
                        url: 'url',
                        data: {
                            xxx,
                            form_key: 'qiIiwjIPOdmrA0Uq',
                            return_session_messages_only: 1
                        },
                        dataType: 'json',
                        showLoader: true,
                        context: $form
                    })
                    ...
           }

Good Luck!

3
  • For what i should replace the "xxx" ? and how i could call the controler by the button? Commented Jun 17, 2020 at 17:09
  • "xxx" are field you need send to save like nome_api: $('input[name="nome_api"]').val() when you click button it will call javascript function is saveHeader() then it will submit data to url. Url is mean your php controller file in this file you can get data and save it. All step above is mean get you "header" data and submit it to another php controller then save. Commented Jun 18, 2020 at 4:45
  • So i'm trying this but happens this exception: Invalid template file: 'Hub_Api::view/deleteheader.phtml' in module: '' block's name: 'deleteheader' Commented Jun 23, 2020 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.