You are here

protected function ProductBundleItemsWidget::selectVariationFromUserInput in Commerce Product Bundle 8

Selects a product variation from user input.

If there's no user input (form viewed for the first time), the default variations are returned.

Parameters

\Drupal\commerce_product\Entity\ProductVariationInterface[] $variations: An array of product variations.

array $user_input: The user input.

Return value

\Drupal\commerce_product\Entity\ProductVariationInterface The selected variation.

1 call to ProductBundleItemsWidget::selectVariationFromUserInput()
ProductBundleItemsWidget::getBundleItemForm in src/Plugin/Field/FieldWidget/ProductBundleItemsWidget.php
Gets the child form element for a given bundle item.

File

src/Plugin/Field/FieldWidget/ProductBundleItemsWidget.php, line 285

Class

ProductBundleItemsWidget
Plugin implementation of the 'commerce_product_bundle_items' widget.

Namespace

Drupal\commerce_product_bundle\Plugin\Field\FieldWidget

Code

protected function selectVariationFromUserInput(array $variations, array $user_input) {
  $current_variation = reset($variations);
  if (!empty($user_input)) {
    $attributes = $user_input['attributes'];
    foreach ($variations as $variation) {
      $match = TRUE;
      foreach ($attributes as $field_name => $value) {
        if ($variation
          ->getAttributeValueId($field_name) != $value) {
          $match = FALSE;
        }
      }
      if ($match) {
        $current_variation = $variation;
        break;
      }
    }
  }
  return $current_variation;
}