modules/acm_sku/src/Plugin/AcquiaCommerce/SKUType/Configurable.php
public static function configurableAjaxCallback(array $form, FormStateInterface $form_state) {
    $dynamic_parts = &$form['ajax'];

    $configurables = $form_state->getValue('configurables');

    /** @var \Drupal\acm_sku\CartFormHelper $helper */
    $helper = \Drupal::service('acm_sku.cart_form_helper');

    $tree = $form_state->get('tree');
    $tree_pointer = &$tree['options'];

    foreach ($configurables as $key => $value) {
      if (empty($value)) {
        continue;
      }

      // Move the tree pointer if the selection is valid.
      if (isset($tree_pointer["$key:$value"])) {
        $tree_pointer = &$tree_pointer["$key:$value"];
      }
    }

    if ($tree_pointer instanceof SKU) {
      $view_builder = \Drupal::entityTypeManager()->getViewBuilder('acm_sku');

      $view = $view_builder->view($tree_pointer);

      // Block add to cart render because Form API won't allow AJAX Formception.
      $view['#no_add_to_cart'] = TRUE;

      $dynamic_parts['add_to_cart'] = [
        'entity_render' => ['#markup' => render($view)],
      ];
    }
    else {
      $available_config = $tree_pointer['#available_config'];

      foreach ($available_config as $key => $config) {
        $options = [
          '' => $dynamic_parts['configurables']['color']['#options'][''],
        ];

        foreach ($config['values'] as $value) {
          $options[$value['value_id']] = $value['label'];
        }

        // Sort config options before pushing them to the select list based on
        // the config.
        if ($helper->isAttributeSortable($key)) {
          // Make sure the first element in the list is the empty option.
          $sorted_options = [
            '' => $dynamic_parts['configurables'][$key]['#options'][''],
          ];
          $sorted_options += self::sortConfigOptions($options, $key);
        }
        else {
          // Use this in case the attribute is not sortable as per the config.
          $sorted_options = $options;
        }

        $dynamic_parts['configurables'][$key]['#options'] = $sorted_options;
      }
    }

    return $dynamic_parts;
  }
public static function configurableAjaxCallback(array $form, FormStateInterface $form_state) {
    $dynamic_parts = &$form['ajax'];

    $configurables = $form_state->getValue('configurables');

    /** @var \Drupal\acm_sku\CartFormHelper $helper */
    $helper = \Drupal::service('acm_sku.cart_form_helper');

    $tree = $form_state->get('tree');
    $tree_pointer = &$tree['options'];

    foreach ($configurables as $key => $value) {
      if (empty($value)) {
        continue;
      }

      // Move the tree pointer if the selection is valid.
      if (isset($tree_pointer["$key:$value"])) {
        $tree_pointer = &$tree_pointer["$key:$value"];
      }
    }

    if ($tree_pointer instanceof SKU) {
      $view_builder = \Drupal::entityTypeManager()->getViewBuilder('acm_sku');

      $view = $view_builder->view($tree_pointer);

      // Block add to cart render because Form API won't allow AJAX Formception.
      $view['#no_add_to_cart'] = TRUE;

      $dynamic_parts['add_to_cart'] = [
        'entity_render' => ['#markup' => render($view)],
      ];
    }
    else {
      $available_config = $tree_pointer['#available_config'];

      foreach ($available_config as $key => $config) {
        $options = [
          '' => $dynamic_parts['configurables']['color']['#options'][''],
        ];

        foreach ($config['values'] as $value) {
          $options[$value['value_id']] = $value['label'];
        }

        // Sort config options before pushing them to the select list based on
        // the config.
        if ($helper->isAttributeSortable($key)) {
          // Make sure the first element in the list is the empty option.
          $sorted_options = [
            '' => $dynamic_parts['configurables'][$key]['#options'][''],
          ];
          $sorted_options += self::sortConfigOptions($options, $key);
        }
        else {
          // Use this in case the attribute is not sortable as per the config.
          $sorted_options = $options;
        }

        $dynamic_parts['configurables'][$key]['#options'] = $sorted_options;
      }
    }

    return $dynamic_parts;
  }
public static function configurableAjaxCallback ( array $ form , FormStateInterface $ form_state ) { $ dynamic_parts = & $ form [ 'ajax' ] ; $ configurables = $ form_state -> getValue ( 'configurables' ) ; /** @var \Drupal\acm_sku\CartFormHelper $helper */ $ helper = \ Drupal :: service ( 'acm_sku.cart_form_helper' ) ; $ tree = $ form_state -> get ( 'tree' ) ; $ tree_pointer = & $ tree [ 'options' ] ; foreach ( $ configurables as $ key => $ value ) { if ( empty ( $ value ) ) { continue ; } // Move the tree pointer if the selection is valid. if ( isset ( $ tree_pointer [ "$key:$value" ] ) ) { $ tree_pointer = & $ tree_pointer [ "$key:$value" ] ; } } if ( $ tree_pointer instanceof SKU ) { $ view_builder = \ Drupal :: entityTypeManager ( ) -> getViewBuilder ( 'acm_sku' ) ; $ view = $ view_builder -> view ( $ tree_pointer ) ; // Block add to cart render because Form API won't allow AJAX Formception. $ view [ '#no_add_to_cart' ] = TRUE ; $ dynamic_parts [ 'add_to_cart' ] = [ 'entity_render' => [ '#markup' => render ( $ view ) ] , ] ; } else { $ available_config = $ tree_pointer [ '#available_config' ] ; foreach ( $ available_config as $ key => $ config ) { $ options = [ '' => $ dynamic_parts [ 'configurables' ] [ 'color' ] [ '#options' ] [ '' ] , ] ; foreach ( $ config [ 'values' ] as $ value ) { $ options [ $ value [ 'value_id' ] ] = $ value [ 'label' ] ; } // Sort config options before pushing them to the select list based on // the config. if ( $ helper -> isAttributeSortable ( $ key ) ) { // Make sure the first element in the list is the empty option. $ sorted_options = [ '' => $ dynamic_parts [ 'configurables' ] [ $ key ] [ '#options' ] [ '' ] , ] ; $ sorted_options += self :: sortConfigOptions ( $ options , $ key ) ; } else { // Use this in case the attribute is not sortable as per the config. $ sorted_options = $ options ; } $ dynamic_parts [ 'configurables' ] [ $ key ] [ '#options' ] = $ sorted_options ; } } return $ dynamic_parts ; }
Updates the form based on selections.

@param array $form
Array of form.
@param \Drupal\Core\Form\FormStateInterface $form_state
Values of form.

@return array
Array with dynamic parts of the form.
Updates the form based on selections .