You are here

protected function BuyXGetY::getAutoAddStatesVisibility in Commerce Core 8.2

Gets the #states visibility array for the 'auto_add' form element.

Return value

array An array of visibility options for a form element's #state property.

1 call to BuyXGetY::getAutoAddStatesVisibility()
BuyXGetY::buildConfigurationForm in modules/promotion/src/Plugin/Commerce/PromotionOffer/BuyXGetY.php
Form constructor.

File

modules/promotion/src/Plugin/Commerce/PromotionOffer/BuyXGetY.php, line 648

Class

BuyXGetY
Provides the "Buy X Get Y" offer for orders.

Namespace

Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer

Code

protected function getAutoAddStatesVisibility() {

  // The 'auto_add' form element has to be shown if _any_ condition that
  // provides a purchasable entity is enabled for the 'get' conditions. This
  // means we need to construct a list of OR statements for #states, which
  // looks like this:
  // @code
  // '#states' => [
  //   'visible' => [
  //     [':input[name="some_element"]' => ['checked' => TRUE]],
  //     'or',
  //     [':input[name="another_element"]' => ['checked' => TRUE]],
  //     ...
  //   ],
  // ],
  // @endcode
  $conditions = $this
    ->getPurchasableEntityConditions();
  $states_visibility = array_map(function ($value) {
    return [
      ':input[name="offer[0][target_plugin_configuration][order_buy_x_get_y][get][conditions][products][' . $value . '][enable]"]' => [
        'checked' => TRUE,
      ],
    ];
  }, array_keys($conditions));
  for ($i = 0; $i < count($conditions) - 1; $i++) {
    array_splice($states_visibility, $i + 1, 0, 'or');
  }
  return $states_visibility;
}