1

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 – 4k4 Feb 7 '18 at 13:39
1

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 Feb 8 '18 at 2:17
1

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

$form_state->getFormObject()->getEntity()->getPurchasedEntity()->product_id->getString();
-1

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, privacy policy and cookie policy

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