You are here

class DataComparison in Business Rules 2.x

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

Class DataComparison.

@package Drupal\business_rules\BusinessRulesCondition

Plugin annotation


@BusinessRulesCondition(
  id = "data_comparison",
  label = @Translation("Data Comparison"),
  group = @Translation("Entity"),
  description = @Translation("Compare entity field value against one value."),
  isContextDependent = TRUE,
  reactsOnIds = {},
  hasTargetEntity = TRUE,
  hasTargetBundle = TRUE,
  hasTargetField = TRUE,
)

Hierarchy

Expanded class hierarchy of DataComparison

File

src/Plugin/BusinessRulesCondition/DataComparison.php, line 31

Namespace

Drupal\business_rules\Plugin\BusinessRulesCondition
View source
class DataComparison extends BusinessRulesConditionPlugin {
  const CURRENT_DATA = 'current_data';
  const ORIGINAL_DATA = 'original_data';

  /**
   * {@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['data_to_compare'] = [
      '#type' => 'select',
      '#title' => t('Data to compare'),
      '#required' => TRUE,
      '#options' => [
        '' => t('- Select -'),
        self::CURRENT_DATA => t('Current value'),
        self::ORIGINAL_DATA => t('Original value'),
      ],
      '#description' => t('Current value is the value that is being saved.') . '<br>' . t('Original value is the previous saved value.'),
      '#default_value' => empty($condition
        ->getSettings('data_to_compare')) ? '' : $condition
        ->getSettings('data_to_compare'),
    ];
    $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_to_compare'] = [
      '#title' => t('Value to compare'),
      '#default_value' => $condition
        ->getSettings('value_to_compare'),
      '#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 comparison field 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}}'),
      '#states' => [
        'invisible' => [
          'select[name="operator"]' => [
            'value' => 'empty',
          ],
        ],
      ],
    ];
    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) {
    $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.'));
      }
      elseif ($operator != 'empty' && trim($value_to_compare) == '') {
        $form_state
          ->setErrorByName('value_to_compare', t('This operator require one value to be compared with.'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function process(ConditionInterface $condition, BusinessRulesEvent $event) {

    /** @var \Drupal\Core\Entity\Entity $entity */
    $field = $condition
      ->getSettings('field');
    $operator = $condition
      ->getSettings('operator');
    $variables = $event
      ->getArgument('variables');
    $values_to_compare = explode(chr(10), $condition
      ->getSettings('value_to_compare'));
    $values_to_compare = $this
      ->processInternalVariables($values_to_compare, $variables);
    $data_to_compare = $condition
      ->getSettings('data_to_compare');
    if ($data_to_compare == self::CURRENT_DATA) {
      $entity = $event
        ->getArgument('entity');
    }
    elseif ($data_to_compare == self::ORIGINAL_DATA) {
      $entity = $event
        ->getArgument('entity_unchanged');
    }
    $values = $entity
      ->get($field)
      ->getValue();
    if (count($values)) {
      foreach ($values as $value) {
        foreach ($values_to_compare as $compare) {
          return $this
            ->compare($value, $compare, $operator);
        }
      }
    }
    elseif ($operator == 'empty') {
      foreach ($values_to_compare as $compare) {
        return $this
          ->compare([
          'value' => '',
        ], $compare, $operator);
      }
    }
    return FALSE;
  }

  /**
   * Compare 2 values.
   *
   * @param mixed $value
   *   The First value.
   * @param mixed $compare
   *   The second value.
   * @param string $operator
   *   The operator.
   *
   * @return bool
   *   The comparison result.
   */
  private function compare($value, $compare, $operator) {
    if (isset($value['value'])) {
      $entity_value = strip_tags(strtolower(trim($value['value'])));
      $compare_value = strtolower(trim($compare));
      return $this->util
        ->criteriaMet($entity_value, $operator, $compare_value);
    }
    elseif (isset($value['target_id'])) {
      $entity_value = strip_tags(strtolower(trim($value['target_id'])));
      $compare_value = strtolower(trim($compare));
      return $this->util
        ->criteriaMet($entity_value, $operator, $compare_value);
    }
    else {
      return FALSE;
    }
  }

  /**
   * Process the item replacing the variables by it's values.
   *
   * @param mixed $values_to_compare
   *   The item to be replaced by the variable value.
   * @param \Drupal\business_rules\VariablesSet $variables
   *   Array of Variables provided by the event.
   *
   * @return mixed
   *   The processed content, replacing the variables tokens for it's values.
   */
  public function processInternalVariables($values_to_compare, VariablesSet $variables) {

    /** @var \Drupal\business_rules\VariableObject $variable */
    if ($variables
      ->count()) {
      foreach ($variables
        ->getVariables() as $variable) {
        if (!is_array($variable
          ->getValue())) {
          foreach ($values_to_compare as $key => $value_to_compare) {
            $value = $variable
              ->getValue();
            if (is_string($value) || is_numeric($value)) {
              $values_to_compare[$key] = str_replace('{{' . $variable
                ->getId() . '}}', $value, $value_to_compare);
            }
          }
        }
        else {
          foreach ($values_to_compare as $key => $value_to_compare) {
            foreach ($variable
              ->getValue() as $item) {

              // Add each value from variable array.
              $values_to_compare[] = $item;
            }

            // Unset the variable id from $values_to_compare.
            $variable_key = array_keys($values_to_compare, '{{' . $variable
              ->getId() . '}}');
            foreach ($variable_key as $key) {
              unset($values_to_compare[$key]);
            }
          }
        }
      }
    }
    return $values_to_compare;
  }

}

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
DataComparison::compare private function Compare 2 values.
DataComparison::CURRENT_DATA constant
DataComparison::getSettingsForm public function Return the form array. Overrides BusinessRulesItemPluginBase::getSettingsForm
DataComparison::ORIGINAL_DATA constant
DataComparison::process public function Process the condition. Overrides BusinessRulesConditionPlugin::process
DataComparison::processInternalVariables public function Process the item replacing the variables by it's values.
DataComparison::validateForm public function Performs the form validation. Overrides BusinessRulesItemPluginBase::validateForm
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.