I am currently working on drupal 8 commerce. I need to form alter some of the expose fields but need to get the product id first so I can get my condition on the product fields. I used this kint($form_state) on my form alter to show the attached image. But when I try use this $form['storage'] im having wsod. Thanks

enter image description here

  • $form_state is an object and you have to use methods to get protected properties, for example $form_state->getFormObject()->getEntity()->id() if this is an entity form for product
    – 4uk4
    Commented Feb 7, 2018 at 13:39

3 Answers 3


If You alter 'Add to card form', try this code


Right, you can't ever access "protected" items of the data, which you can see is shown in the kint output. As 4x4 said, you'll want to look at the "Available Methods" tab, and use one of those to extract the data you need, so you'd want something like:

$product_id = $form_state->getFormObject()->getEntity()->getValue('product_id');

I hope this gets you going in the right direction for getting this data you need and understanding a major difference of D7-D8

  • I got wsod when I added the code you mention. Thanks
    – Oltus West
    Commented Feb 8, 2018 at 2:17

I figure it out. Since it is a product page I used this code instead $product = \Drupal::routeMatch()->getParameter('commerce_product')) && is_object($product);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.