class TextComparison

Provides a 'Text comparison' condition.

Plugin annotation


@Condition(
  id = "rules_text_comparison",
  label = @Translation("Text comparison"),
  category = @Translation("Data"),
  context_definitions = {
    "text" = @ContextDefinition("string",
      label = @Translation("Text"),
      description = @Translation("Specifies the text data to evaluate."),
      assignment_restriction = "selector"
    ),
    "operator" = @ContextDefinition("string",
      label = @Translation("Operator"),
      description = @Translation("The comparison operator. One of 'contains', 'starts', 'ends', or 'regex'. Defaults to 'contains'."),
      options_provider = "\Drupal\rules\TypedData\Options\ComparisonOperatorTextOptions",
      assignment_restriction = "input",
      default_value = "contains",
      required = FALSE
    ),
    "match" = @ContextDefinition("string",
      label = @Translation("Matching text"),
      description = @Translation("A string (or pattern in the case of regex) to search for in the given text data.")
    ),
  }
)

Hierarchy

Expanded class hierarchy of TextComparison

File

src/Plugin/Condition/TextComparison.php, line 35

Namespace

Drupal\rules\Plugin\Condition
View source
class TextComparison extends RulesConditionBase {
  
  /**
   * Evaluate the text comparison.
   *
   * @param string $text
   *   The supplied text string.
   * @param string $operator
   *   Text comparison operator. One of:
   *   - contains: (default) Evaluate if $text contains $match.
   *   - starts: Evaluate if $text starts with $match.
   *   - ends: Evaluate if $text ends with $match.
   *   - regex: Evaluate if a regular expression in $match matches $text.
   *   Values that do not match one of these operators default to "contains".
   * @param string $match
   *   The string to be compared against $text.
   *
   * @return bool
   *   The evaluation of the condition.
   */
  protected function doEvaluate($text, $operator, $match) {
    $operator = $operator ? $operator : 'contains';
    switch ($operator) {
      case 'starts':
        return strpos($text, $match) === 0;
      case 'ends':
        return strrpos($text, $match) === strlen($text) - strlen($match);
      case 'regex':
        return (bool) preg_match('/' . str_replace('/', '\\/', $match) . '/', $text);
      case 'contains':
      default:
        // Default operator "contains".
        return strpos($text, $match) !== FALSE;
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConfigurationAccessControlTrait::checkConfigurationAccess public function Checks configuration permission.
ContextProviderTrait::$providedContext protected property The data objects that are provided by this plugin.
ContextProviderTrait::getProvidedContext public function
ContextProviderTrait::getProvidedContextDefinition public function
ContextProviderTrait::getProvidedContextDefinitions public function
ContextProviderTrait::setProvidedValue public function
ContextProviderTrait::upcastEntityId public function Upcasts an entity id to a full entity object.
ExecutablePluginTrait::getLabelValue protected function Get the translated label from the plugin definition.
ExecutablePluginTrait::summary public function Get the translated summary from the label annotation. 4
RulesConditionBase::assertMetadata public function Asserts additional metadata for the selected data. Overrides ContextAwarePluginInterface::assertMetadata 1
RulesConditionBase::evaluate public function 5
RulesConditionBase::getContextValue public function
RulesConditionBase::negate public function Negates the result after evaluating this condition. Overrides RulesConditionInterface::negate
RulesConditionBase::refineContextDefinitions public function Refines used and provided context definitions based upon context values. Overrides ContextAwarePluginInterface::refineContextDefinitions 1
TextComparison::doEvaluate protected function Evaluate the text comparison.