0

I am creating a custom form in a custom tab of Sales Order View. When I click on Use Template button, it redirects me to Page Not Found 404. I want to show a success/fail message and redirect to same page of the form instead. Any solution for this?

enter image description here

Below is what I have done:

Vendor/Module/Block/Adminhtml/Orderedit/Tab/View.php

<?php

   namespace MarsMage\Eversign\Block\Adminhtml\Orderedit\Tab;

   use Magento\Framework\Data\Form\FormKey;
   use Magento\Framework\View\Element\Template;

  class View extends Template implements \Magento\Backend\Block\Widget\Tab\TabInterface
 {
   protected $_template = 'tab/view/eversign.phtml';

protected $_logger;

protected $_curl;

protected $formKey;

protected $_backendUrl;

public function __construct(
    Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\HTTP\Client\Curl $curl,
    FormKey $formKey,
    array $data = []
) {
    $this->_coreRegistry = $registry;
    parent::__construct($context, $data);
    $this->_curl = $curl;
    $this->_logger = $logger;
    $this->formKey = $formKey;
}

public function getOrder()
{
    return $this->_coreRegistry->registry('current_order');
}

public function getOrderId()
{
    return $this->getOrder()->getEntityId();
}

public function getOrderIncrementId()
{
    return $this->getOrder()->getIncrementId();
}

public function getTabLabel()
{
    return __('Legal Document');
}

public function getTabTitle()
{
    return __('Legal Document');
}

public function canShowTab()
{
    return true;
}

public function isHidden()
{
    return false;
}

public function getDocumentTemplate()
{
    // $url = 'https://api.eversign.com/api/document?access_key=9b735cc49cf593754c8cc54cec2cc71e&business_id=280557&type=templates';
    // $this->curl->get($url);

    // $result = $this->curl->getBody();

    // return $result;
   try{
    $apiUrl = 'https://api.eversign.com/api/document?access_key=9b735cc49cf593754c8cc54cec2cc71e&business_id=280557&type=templates';
    $this->_curl->addHeader('Content-Type', 'application/json');
    $this->_curl->get($apiUrl);
    $response = $this->_curl->getBody();
    $responseData = json_decode($response, TRUE);

    return $responseData;

    // $em  =  array();
    // $this->_logger->Debug($responseData,$em);
   }
   catch(\Exception $e){
    $this->_logger->critical('Error Curl', ['exception' => $e]);
   }
   
    
}

/**
 * Get form action URL for POST booking request
 *
 * @return string
 */
public function getFormAction()
{
    return $this->getUrl('eversign/adminhtml/form');
    // here controller_name is index, action is booking
}

public function getFormKey()
{
    return $this->formKey->getFormKey();
}

 }

Vendor/Module/view/adminhtml/templates/tab/view/eversign.phtml

<?php

/**
 * @var $block \MarsMage\Eversign\Block\Adminhtml\Order\View\Tab\View
 */

$template = $block->getDocumentTemplate();

?>


<div class="fieldset-wrapper order-information">
  <div class="fieldset-wrapper-title">
  <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Legal Document') ?></span></legend>
  </div>
  <div class="template-form-box" id="template-form-box" >
    <form id="template-form" action="<?php echo $block->getFormAction(); ?>" class="templateform"  method="post">
      <label for="template-radio">Template</label></br>
      
      <input type="hidden" name="form_key" value="<?php echo $block->getFormKey(); ?>" />

      <?php foreach ($template as $value) : ?>

        <input type="radio" name="template" value="<?php echo $value['title']; ?>"><?php echo $value['title']; ?></input></br>
      <?php endforeach; ?>

      <input id="templatesubmit" name="author" type="submit" value="Use Template" />

    </form>

    <div class="loading-mask" data-role="loader" style="visibility: hidden;">
      <div class="loader">
        <img alt="Loading..." src="<?php $block->getViewFileUrl('images/loader-1.gif')?>">
        <p>Creating Document...</p>
      </div>
    </div>

  </div>


  <table class="admin__table-secondary">
    <tbody>
      <tr>
        <th><?php echo __('Purchase Agreement Document:') ?></th>
        <td><?php echo $block->getOrderIncrementId(); ?></td>
      </tr>
      <tr>
        <th><?php echo __('Hire Purchase Document:') ?></th>
        <td><?php echo __('History of Order') ?></td>
      </tr>
    </tbody>
  </table>
</div>

<script>

require(['jquery'],function($){
    $('#template-form').submit(function() {
    $('.loading-mask').css('visibility', 'visible');
});
});
  </script>

Vendor/Module/Controller/Adminhtml/Form.php

<?php

namespace MarsMage\Eversign\Controller\Adminhtml;

use Magento\Backend\App\Action\Context;
use \Magento\Framework\Controller\ResultFactory;

class Form extends \Magento\Framework\App\Action\Action
{

protected $_messageManager;

public function __construct(
    Context $context,
    \Magento\Framework\Message\ManagerInterface $messageManager
) {
    parent::__construct($context);
    $this->_messageManager = $messageManager;
}

public function execute()
{
    $post = $this->getRequest()->getParams();
    // Do Validation
    if (empty($post)) {
        $this->_messageManager->addErrorMessage('Your Error Message');
    } else {
        $this->_messageManager->addSuccessMessage('Your Success Message');
    }

   
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    $resultRedirect->setUrl($this->_redirect->getRefererUrl());

    return $resultRedirect;
}

}

Vendor/Module/etc/adminhtml/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="eversign" frontName="eversign">
            <module name="MarsMage_Eversign"/>
        </route>
    </router>
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.