You are here

public function AddToCartForm::buildEntity in Commerce Core 8.2

Builds an updated entity object based upon the submitted form values.

For building the updated entity object the form's entity is cloned and the submitted form values are copied to entity properties. The form's entity remains unchanged.

Parameters

array $form: A nested array form elements comprising the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

\Drupal\Core\Entity\EntityInterface An updated copy of the form's entity object.

Overrides ContentEntityForm::buildEntity

See also

\Drupal\Core\Entity\EntityFormInterface::getEntity()

File

modules/cart/src/Form/AddToCartForm.php, line 221

Class

AddToCartForm
Provides the order item add to cart form.

Namespace

Drupal\commerce_cart\Form

Code

public function buildEntity(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $entity */
  $entity = parent::buildEntity($form, $form_state);

  // Now that the purchased entity is set, populate the title and price.
  $purchased_entity = $entity
    ->getPurchasedEntity();
  $entity
    ->setTitle($purchased_entity
    ->getOrderItemTitle());
  if (!$entity
    ->isUnitPriceOverridden()) {
    $store = $this
      ->selectStore($purchased_entity);
    $context = new Context($this->currentUser, $store);
    $resolved_price = $this->chainPriceResolver
      ->resolve($purchased_entity, $entity
      ->getQuantity(), $context);
    $entity
      ->setUnitPrice($resolved_price);
  }
  $order_type_id = $this->orderTypeResolver
    ->resolve($entity);
  $store = $this
    ->selectStore($purchased_entity);
  $cart = $this->cartProvider
    ->getCart($order_type_id, $store);
  if ($cart) {
    $entity
      ->set('order_id', $cart
      ->id());
  }
  return $entity;
}