You are here

public function ProductBundleItemInlineForm::entityForm in Commerce Product Bundle 8

Builds the entity form.

Parameters

array $entity_form: The entity form, containing the following basic properties:

  • #entity: The entity for the current entity form.
  • #op: The form operation. 'add' or 'edit'.
  • #form_mode: The form mode used to display the entity form.
  • #parents: Identifies the position of the entity form in the overall parent form, and identifies the location where the field values are placed within $form_state->getValues().

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

Overrides EntityInlineForm::entityForm

File

src/Form/ProductBundleItemInlineForm.php, line 48

Class

ProductBundleItemInlineForm
Defines the inline form for product bundle items.

Namespace

Drupal\commerce_product_bundle\Form

Code

public function entityForm(array $entity_form, FormStateInterface $form_state) {
  $entity_form = parent::entityForm($entity_form, $form_state);

  // This if/else make sure that the form can find the right position of
  // widget element.
  if (isset($entity_form['product']['widget'][0])) {
    $entity_form['product']['widget'][0]['target_id']['#ajax'] = [
      'callback' => [
        get_class($this),
        'variationsRefresh',
      ],
      'event' => 'autocompleteclose',
      'wrapper' => $entity_form['#ief_row_delta'] . 'product_variations_refresh',
    ];
  }
  else {
    $entity_form['product']['widget']['#ajax'] = [
      'callback' => [
        get_class($this),
        'variationsRefresh',
      ],
      'wrapper' => $entity_form['#ief_row_delta'] . 'product_variations_refresh',
    ];
  }
  $entity_form['variations']['#attributes']['id'] = $entity_form['#ief_row_delta'] . 'product_variations_refresh';
  $entity_form['variations']['widget']['#disabled'] = FALSE;

  /** @var \Drupal\commerce_product_bundle\Entity\ProductBundleItem $productBundleItem */
  $productBundleItem = $entity_form['#entity'];
  if ($productBundleItem
    ->hasProduct()) {
    $product = $productBundleItem
      ->getProduct();
    if (!empty($product)) {
      $productId = $productBundleItem
        ->getProductId();
    }
  }
  $userInput = $form_state
    ->getUserInput();
  if ($entity_form['#op'] == 'add') {
    if (isset($userInput['bundle_items']['form']['inline_entity_form']['product'])) {
      $productId = $userInput['bundle_items']['form']['inline_entity_form']['product'];
      if (isset($productId[0]['target_id'])) {
        $productId = EntityAutocomplete::extractEntityIdFromAutocompleteInput($productId[0]['target_id']);
      }
    }
  }
  elseif ($entity_form['#op'] == 'edit') {
    if (isset($userInput['bundle_items']['form']['inline_entity_form']['entities'])) {
      $entities = $userInput['bundle_items']['form']['inline_entity_form']['entities'];
      if (isset($entities[$entity_form['#ief_row_delta']]['form']['product'])) {
        $productId = $entities[$entity_form['#ief_row_delta']]['form']['product'];
        if (isset($productId[0]['target_id'])) {
          $productId = EntityAutocomplete::extractEntityIdFromAutocompleteInput($productId[0]['target_id']);
        }
      }
    }
  }
  $triggering_element = $form_state
    ->getTriggeringElement();

  // isset($triggering_element) is for edit or add page.
  // isset($userInput['_triggering_element_value']) is for changing product.
  // $userInput['op'] == 'Save') is for clicking product_bundle's form
  // save directly.
  if (!empty($productId) && (isset($triggering_element) || isset($userInput['_triggering_element_value']) || $userInput['op'] == 'Save')) {
    $entity_form['variations']['widget']['#options'] = $this
      ->variationsOptions($productId);
  }
  else {
    $entity_form['variations']['widget']['#disabled'] = TRUE;
  }
  return $entity_form;
}