/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) { $query = \Drupal::request()->query->all(); $form['#action'] = Url::fromRoute('<current>')->setOptions(['query' => $query])->toString(); $form['nid'] = array('#type' => 'value', '#value' => $node->id()); $form['qty'] = array('#type' => 'value', '#value' => 1); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Add to cart'), '#id' => 'edit-submit-' . $node->id()); uc_form_alter($form, $form_state, $this->getFormId()); return $form; }
/** * Sets the quantity and attributes of a product added to the order. */ protected function addProductForm($form, FormStateInterface $form_state, $order, $node) { $data = array(); if ($form_state->hasValue(['product_controls', 'qty'])) { $data += \Drupal::moduleHandler()->invokeAll('uc_add_to_cart_data', array($form_state->getValue('product_controls'))); } if (!empty($node->data) && is_array($node->data)) { $data += $node->data; } $node = uc_product_load_variant(intval($form_state->getValue(['product_controls', 'nid'])), $data); $form['title'] = array('#markup' => '<h3>' . $node->label() . '</h3>'); $form['nid'] = array('#type' => 'hidden', '#value' => $node->id()); $form['qty'] = array('#type' => 'uc_quantity', '#title' => $this->t('Quantity'), '#default_value' => 1); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Add to order'), '#submit' => array(array($this, 'addProductSubmit')), '#ajax' => array('callback' => array($this, 'ajaxCallback'), 'wrapper' => 'product-controls')); $form['actions']['cancel'] = array('#type' => 'submit', '#value' => $this->t('Cancel'), '#submit' => array(array($this, 'productSelectSearch')), '#ajax' => array('callback' => array($this, 'ajaxCallback'), 'wrapper' => 'product-controls'), '#limit_validation_errors' => array()); $form['node'] = array('#type' => 'value', '#value' => $node); uc_form_alter($form, $form_state, __FUNCTION__); return $form; }