How to override Admin controller or action

Hi @mertaivan,

we would share our solution, but unfortunately it isn’t working yet.
In our last post is every line of code our bundle contains. We still hope for some help from @dpfaffenbauer

When there is a working solution we’ll share the bundle here, promised :wink:

did you try to clear the cache?

Hi @dpfaffenbauer

Clearing cache didn’t work for me and I am still getting

Message: Controller “AppBundle\Controller\DataObjectController::treeGetChildsByIdAction()” requires that you provide a value for the “$eventDispatcher” argument.

Thanks

Cache clearing also isn’t working for us.
@dpfaffenbauer did you try our bundle (which I’ve uploaded for you)?

will test your bundle and publish it here later today.

there you go guys, both of you owe me a beer now, this took way longer than expected… had to debug symfony in order to understand how autowiring works. :stuck_out_tongue:

PimcoreModBundle.zip (5.0 KB)

the bundle now uses configuration to overwrite controllers, simply add following to your config.yml

pimcore_mod:
    admin_controllers:
        Pimcore\Bundle\AdminBundle\Controller\Admin\DataObjectController: AppBundle\Controller\Admin\DataObjectController

example controller:

<?php

namespace AppBundle\Controller\Admin;

use Pimcore\Bundle\AdminBundle\Controller\Admin\DataObjectController as BaseDataObjectController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class DataObjectController extends BaseDataObjectController
{
    public function treeGetChildsByIdAction(Request $request, EventDispatcherInterface $eventDispatcher = null)
    {
        $result = parent::treeGetChildsByIdAction($request, $eventDispatcher);

        if ($result instanceof JsonResponse) {
            $json = $result->getContent();
            $json = json_decode($json, true);

            $json['blub'] = 'blab';

            return new JsonResponse($json);
        }

        return $result;
    }

}
1 Like

You’re indeed a master, @dpfaffenbauer. I have no idea what you’re doing there (I am just getting started with Symfony because of Pimcore) but it works - I changed above in the same manner for AppBundle.
Thanks a lot and ask me for a beer when you’re in Prague :).

:wink:

Sure I will come back to Prague one day :wink: I will come back to your offer.

Yeah! Finally it’s working !
Great, thank you very much for your help.

If you ever come to cologne, you will get beer :wink:

I will come back to your offer as well :wink:

+1 Beer @dpfaffenbauer if you’re in the UK, this saved my day!

I am there next week :wink:

Hey dpfaffenbauer, want to increase your beer-credit in Sweden as well?

I’m after a similar solution (need to override Admin\MiscController) but I can’t even get the zip you provided to work … Can you please describe the key components to override an admin controller? What parts of the code in the bundle zip are important specifically for overriding?

@patrickfabrizius That Bundle should also work for you.

Try placing it into your src Directory and enable it in AppBundle.php by doing this:

<?php

use Pimcore\HttpKernel\BundleCollection\BundleCollection;
use Pimcore\Kernel;

class AppKernel extends Kernel
{
    public function registerBundlesToCollection(BundleCollection $collection)
    {
        $collection->addBundle(new \AppBundle\AppBundle);
        $collection->addBundle(new \PimcoreModBundle\PimcoreModBundle);
    }
}

I assume you mean app/AppKernel.php ? I tried adding PimcoreModBundle there, added the contents of the zip under src/, modified app/config/config.yml and added src/AppBundle/Controller/DataObjectController.php according to your earlier entry. I get two exceptions:

### (1/2) InvalidArgumentException
There is no extension able to load the configuration for "pimcore_mod"

### (2/2) FileLoaderLoadException
There is no extension able to load the configuration for "pimcore_mod"

Assuming “pimcore_mod” should instead be “pimcore”, I get a different exception:

Class "AppBundle\Controller\Admin\DataObjectController" used for service "AppBundle\Controller\Admin\DataObjectController" cannot be found.

Is it really necessary to have a complete bundle with five files in order to overwrite an admin controller, or does the bundle also contain code specific for alexdrupals original question?

For your two exceptions: Before you add the configuration you have to enable & install the bundle. Otherwise symfony doesn’t know that this configuration is valid.

@scrummer No installation required, just enabling it, either by the pimcore extension config or directly in the AppKernel.

pimcore_mod is the right configuration path ;), did you enable the Bundle in the AppKernel?

Yes, I added

if (class_exists('\Pimcore\Bundle\PimcoreModBundle\PimcoreModBundle')) {
    $collection->addBundle(new \PimcoreModBundle\PimcoreModBundle);
}

to app/AppKernel.php. Saw a mistake there though, should not be \Pimcore of course but \PimcoreModBundle\PimcoreModBundle. After correcting, I instead get

(1/1) ServiceNotFoundException

You have requested a non-existent service "Pimcore\Bundle\AdminBundle\Controller\Admin\DataObjectController".

As you’re using a Pimcore-Bundle you have to enable/install the bundle in the Pimcore Backend (Tools > Extensions)

@scrummer: NO! you don’t have to, you can enable it like any Symfony Bundle and this Bundle doesn’t have an installer.