0

I am creating a suborder view page in BackOffice that retrieves data from the customized table. However, I received an error on above topic when I click the view button in my customized grid. Did I write the wrong code?

app/code/MyVendor/SubOrder/Controller/Adminhtml/Order/View.php

<?php

namespace MyVendor\SubOrder\Controller\Adminhtml\Order;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use MyVendor\SubOrder\Model\ResourceModel\SubOrder\CollectionFactory;
use Magento\Framework\App\Action\Action;

class View extends Action
{
/**
 * @var PageFactory
 */
protected $resultPageFactory;

/**
 * @var CollectionFactory
 */
protected $subOrderCollectionFactory;

/**
 * View constructor.
 * @param Context $context
 * @param PageFactory $resultPageFactory
 * @param CollectionFactory $subOrderCollectionFactory
 */
public function __construct(
    Context $context,
    PageFactory $resultPageFactory,
    CollectionFactory $subOrderCollectionFactory
) {
    parent::__construct($context);
    $this->resultPageFactory = $resultPageFactory;
    $this->subOrderCollectionFactory = $subOrderCollectionFactory;
}

/**
 * Load the page defined in view.xml layout file
 *
 * @return \Magento\Framework\View\Result\Page
 */
public function execute()
{
    $orderId = $this->getRequest()->getParam('order_id');
    var_dump($orderId); die;
    $orderCollection = $this->subOrderCollectionFactory->create();
    $orderCollection->addFieldToFilter('entity_id', $orderId);
    $order = $orderCollection->getFirstItem();

    $resultPage = $this->resultPageFactory->create();
    $resultPage->getConfig()->getTitle()->prepend(__('Order #%1', $order->getIncrementId()));
    return $resultPage;
}
 }
?>

I created an action column for viewing the order details app/code/MyVendor/SubOrder/view/adminhtml/ui_component/myvendor_suborder_order_listing.xml

<actionsColumn name="actions" class="MyVendor\SubOrder\Ui\Component\Listing\Column\ViewAction">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="viewUrlPath" xsi:type="string">myvendor_suborder/order/view</item>
                <item name="urlEntityParamName" xsi:type="string">order_id</item>
            </item>
        </argument>
        <settings>
            <indexField>entity_id</indexField>
        </settings>
    </actionsColumn>

app/code/MyVendor/SubOrder/Ui/Component/Listing/Column/ViewAction.php

<?php

 namespace MyVendor\SubOrder\Ui\Component\Listing\Column;

 use Magento\Framework\UrlInterface;
 use Magento\Framework\View\Element\UiComponent\ContextInterface;
 use Magento\Framework\View\Element\UiComponentFactory;
 use Magento\Ui\Component\Listing\Columns\Column;

 class ViewAction extends Column
 {
/**
 * @var UrlInterface
 */
protected $urlBuilder;

/**
 * Constructor
 *
 * @param ContextInterface $context
 * @param UiComponentFactory $uiComponentFactory
 * @param UrlInterface $urlBuilder
 * @param array $components
 * @param array $data
 */
public function __construct(
    ContextInterface $context,
    UiComponentFactory $uiComponentFactory,
    UrlInterface $urlBuilder,
    array $components = [],
    array $data = []
) {
    $this->urlBuilder = $urlBuilder;
    parent::__construct($context, $uiComponentFactory, $components, $data);
}

/**
 * Prepare Data Source
 *
 * @param array $dataSource
 * @return array
 */
public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        $fieldName = $this->getData('name');
        foreach ($dataSource['data']['items'] as &$item) {
            $item[$fieldName]['view'] = [
                'href' => $this->urlBuilder->getUrl(
                    $this->getData('config/viewUrlPath'),
                    [
                        $this->getData('config/urlEntityParamName') => $item['entity_id']
                    ]
                ),
                'label' => __('View')
            ];
        }
    }

    return $dataSource;
}

}

1
  • Run setup:di:compile Feb 19, 2023 at 18:31

1 Answer 1

0

Have you register entry of myvendor_suborder in your etc/adminhtml/routes.xml like below?

<?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 frontName="myvendor_suborder" id="myvendor_suborder">
        <module before="Magento_Backend" name="MyVendor_SubOrder"/>
    </route>
</router>
3
  • Yes I have it but still same error
    – hill2011
    Feb 19, 2023 at 4:21
  • Have you tried php bin/magento setup:static-content:deploy ? Feb 19, 2023 at 13:48
  • it's ok after running setup:static-content:deploy
    – hill2011
    Feb 20, 2023 at 3:12

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.