Skip to content

Commit f668710

Browse files
authored
Merge pull request #12135 from craftcms/feature/dev-653-more-textual-condition-rule-operators
More textual condition rule operators
2 parents 5e80bcc + 92d5107 commit f668710

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

CHANGELOG-WIP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
- Notifications are now shown after executing folder actions on the Assets index page. ([#11906/](https://github.com/craftcms/cms/pull/11906))
9494
- Date range condition rules now support “Today”, “This week”, “This month”, “This year”, “Past 7 days”, “Past 30 days”, “Past 30 days”, “Past year”, “Before…”, and “After…” relative range types, in addition to specifying a custom date range. ([#11888](https://github.com/craftcms/cms/pull/11888))
9595
- Number condition rules now support an “is between…” operator. ([#11950](https://github.com/craftcms/cms/pull/11950))
96+
- All text, number, and date range condition rules now support “has a value” and “is empty” operators. ([#11070](https://github.com/craftcms/cms/discussions/11070))
9697
- If Live Preview is triggered while a draft is saving, it will now wait until the save completes before opening. ([#11858](https://github.com/craftcms/cms/issues/11858), [#11895](https://github.com/craftcms/cms/pull/11895))
9798
- Addresses now support change tracking.
9899
- It’s now possible to restore assets that were deleted programmatically with `craft\elements\Asset::$keepFile` set to `true`. ([#11761](https://github.com/craftcms/cms/issues/11761))

src/base/conditions/BaseDateRangeConditionRule.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ protected function inputHtml(): string
139139
DateRange::TYPE_RANGE,
140140
])) {
141141
$index = 1;
142+
} elseif (in_array($value, [self::OPERATOR_NOT_EMPTY, self::OPERATOR_EMPTY])) {
143+
$index = 2;
142144
} else {
143145
$index = 0;
144146
}
@@ -273,6 +275,8 @@ protected function rangeTypeOptions(): array
273275
DateRange::TYPE_BEFORE => Craft::t('app', 'Before…'),
274276
DateRange::TYPE_AFTER => Craft::t('app', 'After…'),
275277
DateRange::TYPE_RANGE => Craft::t('app', 'Range…'),
278+
self::OPERATOR_NOT_EMPTY => Craft::t('app', 'has a value'),
279+
self::OPERATOR_EMPTY => Craft::t('app', 'is empty'),
276280
];
277281
}
278282

@@ -335,6 +339,12 @@ protected function queryParamValue(): array|string|null
335339
return ($this->rangeType === DateRange::TYPE_AFTER ? '>=' : '<') . ' ' .
336340
DateTimeHelper::toIso8601(DateTimeHelper::now()->add($dateInterval));
337341

342+
case self::OPERATOR_EMPTY:
343+
return ':empty:';
344+
345+
case self::OPERATOR_NOT_EMPTY:
346+
return 'not :empty:';
347+
338348
default:
339349
[$startDate, $endDate] = DateRange::dateRangeByType($this->rangeType);
340350
$startDate = DateTimeHelper::toIso8601($startDate);
@@ -373,6 +383,12 @@ protected function matchValue(?DateTime $value): bool
373383

374384
return $value && $value < $date;
375385

386+
case self::OPERATOR_EMPTY:
387+
return !$value;
388+
389+
case self::OPERATOR_NOT_EMPTY:
390+
return (bool)$value;
391+
376392
default:
377393
[$startDate, $endDate] = DateRange::dateRangeByType($this->rangeType);
378394
return $value && $value >= $startDate && $value < $endDate;

src/base/conditions/BaseNumberConditionRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ protected function operators(): array
5454
self::OPERATOR_GT,
5555
self::OPERATOR_GTE,
5656
self::OPERATOR_BETWEEN,
57+
self::OPERATOR_NOT_EMPTY,
58+
self::OPERATOR_EMPTY,
5759
];
5860
}
5961

src/base/conditions/BaseTextConditionRule.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ protected function operators(): array
4848
self::OPERATOR_BEGINS_WITH,
4949
self::OPERATOR_ENDS_WITH,
5050
self::OPERATOR_CONTAINS,
51+
self::OPERATOR_NOT_EMPTY,
52+
self::OPERATOR_EMPTY,
5153
];
5254
}
5355

@@ -106,6 +108,13 @@ protected function defineRules(): array
106108
*/
107109
protected function paramValue(): ?string
108110
{
111+
switch ($this->operator) {
112+
case self::OPERATOR_EMPTY:
113+
return ':empty:';
114+
case self::OPERATOR_NOT_EMPTY:
115+
return 'not :empty:';
116+
}
117+
109118
if ($this->value === '') {
110119
return null;
111120
}
@@ -128,6 +137,13 @@ protected function paramValue(): ?string
128137
*/
129138
protected function matchValue(mixed $value): bool
130139
{
140+
switch ($this->operator) {
141+
case self::OPERATOR_EMPTY:
142+
return !$value;
143+
case self::OPERATOR_NOT_EMPTY:
144+
return (bool)$value;
145+
}
146+
131147
if ($this->value === '') {
132148
return true;
133149
}

0 commit comments

Comments
 (0)