Skip to content

Instantly share code, notes, and snippets.

@timkelty
Last active March 24, 2026 14:11
Show Gist options
  • Select an option

  • Save timkelty/c0b731fd369e2152388dc733873119d7 to your computer and use it in GitHub Desktop.

Select an option

Save timkelty/c0b731fd369e2152388dc733873119d7 to your computer and use it in GitHub Desktop.
<?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());
}
}
@timkelty

Copy link
Copy Markdown
Author

Element condition for Craft CMS that adds simple field-level locking.

CleanShot 2026-03-24 at 10 10 38@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment