You are here

function uc_attribute_uc_form_alter in Ubercart 8.4

Same name and namespace in other branches
  1. 6.2 uc_attribute/uc_attribute.module \uc_attribute_uc_form_alter()
  2. 7.3 uc_attribute/uc_attribute.module \uc_attribute_uc_form_alter()

Implements hook_uc_form_alter().

Attaches option selectors to the form with the "Add to Cart" button.

This function also handles selecting attributes for products added to orders manually.

File

uc_attribute/uc_attribute.module, line 137
Ubercart Attribute module.

Code

function uc_attribute_uc_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (strpos($form_id, 'add_to_cart_form') || $form_id == 'uc_order_add_product_form') {
    $use_ajax = strpos($form_id, 'add_to_cart_form') && \Drupal::config('uc_product.settings')
      ->get('update_node_view');
    $node =& $form['node']['#value'];
    $id = $form_id . '-' . $node
      ->id() . '-attributes';

    // If the node has a product list, add attributes to them.
    if (isset($form['products']) || isset($form['sub_products'])) {
      if (isset($form['products'])) {
        $element =& $form['products'];
      }
      else {
        $element =& $form['sub_products'];
      }
      foreach (Element::children($element) as $key) {
        $element[$key]['attributes'] = _uc_attribute_alter_form($id . '-' . $key, $node->products[$key], $use_ajax);
        if (is_array($element[$key]['attributes'])) {
          $element[$key]['attributes']['#tree'] = TRUE;
          $element[$key]['#type'] = 'details';
        }
      }
    }
    else {
      $form['attributes'] = _uc_attribute_alter_form($id, $node, $use_ajax);
      if (is_array($form['attributes'])) {
        $form['attributes']['#tree'] = TRUE;
        $form['attributes']['#weight'] = -1;
      }
    }
  }
}