Skip to content

Commit

Permalink
“Matrix field” entry condition rule
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Oct 10, 2023
1 parent 49c3444 commit 711c8de
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`.
Expand Down
1 change: 1 addition & 0 deletions src/elements/conditions/entries/EntryCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected function conditionRuleTypes(): array
PostDateConditionRule::class,
SavableConditionRule::class,
SectionConditionRule::class,
MatrixFieldConditionRule::class,
TypeConditionRule::class,
ViewableConditionRule::class,
]);
Expand Down
66 changes: 66 additions & 0 deletions src/elements/conditions/entries/MatrixFieldConditionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

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'];
}

/**
* @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);
}
}
1 change: 1 addition & 0 deletions src/translations/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 711c8de

Please sign in to comment.