You are here

public function PriceListItemInlineForm::priceForm in Commerce Pricelist 8

Handle the disable property of price.

Parameters

array $entity_form: The entity form.

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

Return value

array The entity form.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

1 call to PriceListItemInlineForm::priceForm()
PriceListItemInlineForm::entityForm in src/Form/PriceListItemInlineForm.php
Builds the entity form.

File

src/Form/PriceListItemInlineForm.php, line 138

Class

PriceListItemInlineForm
Defines the inline form for product variations.

Namespace

Drupal\commerce_pricelist\Form

Code

public function priceForm(array $entity_form, FormStateInterface $form_state) {

  /** @var \Drupal\commerce_pricelist\Entity\PriceListItem $entity */
  $entity = $entity_form['#entity'];
  $entity_form['price']['widget']['#disabled'] = FALSE;
  if ($entity
    ->hasPurchasedEntity()) {
    $purchasedEntity = $entity
      ->getPurchasedEntity();
  }
  $userInput = $form_state
    ->getUserInput();

  // When the operation is 'add'.
  if ($entity_form['#op'] == 'add') {
    if (isset($userInput['field_price_list_item']['form']['inline_entity_form']['purchased_entity'])) {
      $purchasedEntityId = $userInput['field_price_list_item']['form']['inline_entity_form']['purchased_entity'];
    }
    if (isset($purchasedEntityId[0]['target_id'])) {
      $purchasedEntityId = EntityAutocomplete::extractEntityIdFromAutocompleteInput($purchasedEntityId[0]['target_id']);
    }
  }
  else {
    if (isset($userInput['field_price_list_item']['form']['inline_entity_form']['entities'])) {
      $entities = $userInput['field_price_list_item']['form']['inline_entity_form']['entities'];
    }
    if (isset($entities[$entity_form['#ief_row_delta']]['form']['purchased_entity'])) {
      $purchasedEntityId = $entities[$entity_form['#ief_row_delta']]['form']['purchased_entity'];

      // If the widget type is entity_auto_complete, we can just get entity id
      // by using regular, because the entity_id can't be get directly in
      // inline_entity_form.
      if (isset($purchasedEntityId[0]['target_id'])) {
        $purchasedEntityId = EntityAutocomplete::extractEntityIdFromAutocompleteInput($purchasedEntityId[0]['target_id']);
      }
    }
  }
  if (isset($purchasedEntityId)) {
    $purchasedEntityTargetType = $entity
      ->getFieldDefinition('purchased_entity')
      ->getSetting('target_type');
    $purchasedEntity = $this->entityTypeManager
      ->getStorage($purchasedEntityTargetType)
      ->load($purchasedEntityId);
  }
  if (!empty($purchasedEntity)) {
    $price = $purchasedEntity
      ->getPrice();
    if (!$price) {
      $entity_form['price']['widget']['#disabled'] = TRUE;
    }
  }
  return $entity_form;
}