From 711c8de631c98f14bc9b474dccfd20dd17642c78 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 10 Oct 2023 16:48:29 -0700 Subject: [PATCH] =?UTF-8?q?=E2=80=9CMatrix=20field=E2=80=9D=20entry=20cond?= =?UTF-8?q?ition=20rule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #13794 --- CHANGELOG-WIP.md | 2 + .../conditions/entries/EntryCondition.php | 1 + .../entries/MatrixFieldConditionRule.php | 66 +++++++++++++++++++ src/translations/en/app.php | 1 + 4 files changed, 70 insertions(+) create mode 100644 src/elements/conditions/entries/MatrixFieldConditionRule.php diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 44d236c23cf..4e5e88c4099 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -4,6 +4,7 @@ - All elements can now have thumbnails, provided by Assets fields. ([#12484](https://github.com/craftcms/cms/discussions/12484), [#12706](https://github.com/craftcms/cms/discussions/12706)) - Element indexes and relational fields now have the option to use card views. ([#6024](https://github.com/craftcms/cms/pull/6024)) - Element indexes now support inline editing for some custom field values. +- Entry conditions can now have a “Matrix field” rule. ([#13794](https://github.com/craftcms/cms/discussions/13794)) - User addresses are now displayed within an embedded element index. - Selected elements within relational fields now include a context menu with “View in a new tab”, “Edit”, and “Remove” options. - Selected elements within relational fields now include a dedicated drag handle. @@ -118,6 +119,7 @@ - Added `craft\elements\Tag::gqlTypeName()`. - Added `craft\elements\User::GQL_TYPE_NAME`. - Added `craft\elements\conditions\ElementConditionInterface::getFieldLayouts()`. +- Added `craft\elements\conditions\entries\MatrixFieldConditionRule`. - Added `craft\elements\db\EagerLoadInfo`. - Added `craft\elements\db\EagerLoadPlan::$lazy`. - Added `craft\elements\db\ElementQuery::$eagerLoadAlias`. diff --git a/src/elements/conditions/entries/EntryCondition.php b/src/elements/conditions/entries/EntryCondition.php index 990f71c0825..80be5078e1b 100644 --- a/src/elements/conditions/entries/EntryCondition.php +++ b/src/elements/conditions/entries/EntryCondition.php @@ -26,6 +26,7 @@ protected function conditionRuleTypes(): array PostDateConditionRule::class, SavableConditionRule::class, SectionConditionRule::class, + MatrixFieldConditionRule::class, TypeConditionRule::class, ViewableConditionRule::class, ]); diff --git a/src/elements/conditions/entries/MatrixFieldConditionRule.php b/src/elements/conditions/entries/MatrixFieldConditionRule.php new file mode 100644 index 00000000000..e4b8862e898 --- /dev/null +++ b/src/elements/conditions/entries/MatrixFieldConditionRule.php @@ -0,0 +1,66 @@ + + * @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']; + } + + /** + * @inheritdoc + */ + protected function options(): array + { + $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); + } +} diff --git a/src/translations/en/app.php b/src/translations/en/app.php index 70722f5e207..54a147b2cd5 100644 --- a/src/translations/en/app.php +++ b/src/translations/en/app.php @@ -868,6 +868,7 @@ 'Manage relations on a per-site basis' => 'Manage relations on a per-site basis', 'Manage your Craft Console account' => 'Manage your Craft Console account', 'Manipulated SVG image rasterizing is unreliable. See \\craft\\services\\Images::loadImage()' => 'Manipulated SVG image rasterizing is unreliable. See \\craft\\services\\Images::loadImage()', + 'Matrix field' => 'Matrix field', 'Matrix' => 'Matrix', 'Max Date' => 'Max Date', 'Max Length' => 'Max Length',