-1

I want to alter a product display using hook_form_alter as below:

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function HOOK_form_commerce_order_item_add_to_cart_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  $product = $form_state->getFormObject()->getEntity()->getPurchasedEntity();

// THIS WHERE I want something like 
  if($product->viewMode() == 'compact') {
     // do something...
  }
}

Any help would be appriciated.

1 Answer 1

0

Never mind figured out myself,

I found the answer to get the view_mode you need to access storage of form stage using following code.

$form_state->getStorage()['view_mode'];

This will return string based on which view mode the form is attached to.

i.e, "defualt" or "compact" etc...

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.