docs icon indicating copy to clipboard operation
docs copied to clipboard

Get the id_product param while edit a product inside the hook displayBackOfficeDisplayHeader

Open PrestaEdit opened this issue 7 years ago • 9 comments

Hi there !

Can you tell me how I can have the "id_product" inside the hook "displayBackOfficeDisplayHeader" when I edit a product on the PrestaShop 1.7 version, maybe ?

PrestaEdit avatar Nov 18 '16 13:11 PrestaEdit

ping @Shudrum @julienbourdeau

PrestaEdit avatar Nov 21 '16 10:11 PrestaEdit

Hi Jonathan,

I think in a module, a simple Tools::getValue('id') should do the job just fine.

We considered $request = Request::createFromGlobals() or the snippet you sent me by email but we think there is no point. If there is no container, using the good old way is fine :)

What do you think?

Julien

julienbourdeau avatar Nov 30 '16 09:11 julienbourdeau

It was I expected... But isn't work, for sure (it's why I have created this issue, because the old way doesn't seems to work there). ;-)

PrestaEdit avatar Nov 30 '16 09:11 PrestaEdit

Sorry we got a bit confused! Indeed the id is extracted from the URL by the routing component but it isn't in $_GET nor $_POST. It's an attribute in the request.

Hence your snippet was the best way to achieve that, well done! :)

For reference:

global $kernel;
$request = $kernel->getContainer()->get('request');
$idProduct = $request->get('id');

julienbourdeau avatar Dec 01 '16 08:12 julienbourdeau

I'll reopen since it needs documentation.

julienbourdeau avatar Dec 01 '16 08:12 julienbourdeau

Hi @julienbourdeau

We are not supposed to use global into modules as it's forbidden into the validator. By the way, I would prefer (and it would keep BC) keep using Tools::getValue().

Tools::getValue method would use the kernel to retrieve the request and return the needed data when we are on a symfony controller.

What do you think ?

prestamodule avatar Dec 01 '16 08:12 prestamodule

To "avoid" globals, you can use $request = Request::createFromGlobals() instead.

The very best way would be making the request available in hooks! We'll discuss this today and we'll let you know.

Thanks for your detailed feedback.

julienbourdeau avatar Dec 01 '16 09:12 julienbourdeau

If i remember well, $request = Request::createFromGlobals() does not include parameters (id of product). About making request available in hooks, it's fine too, but if we can keep some BC using Tools::getValue()... That is fine too !

prestamodule avatar Dec 01 '16 11:12 prestamodule

You remember well, @prestamodule. The $idProduct will be null if you use the Request call.

PrestaEdit avatar Dec 01 '16 12:12 PrestaEdit