Skip to content

Commit

Permalink
Allow --drafts, --provisional-drafts, and --revisions to be set to `n…
Browse files Browse the repository at this point in the history
…ull`
  • Loading branch information
brandonkelly committed Dec 6, 2024
1 parent 98fa5a3 commit 4f05385
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- The Queue Manager utility now shows jobs’ class names. ([#16228](https://github.com/craftcms/cms/pull/16228))
- Improved the wording of field instance action labels. ([#16261](https://github.com/craftcms/cms/discussions/16261))
- Improved the error output for nested elements when they can’t be resaved via `resave` commands.
- `resave` commands’ `--drafts`, `--provisional-drafts`, and `--revisions` options can now be set to `null`, causing elements to be resaved regardless of whether they’re drafts/provisional drafts/revisions.

### Development
- Added support for fallback element partial templates, e.g. `_partials/entry.twig` as opposed to `_partials/entry/typeHandle.twig`. ([#16125](https://github.com/craftcms/cms/pull/16125))
Expand Down
41 changes: 25 additions & 16 deletions src/console/controllers/ResaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use craft\elements\User;
use craft\errors\InvalidElementException;
use craft\events\MultiElementActionEvent;
use craft\helpers\App;
use craft\helpers\Console;
use craft\helpers\ElementHelper;
use craft\helpers\Inflector;
Expand Down Expand Up @@ -102,21 +103,24 @@ final public static function normalizeTo(?string $to): callable

/**
* @var bool Whether to resave element drafts.
* Set to `null` if all elements should be resaved regardless of whether they’re drafts.
* @since 3.6.5
*/
public bool $drafts = false;
public bool|string|null $drafts = null;

/**
* @var bool Whether to resave provisional element drafts.
* Set to `null` if all elements should be resaved regardless of whether they’re provisional drafts.
* @since 3.7.0
*/
public bool $provisionalDrafts = false;
public bool|string|null $provisionalDrafts = null;

/**
* @var bool Whether to resave element revisions.
* @var bool|string Whether to resave element revisions.
* Set to `null` if all elements should be resaved regardless of whether they’re revisions.
* @since 3.7.35
*/
public bool $revisions = false;
public bool|string|null $revisions = null;

/**
* @var int|string|null The ID(s) of the elements to resave.
Expand Down Expand Up @@ -323,6 +327,16 @@ public function beforeAction($action): bool
return false;
}

// Can't default these properties to false because then yii\console\Controller::runAction() will
// typecast their values to booleans
foreach (['drafts', 'provisionalDrafts', 'revisions'] as $property) {
$this->$property ??= false;
if (is_string($this->$property)) {
$value = App::normalizeValue($this->$property);
$this->$property = $value !== null ? (bool)$value : null;
}
}

if (isset($this->propagateTo)) {
$siteHandles = array_filter(StringHelper::split($this->propagateTo));
$this->propagateTo = [];
Expand Down Expand Up @@ -678,21 +692,16 @@ public function saveElements(ElementQueryInterface $query): int
*/
private function _baseCriteria(): array
{
$criteria = [];
$criteria = [
'drafts' => $this->drafts,
'provisionalDrafts' => $this->provisionalDrafts,
'revisions' => $this->revisions,
];

if ($this->drafts) {
if ($this->provisionalDrafts !== false && $this->drafts == false) {
$criteria['drafts'] = true;
}

if ($this->provisionalDrafts) {
$criteria['drafts'] = true;
$criteria['provisionalDrafts'] = true;
}

if ($this->revisions) {
$criteria['revisions'] = true;
}

if ($this->elementId) {
$criteria['id'] = is_int($this->elementId) ? $this->elementId : explode(',', $this->elementId);
}
Expand Down Expand Up @@ -823,7 +832,7 @@ private function _resaveElements(ElementQueryInterface $query): int
} else {
$elementsService->on(Elements::EVENT_BEFORE_RESAVE_ELEMENT, $beforeCallback);
$elementsService->on(Elements::EVENT_AFTER_RESAVE_ELEMENT, $afterCallback);
$elementsService->resaveElements($query, true, !$this->revisions, $this->updateSearchIndex, $this->touch);
$elementsService->resaveElements($query, true, $this->revisions === false, $this->updateSearchIndex, $this->touch);
$elementsService->off(Elements::EVENT_BEFORE_RESAVE_ELEMENT, $beforeCallback);
$elementsService->off(Elements::EVENT_AFTER_RESAVE_ELEMENT, $afterCallback);
}
Expand Down

0 comments on commit 4f05385

Please sign in to comment.