I try to make an SMS form block for Magento2. Until now, I used the Magento\Sales\Block\Adminhtml\Order\View\History as a guide on how to implement it. I am almost there, but I cannot mimic the getUrl function for my custom button. I tried overriding the order model but no luck calling my function.
inside Vendor\Module\Block\Adminhtml\Order\View\Sendsms.php I have
<?php namespace Vendor\Module\Block\Adminhtml\Order\View; use Magento\Sales\Model\Order; use Magento\Framework\App\ObjectManager; use Magento\Framework\Controller\ResultFactory; use Magento\Shipping\Helper\Data as ShippingHelper; use Magento\Tax\Helper\Data as TaxHelper; class SendSMS extends \Magento\Backend\Block\Widget { /** * Core registry * * @var \Magento\Framework\Registry */ protected $_coreRegistry = null; /** * Admin helper * * @var \Magento\Sales\Helper\Admin */ protected $_adminHelper; /** * @param \Magento\Backend\Block\Template\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Sales\Helper\Admin $adminHelper * @param array $data * @param ShippingHelper|null $shippingHelper * @param TaxHelper|null $taxHelper */ public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Sales\Helper\Admin $adminHelper, array $data = [], ?ShippingHelper $shippingHelper = null, ?TaxHelper $taxHelper = null ) { $this->_adminHelper = $adminHelper; $this->_coreRegistry = $registry; $data['shippingHelper'] = $shippingHelper ?? ObjectManager::getInstance()->get(ShippingHelper::class); $data['taxHelper'] = $taxHelper ?? ObjectManager::getInstance()->get(TaxHelper::class); parent::__construct($context, $data); } /** * Preparing global layout * * @return $this */ protected function _prepareLayout() { $onclick = "submitAndReloadArea($('order_sendsms').parentNode, '" . $this->getSubmitUrl() . "')"; $button = $this->getLayout()->createBlock( \Magento\Backend\Block\Widget\Button::class )->setData( ['label' => __('Send'), 'class' => 'action-save action-secondary', 'onclick' => $onclick] ); $this->setChild('submit_button', $button); return parent::_prepareLayout(); } /** * Submit URL getter * * @return string */ public function getSubmitUrl() { return $this->getUrl('sales/*/idk', ['order_id' => $this->getOrder()->getId()]); } }
Inside etc/di.xml I have
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Sales\Model\Order" type="Vendor\Module\Model\Order" /> </config>
And Inside Vendor\Module\Model\Order.php I have
<?php namespace Vendor\Module\Model; class Order extends \Magento\Sales\Model\Order { public function idk() { error_log("idk"); } }
I skipped phtml files because they are working as expected. My "idk" function is never called. Any help?
Solved! Go to Solution.
Magento\Sales\Model\Order
Magento\Sales\Model\Order
I don't understand the last part. My goal is to call a function in PHP when someone clicks a button
And I want to be able to get the data a user added inside my custom block
Just ignore me. I forgot about the controller