Jump to content

How to properly decorate a Core Controller ?


Recommended Posts

I need to add an attribute deactivation feature into Prestashop 1.7.8.7 admin panel. In order to achieve that, I've chosen to decorate the core controller PrestaShopBundle\Controller\Admin\Sell\Catalog\AttributeController via a custom module, created by myself.

I've followed PrestaShop Documentation about controller decoration, but nothing works : after resolving the few error messages encountered, and clearing the dev cache, nothing happens. When I click on Sell > Catalog > Attributes & Features and select an attribute, the original PrestaShop page is still displayed (while I rewrote the indexAction method of the controller with a simple die() ).

Here is what I have done so far:

I've created the custom module base as shown in the documentation. Here is the code of the cl_attribute.php file:

<?php

declare(strict_types=1);

use PrestaShop\PrestaShop\Adapter\Module\Module;

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

require_once __DIR__ . '/vendor/autoload.php';

class Cl_attribute extends Module
{
    public function __construct()
    {
        $this->name = 'cl_attribute';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'John Doe';
        $this->need_instance = 1;
        $this->ps_versions_compliancy = [
            'min' => '1.7',
            'max' => _PS_VERSION_
        ];
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->trans('Attributs', [], 'Modules.Clattribute.Admin');
        $this->description = $this->trans("Module permettant d'activer ou de désactiver des attributs", [], 'Modules.Clattribute.Admin');

        $this->confirmUninstall = $this->trans('Êtes-vous sur de vouloir désinstaller ce module?', [], 'Modules.Clattribute.Admin');
    }

    public function install()
    {
        return parent::install();
    }

    public function uninstall()
    {
        return parent::uninstall();
    }
}

I've added a composer.json at the root module :

{
    "name": "xxx/cl_attribute",
    "license": "AFL-3.0",    
    "autoload": {
        "psr-4": {"Namespace\\": "src/"},
        "classmap": ["cl_attribute.php"],
        "config": {"prepend-autoloader": false},
        "type": "prestashop-module"
    }
}

I've created a config/services.yml file :

services:
  Cl_attribute:
    class: Cl_attribute\Controller\Admin\DemoController
    decorates: PrestaShopBundle\Controller\Admin\Sell\Catalog\AttributeController
    arguments: ['@cl_attribute.inner']

I've created a src/Controller/Admin/DemoController.php file :

<?php

namespace Cl_attribute\Controller\Admin;

use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Controller\Admin\Sell\Catalog\AttributeController;
use PrestaShop\PrestaShop\Core\Search\Filters\AttributeFilters;
use Symfony\Component\HttpFoundation\Request;

class DemoController extends FrameworkBundleAdminController
{
    private $decoratedController;

    public function __construct(AttributeController $decoratedController)
    {
        $this->decoratedController = $decoratedController;
    }

    public function indexAction(Request $request, $attributeGroupId, AttributeFilters $attributeFilters)
    {
        die('lol');
        // return $this->decoratedController->indexAction($request, $attributeGroupId, $attributeFilters);
    }
}

I also followed another tutorial along with the documentation, but I still can't decorate this controller.

What did I miss ?

Edited by Loraga (see edit history)
Link to comment
Share on other sites

Update : I used Xdebug to trace the route and it seems that the core controller located in "src/PrestaShopBundle/Controller/Admin/Sell/Catalog/AttributeController.php" is not called at all ! (AttributeGroupController.php no more)

However, the controller located in "controllers/admin/AdminAttributesGroupsController.php" is the right called controller. It looks like a legacy controller; I don't think we can decorate it.

What do you think about it ?

Link to comment
Share on other sites

Update V2 : Using the old way of controller override does not work either. The override is simply not taken into account for the controller "controllers/admin/AdminAttributesGroupsController.php".

However, if I manually add my override in the root folder, it works (but it's not a clean solution)

Nobody has encountered this problem?

Link to comment
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
×
×
  • Create New...