Support hide_form like commerce add to cart form

Created on 11 July 2023, over 1 year ago
Updated 12 July 2023, over 1 year ago

Problem/Motivation

Drupal Commerce "Add to Cart" form allows setting "hide_form" parameter on form_state to hide the cart form.
This is helpful and an alternative to setting #access = FALSE; directly.

In commerce_cart's class AddToCartForm it's implemented like this:

// The widgets are allowed to signal that the form should be hidden
    // (because there's no purchasable entity to select, for example).
    if ($form_state->get('hide_form')) {
      $form['#access'] = FALSE;
    }

This is the complete AddToCartForm.php implementation:

/**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $original_form = $form;
    $form = parent::buildForm($form, $form_state);
    /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
    $order_item = $this->entity;
    // The widgets are allowed to signal that the form should be hidden
    // (because there's no purchasable entity to select, for example).
    if ($form_state->get('hide_form')) {
      $form['#access'] = FALSE;
    }
    else {
      $selected_variation = $form_state->get('selected_variation');
      // If the order item references a different variation than the one
      // currently selected, and the selected variation is supposed to use
      // a different order item type, rebuild the form.
      if ($selected_variation && $order_item->getPurchasedEntityId() != $selected_variation) {
        /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $selected_variation */
        $selected_variation = $this->entityTypeManager->getStorage('commerce_product_variation')->load($selected_variation);
        if ($selected_variation->getOrderItemTypeId() !== $order_item->bundle()) {
          /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
          $order_item_storage = $this->entityTypeManager->getStorage('commerce_order_item');
          $order_item = $order_item_storage->createFromPurchasableEntity($selected_variation);
          $this->prepareOrderItem($order_item);
          $this->setEntity($order_item);
          $form_display = EntityFormDisplay::collectRenderDisplay($order_item, $this->operation);
          $this->setFormDisplay($form_display, $form_state);
          $form = $original_form;
          $form = parent::buildForm($form, $form_state);
        }
      }
    }

    return $form;
  }

I think it would make sense to have the same functionality in commerce_variation_cart_form?
It doesn't seem to be inherited / called, at least nothing happens, if I alter the commerce_variation_cart_form and set the value accordingly. Only setting #access = FALSE; works.

Steps to reproduce

Proposed resolution

Implement the same logic as in commerce_cart

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024