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

3.1 element full delete #3403

Merged
merged 5 commits into from
Oct 25, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG-v3.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- Added `craft\db\SoftDeleteTrait`, which can be used by Active Record classes that wish to support soft deletes.
- Added `craft\elements\actions\Restore`, which can be included in elements’ `defineActions()` methods to opt into element restoration.
- Added `craft\events\ConfigEvent`.
- Added `craft\events\DeleteElementEvent`, which provides a `$hardDelete` property that can be set to `true` to force an element to be immediately hard-deleted. ([#3403](https://github.com/craftcms/cms/pull/3403))
- Added `craft\helpers\App::editionHandle()`.
- Added `craft\helpers\App::editionIdByHandle()`.
- Added `craft\helpers\App::mailSettings()`.
Expand Down
28 changes: 28 additions & 0 deletions src/events/DeleteElementEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\events;

use craft\base\ElementInterface;
use yii\base\Event;

/**
* Delete element event class.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
*/
class DeleteElementEvent extends ElementEvent
{
// Properties
// =========================================================================

/**
* @var bool Whether to immediately hard-delete the element, rather than soft-deleting it
*/
public $hardDelete = false;
}
27 changes: 17 additions & 10 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use craft\elements\User;
use craft\errors\ElementNotFoundException;
use craft\errors\InvalidElementException;
use craft\events\DeleteElementEvent;
use craft\events\ElementEvent;
use craft\events\MergeElementsEvent;
use craft\events\RegisterComponentTypesEvent;
Expand Down Expand Up @@ -921,11 +922,11 @@ public function deleteElement(ElementInterface $element): bool
{
/** @var Element $element */
// Fire a 'beforeDeleteElement' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_DELETE_ELEMENT)) {
$this->trigger(self::EVENT_BEFORE_DELETE_ELEMENT, new ElementEvent([
'element' => $element,
]));
}
$event = new DeleteElementEvent([
'element' => $element,
'hardDelete' => false,
]);
$this->trigger(self::EVENT_BEFORE_DELETE_ELEMENT, $event);

if (!$element->beforeDelete()) {
return false;
Expand Down Expand Up @@ -956,12 +957,18 @@ public function deleteElement(ElementInterface $element): bool
// this element is suddenly going to show up in a new query)
Craft::$app->getTemplateCaches()->deleteCachesByElementId($element->id, false);

// Soft delete the elements table row
Craft::$app->getDb()->createCommand()
->softDelete('{{%elements}}', ['id' => $element->id])
->execute();
if ($event->hardDelete) {
Craft::$app->getDb()->createCommand()
->delete('{{%elements}}', ['id' => $element->id])
->execute();
} else {
// Soft delete the elements table row
Craft::$app->getDb()->createCommand()
->softDelete('{{%elements}}', ['id' => $element->id])
->execute();
}

// Hard delete the search indexes
// Always hard delete the search indexes
Craft::$app->getDb()->createCommand()
->delete('{{%searchindex}}', ['elementId' => $element->id])
->execute();
Expand Down