commerce_conditions_plus-1.0.x-dev/commerce_conditions_plus.module
commerce_conditions_plus.module
<?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;
/**
* Implements hook_element_plugin_alter().
*/
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;
}
/**
* Implements hook_field_widget_info_alter().
*/
function commerce_conditions_plus_field_widget_info_alter(array &$info) {
$info['commerce_conditions']['class'] = ConditionsTableWidget::class;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function commerce_conditions_plus_form_commerce_payment_gateway_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['conditions']['#type'] = 'commerce_conditions_table';
unset($form['conditionOperator']['#title_display']);
$form['conditionOperator']['#title'] = 'Conditions table base logic';
$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)';
$form['#after_build'][] = 'commerce_conditions_plus_adjust_conditions_weight';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
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'] = 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)';
$form['#after_build'][] = 'commerce_conditions_plus_adjust_conditions_weight';
}
/**
* Ensures condition operator is before conditions table.
*
* Swaps the #weight values between the two elements to prevent disrupting any
* other form element weights.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return array
* The form.
*/
function commerce_conditions_plus_adjust_conditions_weight(array $form, FormStateInterface $form_state) {
$condition_operator_key = 'condition_operator';
if (isset($form['conditionOperator'])) {
$condition_operator_key = 'conditionOperator';
}
$conditions_weight = $form['conditions']['#weight'];
$conditions_operator_weight = $form[$condition_operator_key]['#weight'];
$form[$condition_operator_key]['#weight'] = $conditions_weight;
$form['conditions']['#weight'] = $conditions_operator_weight;
return $form;
}
/**
* Implements hook_config_schema_info_alter().
*/
function commerce_conditions_plus_config_schema_info_alter(&$definitions) {
$definitions['commerce_condition_configuration']['mapping'] = [
'parent' => [
'type' => 'string',
'label' => 'Parent condition',
],
'depth' => [
'type' => 'integer',
'label' => 'Depth',
],
'weight' => [
'type' => 'integer',
'label' => 'Weight',
],
// We name this `negate_condition` to handle plugins which also have
// their own negate functionality to prevent conflicts.
'negate_condition' => [
'type' => 'boolean',
'label' => 'Negate',
],
];
}
/**
* Implements hook_entity_type_alter().
*/
function commerce_conditions_plus_entity_type_alter(array &$entity_types) {
/** @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(PaymentGateway::class);
}
}
