Last active
March 24, 2026 14:11
-
-
Save timkelty/c0b731fd369e2152388dc733873119d7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <?php | |
| namespace modules\foo\elements\conditions; | |
| use Craft; | |
| use craft\base\conditions\BaseLightswitchConditionRule; | |
| use craft\base\Element; | |
| use craft\base\ElementInterface; | |
| use craft\elements\db\ElementQueryInterface; | |
| /** | |
| * Element has existing drafts. | |
| * | |
| * @author Pixel & Tonic, Inc. <support@pixelandtonic.com> | |
| * @since 5.3.0 | |
| */ | |
| class HasExistingDrafts extends BaseLightswitchConditionRule implements ElementConditionRuleInterface | |
| { | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function getLabel(): string | |
| { | |
| return Craft::t('app', 'Has Existing Drafts'); | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function getExclusiveQueryParams(): array | |
| { | |
| return ['hasExistingDrafts']; | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function modifyQuery(ElementQueryInterface $query): void | |
| { | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function matchElement(ElementInterface $element): bool | |
| { | |
| $query = $element | |
| ->find() | |
| ->draftOf($element); | |
| if ($element->getIsDraft() && $element->dateCreated) { | |
| $query->dateCreated("< {$element->dateCreated->format(\DateTime::ATOM)}"); | |
| } | |
| return $this->matchValue($query->exists()); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Element condition for Craft CMS that adds simple field-level locking.