Commit ceb402f9 authored by tonytheferg's avatar tonytheferg
Browse files

Issue #3257438 by cspitzlay, tonytheferg: Wrong default value for negation

parent cf2e4dd5
Loading
Loading
Loading
Loading
+16 −7
Original line number Diff line number Diff line
<?php declare(strict_types=1);
<?php

/**
 * @file
 * Contains hooks for Commerce Conditions Plus.
 */

declare(strict_types=1);

use Drupal\commerce_conditions_plus\Entity\ShippingMethod;
use Drupal\commerce_conditions_plus\Entity\PaymentGateway;
use Drupal\commerce_conditions_plus\Plugin\Field\FieldWidget\ConditionsTable as ConditionsTableWidget;
use Drupal\Core\Form\FormStateInterface;

@@ -9,7 +17,8 @@ use Drupal\Core\Form\FormStateInterface;
 */
function commerce_conditions_plus_element_plugin_alter(array &$definitions) {
  // @note cannot do this, as it breaks when embedded in promotion offers.
  // $definitions['commerce_conditions']['class'] = ConditionsTableElement::class;
  // $definitions['commerce_conditions']['class'] =
  // ConditionsTableElement::class;
}

/**
@@ -26,7 +35,7 @@ function commerce_conditions_plus_form_commerce_payment_gateway_form_alter(&$for
  $form['conditions']['#type'] = 'commerce_conditions_table';
  unset($form['conditionOperator']['#title_display']);
  $form['conditionOperator']['#title'] = 'Conditions table base logic';
  $form['conditionOperator']['#description'] = 'This sets the initial logic for the conditions table. Conditions that are nested under additional AND or OR operators will use the logic of their parent operator.';
  $form['conditionOperator']['#description'] = t('This sets the initial logic for the conditions table. Conditions that are nested under additional AND or OR operators will use the logic of their parent operator.');
  $form['conditionOperator']['#options']['AND'] = '<strong>AND</strong> (All conditions must pass)';
  $form['conditionOperator']['#options']['OR'] = '<strong>OR</strong> (Only one condition must pass)';

@@ -38,7 +47,7 @@ function commerce_conditions_plus_form_commerce_payment_gateway_form_alter(&$for
 */
