You are here

class VariableDataComparison in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesCondition/VariableDataComparison.php \Drupal\business_rules\Plugin\BusinessRulesCondition\VariableDataComparison

Class VariableDataComparison.

@package Drupal\business_rules\Plugin\BusinessRulesCondition

Plugin annotation


@BusinessRulesCondition(
  id = "variable_data_comparison",
  label = @Translation("Variable Data Comparison"),
  group = @Translation("Variable"),
  description = @Translation("Compare two variable values."),
  isContextDependent = FALSE,
)

Hierarchy

Expanded class hierarchy of VariableDataComparison

File

src/Plugin/BusinessRulesCondition/VariableDataComparison.php, line 24

Namespace

Drupal\business_rules\Plugin\BusinessRulesCondition
View source
class VariableDataComparison extends BusinessRulesConditionPlugin {

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $condition) {

    // Only show settings form if the item is already saved.
    if ($condition
      ->isNew()) {
      return [];
    }
    $settings['value_1'] = [
      '#type' => 'textfield',
      '#title' => t('Value 1'),
      '#required' => TRUE,
      '#description' => t('The value 1 to compare the value.
      <br>To use variables, just type the variable machine name as {{variable_id}}. If the variable is an Entity Variable, you can access the fields values using {{variable_id->field}}'),
      '#default_value' => $condition
        ->getSettings('value_1'),
    ];
    $settings['operator'] = [
      '#type' => 'select',
      '#required' => TRUE,
      '#title' => t('Operator'),
      '#description' => t('The operation to be performed on this data comparison.'),
      '#default_value' => $condition
        ->getSettings('operator'),
      '#options' => $this->util
        ->getCriteriaMetOperatorsOptions(),
    ];
    $settings['value_2'] = [
      '#title' => t('Value 2'),
      '#default_value' => $condition
        ->getSettings('value_2'),
      '#required' => TRUE,
      '#type' => 'textarea',
      '#description' => t('For multiple values comparison, include one per line.
        It will return TRUE if at least one element was found.
        <br>If the first value in this comparison is a list of values, enter the element(s) id(s)
        <br>Enter the element(s) id(s), one per line.
        <br>To use variables, just type the variable machine name as {{variable_id}}. If the variable is an Entity Variable, you can access the fields values using {{variable_id->field}}'),
      '#prefix' => '<div id="value_to_compare-wrapper">',
      '#suffix' => '</div>',
    ];
    return $settings;
  }

  /**
   * Performs the form validation.
   *
   * @param array $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\Core\Entity\EntityInterface $item */
    $item = $form_state
      ->getFormObject()
      ->getEntity();
    if (!$item
      ->isNew()) {
      $textarea_fields = [
        'contains',
        '==',
        'starts_with',
        'ends_with',
        '!=',
      ];
      $value_to_compare = $form_state
        ->getValue('value_to_compare');
      $operator = $form_state
        ->getValue('operator');
      if (!in_array($operator, $textarea_fields) && stristr($value_to_compare, chr(10))) {
        $form_state
          ->setErrorByName('value_to_compare', t('This operator only allows one value in one line. Please remove the additional lines.'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function process(ConditionInterface $condition, BusinessRulesEvent $event) {
    $value_1 = $condition
      ->getSettings('value_1');
    $value_2 = $condition
      ->getSettings('value_2');
    $operator = $condition
      ->getSettings('operator');
    $event_variables = $event
      ->getArgument('variables');

    // Process values variables.
    $value_1 = $this
      ->processVariables($value_1, $event_variables);
    $value_2 = $this
      ->processVariables($value_2, $event_variables);

    // Remove tags, trim and to lowercase.
    $value_1 = strip_tags(strtolower(trim($value_1)));
    $value_2 = strip_tags(strtolower(trim($value_2)));
    return $this->util
      ->criteriaMet($value_1, $operator, $value_2);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BusinessRulesItemPluginBase::$processor protected property The business rules processor.
BusinessRulesItemPluginBase::$util protected property The business rules util.
BusinessRulesItemPluginBase::buildForm public function Form constructor. Overrides BusinessRulesItemPluginInterface::buildForm 11
BusinessRulesItemPluginBase::getDescription public function Provide a description of the item. Overrides BusinessRulesItemPluginInterface::getDescription
BusinessRulesItemPluginBase::getEditUrl public function Get the redirect url for the item edit-form route. Overrides BusinessRulesItemPluginInterface::getEditUrl
BusinessRulesItemPluginBase::getGroup public function Provide the group of the item. Overrides BusinessRulesItemPluginInterface::getGroup
BusinessRulesItemPluginBase::getRedirectUrl public function Get the redirect url for the item collection route. Overrides BusinessRulesItemPluginInterface::getRedirectUrl
BusinessRulesItemPluginBase::getVariables public function Return a variable set with all used variables on the item. Overrides BusinessRulesItemPluginInterface::getVariables 9
BusinessRulesItemPluginBase::pregMatch public function Extract the variables from the plugin settings. Overrides BusinessRulesItemPluginInterface::pregMatch
BusinessRulesItemPluginBase::processSettings public function Process the item settings before it's saved. Overrides BusinessRulesItemPluginInterface::processSettings 19
BusinessRulesItemPluginBase::processTokenArraySetting private function Helper function to process tokens if the setting is an array.
BusinessRulesItemPluginBase::processTokens public function Process the tokens on the settings property for the item. Overrides BusinessRulesItemPluginInterface::processTokens
BusinessRulesItemPluginBase::processVariables public function Process the item replacing the variables by it's values. Overrides BusinessRulesItemPluginInterface::processVariables 1
BusinessRulesItemPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 11
BusinessRulesItemPluginInterface::VARIABLE_REGEX constant
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
VariableDataComparison::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
VariableDataComparison::process public function Process the condition. Overrides BusinessRulesConditionPlugin::process
VariableDataComparison::validateForm public function Performs the form validation. Overrides BusinessRulesItemPluginBase::validateForm