Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change TypeConditionRule to allow multiple selections of entry types #11124

Merged
merged 15 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
### Changed
- Improved pagination UI accessibility. ([#11126](https://github.com/craftcms/cms/pull/11126))
- Live Preview now always shows a “Refresh” button, regardless of whether the preview target has auto-refresh enabled. ([#11160](https://github.com/craftcms/cms/discussions/11160))
- Entry Type condition rules now allow multiple selections. ([#11124](https://github.com/craftcms/cms/pull/11124))

### Removed
- Removed `craft\elements\conditions\entries\EntryTypeCondition::$sectionUid`.
- Removed `craft\elements\conditions\entries\EntryTypeCondition::$entryTypeUid`.
112 changes: 14 additions & 98 deletions src/elements/conditions/entries/TypeConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
namespace craft\elements\conditions\entries;

use Craft;
use craft\base\conditions\BaseConditionRule;
use craft\base\conditions\BaseMultiSelectConditionRule;
use craft\base\ElementInterface;
use craft\db\Table;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;
use craft\elements\db\EntryQuery;
use craft\elements\Entry;
use craft\helpers\ArrayHelper;
use craft\helpers\Cp;
use craft\helpers\Db;
use craft\helpers\UrlHelper;
use craft\models\Section;

/**
Expand All @@ -22,7 +17,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.0.0
*/
class TypeConditionRule extends BaseConditionRule implements ElementConditionRuleInterface
class TypeConditionRule extends BaseMultiSelectConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritdoc
Expand All @@ -45,130 +40,52 @@ public function getExclusiveQueryParams(): array
*/
private array $_sections = [];

public ?string $sectionUid = null;
public ?string $entryTypeUid = null;

/**
* @inheritdoc
*/
public function init(): void
{
$this->_sections = Craft::$app->getSections()->getAllSections();

// Set a default section
if (!$this->sectionUid) {
$this->sectionUid = ArrayHelper::firstValue($this->_sections)?->uid;
}

// Once we have a section, set a default entry type
$this->_ensureEntryType();

parent::init();
}

/**
* @inheritdoc
*/
public function getConfig(): array
public function setAttributes($values, $safeOnly = true): void
{
return array_merge(parent::getConfig(), [
'sectionUid' => $this->sectionUid,
'entryTypeUid' => $this->entryTypeUid,
]);
}
if (array_key_exists('entryTypeUid', $values)) {
$values['values'] = array_filter([$values['entryTypeUid']]);
unset($values['entryTypeUid'], $values['sectionUid']);
}

/**
* @return array
*/
private function _sectionOptions(): array
{
return ArrayHelper::map($this->_sections, 'uid', 'name');
parent::setAttributes($values, $safeOnly);
}

/**
* @return array
*/
private function _entryTypeOptions(): array
protected function options(): array
{
$options = [];
foreach ($this->_sections as $section) {
if ($section->uid == $this->sectionUid) {
foreach ($section->getEntryTypes() as $entryType) {
$options[$entryType->uid] = $entryType->name;
}
foreach ($section->getEntryTypes() as $entryType) {
$options[$entryType->uid] = sprintf('%s - %s', $section->name, $entryType->name);
}
}

return $options;
}

/**
* @inheritdoc
*/
protected function inputHtml(): string
{
$html = Cp::selectHtml([
'name' => 'sectionUid',
'value' => $this->sectionUid,
'options' => $this->_sectionOptions(),
'inputAttributes' => [
'hx' => [
'post' => UrlHelper::actionUrl('conditions/render'), // Only the section re-renders the body
'target' => 'closest .rule-body',
'select' => '#' . Craft::$app->getView()->namespaceInputId('rule-body'),
'swap' => 'outerHTML',
],
],
]);

$this->_ensureEntryType();

$html .= Cp::selectHtml([
'name' => 'entryTypeUid',
'value' => $this->entryTypeUid,
'options' => $this->_entryTypeOptions(),
]);

return $html;
}

/**
* Ensures an entry type is set correctly based on the section selected.
*/
private function _ensureEntryType(): void
{
if (!$this->sectionUid) {
$this->entryTypeUid = null;
return;
}

$entryTypeOptions = $this->_entryTypeOptions();

if (!$this->entryTypeUid || !ArrayHelper::keyExists($this->entryTypeUid, $entryTypeOptions)) {
$this->entryTypeUid = ArrayHelper::firstKey($entryTypeOptions);
}
}

/**
* @inheritdoc
*/
public function modifyQuery(ElementQueryInterface $query): void
{
$typeId = Db::idByUid(Table::ENTRYTYPES, $this->entryTypeUid);
$type = Craft::$app->getSections()->getEntryTypeById($typeId);

/** @var EntryQuery $query */
$query->type($type);
}

/**
* @inheritdoc
*/
protected function defineRules(): array
{
return array_merge(parent::defineRules(), [
[['sectionUid', 'entryTypeUid'], 'safe'],
]);
$sections = Craft::$app->getSections();
$query->typeId($this->paramValue(fn($uid) => $sections->getEntryTypeByUid($uid)->id ?? null));
}

/**
Expand All @@ -177,7 +94,6 @@ protected function defineRules(): array
public function matchElement(ElementInterface $element): bool
{
/** @var Entry $element */
$typeId = Db::idByUid(Table::ENTRYTYPES, $this->entryTypeUid);
return $element->getType()->id === (int)$typeId;
return $this->matchValue((string)$element->getType()->id);
}
}