You are here

public static function ProductBundleItemInlineForm::variationsRefresh in Commerce Product Bundle 8

Product field ajax callback.

Parameters

array $form: Inline entity form.

\Drupal\Core\Form\FormStateInterface $form_state: Inline entity form_state.

Return value

mixed Form element

File

src/Form/ProductBundleItemInlineForm.php, line 145

Class

ProductBundleItemInlineForm
Defines the inline form for product bundle items.

Namespace

Drupal\commerce_product_bundle\Form

Code

public static function variationsRefresh(array $form, FormStateInterface $form_state) {
  $element = [];
  $triggering_element = $form_state
    ->getTriggeringElement();

  // Remove the action and the actions container.
  $array_parents = array_slice($triggering_element['#array_parents'], 0, -2);
  while (!(isset($element['#type']) && $element['#type'] == 'inline_entity_form')) {
    $element = NestedArray::getValue($form, $array_parents);
    array_pop($array_parents);
  }

  // Get the origin variations form.
  $variationsForm = $element['variations'];
  $bundleItems = $form_state
    ->getValue('bundle_items');
  if ($element['#op'] == 'add') {
    if (isset($bundleItems['form']['inline_entity_form']['product'][0]['target_id'])) {
      $productId = $bundleItems['form']['inline_entity_form']['product'][0]['target_id'];
    }
  }
  elseif ($element['#op'] == 'edit') {
    $entities = $bundleItems['form']['inline_entity_form']['entities'];
    if (isset($entities[$element['#ief_row_delta']]['form']['product'][0]['target_id'])) {
      $productId = $entities[$element['#ief_row_delta']]['form']['product'][0]['target_id'];
    }
  }
  if (!empty($productId)) {
    $variationsForm['widget']['#options'] = ProductBundleItemInlineForm::variationsOptions($productId);
  }
  return $variationsForm;
}