Skip to content

Commit

Permalink
"Disabled for Site" status option for entries
Browse files Browse the repository at this point in the history
resolves #2899
  • Loading branch information
brandonkelly committed Nov 3, 2018
1 parent dcbdd24 commit c3aa1d8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

### Added
- Added `craft\base\ElementAction::$elementType`, which element action classes can use to reference their associated element type.
- Added `craft\elements\actions\SetStatus::$allowDisabledForSite`, which can be used by localizable element types to enable a “Disabled for Site” status option.

### Changed
- Entries’ “Enabled” setting is now labeled “Enabled Globally” on multi-site installs. ([#2899](https://github.com/craftcms/cms/issues/2899))
- Entries’ “Enabled for site” setting now includes the site name in its label, and only shows up if the “Enabled Globally” setting is checked. ([#2899](https://github.com/craftcms/cms/issues/2899))
- The Set Status action on the Entries index page now includes a “Disabled for Site” option. ([#2899](https://github.com/craftcms/cms/issues/2899))

### Fixed
- Fixed a bug where the Edit User page could forget which permissions were selected when saving a user with validation errors, if the Username, First Name, and Last name fields were all blank. ([#3412](https://github.com/craftcms/cms/issues/3412))
Expand Down
5 changes: 4 additions & 1 deletion src/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ protected static function defineActions(string $source = null): array

// Set Status
if ($canSetStatus) {
$actions[] = SetStatus::class;
$actions[] = [
'type' => SetStatus::class,
'allowDisabledForSite' => true,
];
}

// Edit
Expand Down
66 changes: 52 additions & 14 deletions src/elements/actions/SetStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@
*/
class SetStatus extends ElementAction
{
// Constants
// =========================================================================

const ENABLED = 'enabled';
const DISABLED_GLOBALLY = 'disabled';
const DISABLED_FOR_SITE = 'disabled-for-site';

// Properties
// =========================================================================

/**
* @var bool Whether to show the “Disabled for Site” status option.
*/
public $allowDisabledForSite = false;

/**
* @var string|null The status elements should be set to
*/
Expand Down Expand Up @@ -52,7 +64,9 @@ public function rules()
$rules[] = [
['status'],
'in',
'range' => [Element::STATUS_ENABLED, Element::STATUS_DISABLED]
'range' => $this->allowDisabledForSite
? [self::ENABLED, self::DISABLED_GLOBALLY, self::DISABLED_FOR_SITE]
: [self::ENABLED, self::DISABLED_GLOBALLY]
];

return $rules;
Expand All @@ -63,7 +77,15 @@ public function rules()
*/
public function getTriggerHtml()
{
return Craft::$app->getView()->renderTemplate('_components/elementactions/SetStatus/trigger');
$allowDisabledForSite = (
$this->allowDisabledForSite &&
Craft::$app->getIsMultiSite() &&
$this->elementType::isLocalized()
);

return Craft::$app->getView()->renderTemplate('_components/elementactions/SetStatus/trigger', [
'allowDisabledForSite' => $allowDisabledForSite,
]);
}

/**
Expand All @@ -75,24 +97,40 @@ public function getTriggerHtml()
public function performAction(ElementQueryInterface $query): bool
{
$elementsService = Craft::$app->getElements();
$enabled = ($this->status === Element::STATUS_ENABLED);

/** @var Element[] $elements */
$elements = $query->all();
$failCount = 0;

foreach ($elements as $element) {
// Skip if there's nothing to change
if ($element->enabled == $enabled && (!$enabled || $element->enabledForSite)) {
continue;
}

if ($enabled) {
// Also enable for this site
$element->enabled = $element->enabledForSite = true;
$element->setScenario(Element::SCENARIO_LIVE);
} else {
$element->enabled = false;
switch ($this->status) {
case self::ENABLED:
// Skip if there's nothing to change
if ($element->enabled && $element->enabledForSite) {
continue 2;
}

$element->enabled = $element->enabledForSite = true;
$element->setScenario(Element::SCENARIO_LIVE);
break;

case self::DISABLED_GLOBALLY:
// Skip if there's nothing to change
if (!$element->enabled) {
continue 2;
}

$element->enabled = false;
break;

case self::DISABLED_FOR_SITE:
// Skip if there's nothing to change
if (!$element->enabledForSite) {
continue 2;
}

$element->enabledForSite = false;
break;
}

if ($elementsService->saveElement($element) === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<div class="menu">
<ul>
<li><a class="formsubmit" data-param="status" data-value="enabled"><span class="status enabled"></span> {{ "Enabled"|t('app') }}</a></li>
<li><a class="formsubmit" data-param="status" data-value="disabled"><span class="status disabled"></span> {{ "Disabled"|t('app') }}</a></li>
<li><a class="formsubmit" data-param="status" data-value="disabled"><span class="status disabled"></span> {{ allowDisabledForSite ? "Disabled Globally"|t('app') : "Disabled"|t('app') }}</a></li>
{% if allowDisabledForSite %}
<li><a class="formsubmit" data-param="status" data-value="disabled-for-site"><span class="status disabled"></span> {{ "Disabled for Site"|t('app') }}</a></li>
{% endif %}
</ul>
</div>

0 comments on commit c3aa1d8

Please sign in to comment.