-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
146 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace craft\elements\conditions\entries; | ||
|
||
use Craft; | ||
use craft\base\conditions\BaseMultiSelectConditionRule; | ||
use craft\base\ElementContainerFieldInterface; | ||
use craft\base\ElementInterface; | ||
use craft\elements\conditions\ElementConditionRuleInterface; | ||
use craft\elements\db\ElementQueryInterface; | ||
use craft\elements\db\EntryQuery; | ||
use craft\elements\Entry; | ||
use Illuminate\Support\Collection; | ||
|
||
/** | ||
* Field condition rule. | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 5.6.0 | ||
*/ | ||
class FieldConditionRule extends BaseMultiSelectConditionRule implements ElementConditionRuleInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
protected bool $reloadOnOperatorChange = true; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getLabel(): string | ||
{ | ||
return Craft::t('app', 'Field'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getExclusiveQueryParams(): array | ||
{ | ||
return ['field', 'fieldId']; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function operators(): array | ||
{ | ||
return [ | ||
...parent::operators(), | ||
self::OPERATOR_NOT_EMPTY, | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function options(): array | ||
{ | ||
return $this->nestedEntryFields() | ||
->keyBy(fn(ElementContainerFieldInterface $field) => $field->uid) | ||
->map(fn(ElementContainerFieldInterface $field) => $field->getUiLabel()) | ||
->all(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function modifyQuery(ElementQueryInterface $query): void | ||
{ | ||
/** @var EntryQuery $query */ | ||
if ($this->operator === self::OPERATOR_NOT_EMPTY) { | ||
$query->field($this->nestedEntryFields()->all()); | ||
} else { | ||
$fieldsService = Craft::$app->getFields(); | ||
$query->fieldId($this->paramValue(fn($uid) => $fieldsService->getFieldByUid($uid)->id ?? null)); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function matchElement(ElementInterface $element): bool | ||
{ | ||
/** @var Entry $element */ | ||
if ($this->operator === self::OPERATOR_NOT_EMPTY) { | ||
return $element->getField() !== null; | ||
} | ||
|
||
return $this->matchValue($element->getField()?->uid); | ||
} | ||
|
||
/** | ||
* @return Collection<ElementContainerFieldInterface> | ||
*/ | ||
private function nestedEntryFields(): Collection | ||
{ | ||
$fieldsService = Craft::$app->getFields(); | ||
return Collection::make($fieldsService->getNestedEntryFieldTypes()) | ||
->map(fn(string $class) => $fieldsService->getFieldsByType($class)) | ||
->flatten(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,65 +2,15 @@ | |
|
||
namespace craft\elements\conditions\entries; | ||
|
||
use Craft; | ||
use craft\base\conditions\BaseMultiSelectConditionRule; | ||
use craft\base\ElementInterface; | ||
use craft\elements\conditions\ElementConditionRuleInterface; | ||
use craft\elements\db\ElementQueryInterface; | ||
use craft\elements\db\EntryQuery; | ||
use craft\elements\Entry; | ||
use craft\fields\Matrix; | ||
use craft\helpers\ArrayHelper; | ||
|
||
/** | ||
* Matrix field condition rule. | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 5.0.0 | ||
*/ | ||
class MatrixFieldConditionRule extends BaseMultiSelectConditionRule implements ElementConditionRuleInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getLabel(): string | ||
{ | ||
return Craft::t('app', 'Matrix field'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getExclusiveQueryParams(): array | ||
{ | ||
return ['field', 'fieldId']; | ||
} | ||
|
||
/** @phpstan-ignore-next-line */ | ||
if (false) { | ||
/** | ||
* @inheritdoc | ||
* @since 5.0.0 | ||
* @deprecated in 5.6.0 | ||
*/ | ||
protected function options(): array | ||
class MatrixFieldConditionRule | ||
{ | ||
$fields = Craft::$app->getFields()->getFieldsByType(Matrix::class); | ||
return ArrayHelper::map($fields, 'uid', 'name'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function modifyQuery(ElementQueryInterface $query): void | ||
{ | ||
/** @var EntryQuery $query */ | ||
$fieldsService = Craft::$app->getFields(); | ||
$query->fieldId($this->paramValue(fn($uid) => $fieldsService->getFieldByUid($uid)->id ?? null)); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function matchElement(ElementInterface $element): bool | ||
{ | ||
/** @var Entry $element */ | ||
return $this->matchValue($element->getField()?->uid); | ||
} | ||
} | ||
|
||
class_alias(FieldConditionRule::class, MatrixFieldConditionRule::class); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters