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
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
- class \Drupal\Core\Executable\ExecutablePluginBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Executable\ExecutableInterface, \Drupal\Core\Cache\CacheableDependencyInterface, \Drupal\Core\Plugin\ContextAwarePluginInterface uses \Drupal\Core\Plugin\ContextAwarePluginTrait
- class \Drupal\Core\Condition\ConditionPluginBase extends \Drupal\Core\Executable\ExecutablePluginBase implements \Drupal\Core\Condition\ConditionInterface uses \Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait
- class \Drupal\rules\Core\RulesConditionBase extends \Drupal\Core\Condition\ConditionPluginBase implements \Drupal\rules\Core\RulesConditionInterface uses \Drupal\rules\Context\ContextProviderTrait, \Drupal\rules\Core\ExecutablePluginTrait, \Drupal\rules\Core\ConfigurationAccessControlTrait
- class \Drupal\rules\Plugin\Condition\TextComparison extends \Drupal\rules\Core\RulesConditionBase
- class \Drupal\rules\Core\RulesConditionBase extends \Drupal\Core\Condition\ConditionPluginBase implements \Drupal\rules\Core\RulesConditionInterface uses \Drupal\rules\Context\ContextProviderTrait, \Drupal\rules\Core\ExecutablePluginTrait, \Drupal\rules\Core\ConfigurationAccessControlTrait
- class \Drupal\Core\Condition\ConditionPluginBase extends \Drupal\Core\Executable\ExecutablePluginBase implements \Drupal\Core\Condition\ConditionInterface uses \Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait
- class \Drupal\Core\Executable\ExecutablePluginBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Executable\ExecutableInterface, \Drupal\Core\Cache\CacheableDependencyInterface, \Drupal\Core\Plugin\ContextAwarePluginInterface uses \Drupal\Core\Plugin\ContextAwarePluginTrait
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
Expanded class hierarchy of TextComparison
File
-
src/
Plugin/ Condition/ TextComparison.php, line 35
Namespace
Drupal\rules\Plugin\ConditionView 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;
}
}
}