function commerce_conditions_plus_form_commerce_shipping_method_form_alter(&$form, FormStateInterface $form_state) {
  $form['condition_operator']['widget']['#title'] = 'Conditions table base logic';
  $form['condition_operator']['widget']['#description'] = 'This sets the initial logic for the conditions table. Conditions that are nested under additional AND or OR operators will use the logic of their parent operator.';
  $form['condition_operator']['widget']['#description'] = t('This sets the initial logic for the conditions table. Conditions that are nested under additional AND or OR operators will use the logic of their parent operator.');
  $form['condition_operator']['widget']['#options']['AND'] = '<strong>AND</strong> (All conditions must pass)';
  $form['condition_operator']['widget']['#options']['OR'] = '<strong>OR</strong> (Only one condition must pass)';

@@ -101,11 +110,11 @@ function commerce_conditions_plus_config_schema_info_alter(&$definitions) {
 * Implements hook_entity_type_alter().
 */
function commerce_conditions_plus_entity_type_alter(array &$entity_types) {
  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  if (isset($entity_types['commerce_shipping_method'])) {
    $entity_types['commerce_shipping_method']->setClass(ShippingMethod::class);
  }
  if (isset($entity_types['commerce_payment_gateway'])) {
    $entity_types['commerce_payment_gateway']->setClass(\Drupal\commerce_conditions_plus\Entity\PaymentGateway::class);
    $entity_types['commerce_payment_gateway']->setClass(PaymentGateway::class);
  }
}
+21 −4
Original line number Diff line number Diff line
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Drupal\commerce_conditions_plus;

use Drupal\commerce_conditions_plus\Plugin\Commerce\Condition\AndOperator;
use Drupal\commerce_conditions_plus\Plugin\Commerce\Condition\OrOperator;
use Drupal\Core\Entity\EntityInterface;

/**
 * Provides an evaluator for conditions.
 */
final class ConditionsEvaluator {

  /**
   * The condition group executor.
   *
   * @param \Drupal\commerce\Plugin\Commerce\Condition\ConditionInterface[] $conditions
   * @param array<string, EntityInterface> $targets
   *   The conditions.
   * @param string $base_operator
   *   The base operator.
   * @param EntityInterface[] $targets
   *   The targets.
   *
   * @return bool
   *   True if the conditions pass, FALSE otherwise.
   */
  public function execute(array $conditions, string $base_operator, array $targets): bool {
    assert($base_operator === 'AND' || $base_operator === 'OR');
@@ -60,17 +71,23 @@ final class ConditionsEvaluator {
  }

  /**
   * The condition group evaluator.
   *
   * @param array $conditions
   *   The conditions.
   * @param string $operator
   *   The operator.
   * @param array $targets
   *   The targets.
   *
   * @return bool
   *   True if the conditions pass, FALSE otherwise.
   */
  private function evaluateConditionGroup(array $conditions, string $operator, array $targets): bool {
    assert($operator === 'AND' || $operator === 'OR');
    $boolean = !($operator === 'AND');
    foreach ($conditions as $condition) {
      $negated = $condition->getConfiguration()['negate_condition'] ?? false;
      $negated = $condition->getConfiguration()['negate_condition'] ?? FALSE;
      $target_entity_type = $condition->getEntityTypeId();
      $result = $condition->evaluate($targets[$target_entity_type]);
      $result = $negated ? !$result : $result;
+21 −5
Original line number Diff line number Diff line
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Drupal\commerce_conditions_plus\Element;

@@ -11,6 +13,8 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;

/**
 * Extends the base class for form element plugins.
 *
 * @FormElement("commerce_conditions_table")
 */
class ConditionsTable extends FormElement {
@@ -191,7 +195,10 @@ class ConditionsTable extends FormElement {
      ];
      $condition_form['configuration'] = [
        '#inline_form' => $inline_form,
        '#parents' => array_merge($element['#parents'], [$index, 'configuration']),
        '#parents' => array_merge(
          $element['#parents'],
          [$index, 'configuration']
        ),
      ];
      $condition_form['configuration'] = $inline_form->buildInlineForm($condition_form['configuration'], $form_state);

@@ -199,8 +206,11 @@ class ConditionsTable extends FormElement {
      $condition_form['negate_condition'] = [
        '#type' => 'checkbox',
        '#title' => t('Negate'),
        '#parents' => array_merge($element['#parents'], [$index, 'negate_condition']),
        '#default_value' => $condition['configuration']['negate_condition'] ?? $index,
        '#parents' => array_merge(
          $element['#parents'],
          [$index, 'negate_condition']
        ),
        '#default_value' => $condition['configuration']['negate_condition'] ?? 0,
        '#access' => !isset($condition_form['configuration']['form']['negate']),
      ];

@@ -300,6 +310,9 @@ class ConditionsTable extends FormElement {
    $form_state->setValueForElement($element, $values);
  }

  /**
   * {@inheritdoc}
   */
  public static function addNewCondition(&$form, FormStateInterface $form_state) {
    $element_parents = array_slice($form_state->getTriggeringElement()['#parents'], 0, -1);
    $values = $form_state->getValue($element_parents);
@@ -319,6 +332,9 @@ class ConditionsTable extends FormElement {
    $form_state->setRebuild();
  }

  /**
   * {@inheritdoc}
   */
  public static function removeConditionSubmit(&$form, FormStateInterface $form_state) {
    $value_index = $form_state->getTriggeringElement()['#value_index'];
    $conditions = $form_state->get('condition_plugins');
+9 −1
Original line number Diff line number Diff line
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Drupal\commerce_conditions_plus\Entity;

use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_payment\Entity\PaymentGateway as BasePaymentGateway;

/**
 * Extends the payment gateway entity class.
 */
class PaymentGateway extends BasePaymentGateway {

  /**
   * {@inheritdoc}
   */
  public function applies(OrderInterface $order) {
    $sut = \Drupal::getContainer()->get('commerce_conditions_plus.conditions_evaluator');
    return $sut->execute($this->getConditions(), $this->getConditionOperator(), [
+10 −2
Original line number Diff line number Diff line
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Drupal\commerce_conditions_plus\Entity;

use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\Entity\ShippingMethod as BaseShippingMethod;

/**
 * Extends the shipping method entity class.
 */
class ShippingMethod extends BaseShippingMethod {

  /**
   * {@inheritdoc}
   */
  public function applies(ShipmentInterface $shipment) {
    $sut = \Drupal::getContainer()->get('commerce_conditions_plus.conditions_evaluator');
    return $sut->execute($this->getConditions(), $this->getConditionOperator(), [
      'commerce_order' => $shipment->getOrder(),
      'commerce_shipment' => $shipment
      'commerce_shipment' => $shipment,
    ]);
  }

Loading