docs
docs copied to clipboard
Get the id_product param while edit a product inside the hook displayBackOfficeDisplayHeader
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 ?
ping @Shudrum @julienbourdeau
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
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). ;-)
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');
I'll reopen since it needs documentation.
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 ?
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.
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 !
You remember well, @prestamodule. The $idProduct will be null if you use the Request call.