Jump to content

How to add custom block in AdminOrder View with override or decorate


Shishir Dabhi
 Share

Recommended Posts

I had implemented with override AdminOrder index page and unable to add custom block in view indexhtml.twig file and file is getting replace with new .html.twig file. Please anyone can help me out on showing custom block on order view page

Below has been implemented with folder and file structure in modules.

1. config > routes.yml

2. module.php

3. src > Controller > Admin > CustomOrdersController.php

4. views>templates>admin>sell>order>index.html.twig

 

1. Routes.yml

admin_orders_index:
    path: /sell/orders/
    # POST is required because admin_order_index is also setup as the grid reset return route.
    methods: [GET,POST]
    defaults:
      _controller: 'module\Controller\Admin\CustomOrdersController::indexAction'
      _disable_module_prefix: true

2. CustomOrdersController.php

<?php

namespace module\Controller\Admin;

if (!defined('_PS_VERSION_')) {
    die;
}

use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use PrestaShopBundle\Security\Annotation\ModuleActivated;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

class CustomOrdersController extends FrameworkBundleAdminController
{
    /**
     * Shows list of orders
     *
     * @param Request $request
     * @param OrderFilters $filters
     *
     * @return Response
     */
    public function indexAction(Request $request)
    {
        return $this->render(
            '@Modules/module/views/templates/admin/sell/order/index.html.twig',
            [
                'enableSidebar' => true,
            ]
        );
    }
}

3. index.html.twig

{{ 'Hello world!' }}
 

custom_order.png

Share this post


Link to post
Share on other sites

services.yml:
 

namespace.custom_order:
    class: 'PrestaShop\Module\ModuleName\Controller\Admin\Sell\Order\CustomOrderController'
    decorates: 'PrestaShopBundle\Controller\Admin\Sell\Order\OrderController'
    arguments: [ '@namespace.custom_order.inner' ]

where namespace is your module namespace from composer and ModuleName is your module's name.
CustomOrderController:
 

        /**
         * @var OrderController
         */
        private $decoratedController;


        /**
         * CustomOrderController constructor.
         *
         * @param OrderController $decoratedController
         */
        public function __construct(OrderController $decoratedController)
        {
            $this -> decoratedController = $decoratedController;
        }

then follow up with the rest of the parent controller's methods and your own methods.

Share this post


Link to post
Share on other sites

Thanks Rhobur... still not able to add custom block into it because controller is not getting called, please find below structure and respective file code, can you help and  let me know what need to be corrected.

Namespace = ABC

moduleName = xyz

--composer.json
--config
    --services.yml
--src
    --Controller
        --Admin
               --Sell
                   --Order
                       --CustomOrderController.php
--views
    --templates
        --admin
            --sell
                --order
                    --view.html.twig

1. composer.json

{
    "name": "xyz/testmodule",
    "description": "Showcases how to override a Symfony controller route",
    "type": "prestashop-module",
    "authors": [
        {
            "name": "XYZ",
            "email": "[email protected]"
        }
    ],
    "autoload": {
        "psr-4": {
            "xyz\\": "src/"
        },
        "classmap": [
            "xyz.php"
        ],
        "exclude-from-classmap": []
    },
    "config": {
        "preferred-install": "dist",
        "prepend-autoloader": false
    }
}
 

2.services.yml

ABC.custom_order:
    class: 'xyz\Controller\Admin\Sell\Order\CustomOrderController'
    decorates: 'PrestaShopBundle\Controller\Admin\Sell\Order\OrderController'
    arguments: [ '@ABC.custom_order.inner' ]

 

3. CustomOrderController.php

ABC xyz\Controller\Admin\Sell\Order;

if (!defined('_PS_VERSION_')) {
    die;
}

use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use PrestaShopBundle\Security\Annotation\ModuleActivated;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

class CustomOrdersController extends FrameworkBundleAdminController
{
    /**
    * @var OrderController
    */
    private $decoratedController;


    /**
    * CustomOrderController constructor.
    *
    * @param OrderController $decoratedController
    */
    public function __construct(OrderController $decoratedController)
    {
        echo "sasas"; exit;
        $this->decoratedController = $decoratedController;
    }
    
    public function viewAction(int $orderId, Request $request)
    {
        echo $orderId; exit;

        return $this->render( '@Modules/xyz/views/templates/admin/view.html.twig' );
    }
}

 

 

Share this post


Link to post
Share on other sites

you need to extend the core view.html.twig in your own view.html.twig by adding there your module's twig file; your viewAction should return the extended view.html.twig

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More