Skip to content

Commit

Permalink
Merge pull request #3403 from narration-sd/3.1-element-full-delete
Browse files Browse the repository at this point in the history
3.1 element full delete
  • Loading branch information
brandonkelly authored Oct 25, 2018
2 parents 1ce08ef + e5e338a commit ec6ad1f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
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

0 comments on commit ec6ad1f

Please sign in to comment.