0

I need to print log info when a button is clicked. I am using the below code, please let me know what are the mistakes in my code. I am trying to call a function like below.

'on_click' => 'execute();',

Button.php

<?php
namespace Dss\CoveoSearch\Block\System\Config;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Data\Form\Element\AbstractElement;
class Button extends Field
{
    protected $_template = 'Dss_CoveoSearch::system/config/button.phtml';
    protected $_logger;
    public function __construct(\Dss\CoveoSearch\Logger\Logger $logger, Context $context, array $data = [])
    {
        parent::__construct($context, $data);
        $this->_logger = $logger;
    }

    public function render(AbstractElement $element)
    {
        $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
        return parent::render($element);
    }
    protected function _getElementHtml(AbstractElement $element)
    {
        return $this->_toHtml();
    }
    public function getCustomUrl()
    {
        return $this->getUrl('hello');
    }
    public function hello()
    {
        $test = 'hello';
        echo $test;
    }
    public function execute()
    {
        $this->_logger->info('Hello This is our Custom log file example');
    }
    public function getButtonHtml()
    {
        $button = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Button')->setData(['id' => 'btn_id', 'label' => __('Create'),'on_click' => 'execute();',]);
        return $button->toHtml();
        
    }
} 

button.phtml

<?php
    $controller = $block->getCustomUrl();
    echo $block->hello();
    echo $block->getButtonHtml();
?>
4
  • What's the error you're encountering? Mar 2 '21 at 4:08
  • not getting any error but the info is not printing in the log.
    – Ajmalriyaz
    Mar 2 '21 at 4:24
  • echo $block->hello(); is this working? Did you try echoing something instead of logging on button click? Mar 2 '21 at 4:29
  • yes, this is working. If I try to echo something then it is displaying near to button.
    – Ajmalriyaz
    Mar 2 '21 at 4:31
1

I tried this for an Ajax call and it's working like a charm. See if this could help.

public function getButtonHtml()
    {
        $button = $this->getLayout()->createBlock(
            'Magento\Backend\Block\Widget\Button'
        )->setData(
            [
                'id' => 'clear_button',
                'label' => __('Clear'),
                'on_click' => sprintf("location.href = '%s';", $this->getAjaxUrl()),
            ]
        );

        return $button->toHtml();
    }

public function getAjaxUrl()
    {
        return $this->getUrl('vendor_module/system_config/ClearCtrl');
    }

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.