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

Adds event before delete search keywords #13952

Closed
wants to merge 3 commits into from
Closed
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
36 changes: 36 additions & 0 deletions src/events/DeleteKeywordsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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 craft\base\FieldInterface;
use yii\base\Event;

/**
* CancelableEvent class.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class DeleteKeywordsEvent extends Event
{
/**
* @var ElementInterface The element being indexed
*/
public ElementInterface $element;

/**
* @var FieldInterface[] List fields whose keywords are being updated
*/
public array $updateFields = [];

/**
* @var int[] List field IDs to be left alone in the {{searchindexes}} table
*/
public array $ignoreFieldIds = [];
}
18 changes: 18 additions & 0 deletions src/services/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use craft\db\Query;
use craft\db\Table;
use craft\elements\db\ElementQuery;
use craft\events\DeleteKeywordsEvent;
use craft\events\IndexKeywordsEvent;
use craft\events\SearchEvent;
use craft\helpers\ArrayHelper;
Expand Down Expand Up @@ -49,6 +50,11 @@
*/
public const EVENT_BEFORE_INDEX_KEYWORDS = 'beforeIndexKeywords';

/**
* @event DeleteKeywordsEvent The event that is triggered before keywords are deleted for an element.
*/
public const EVENT_BEFORE_DELETE_KEYWORDS = 'beforeDeleteKeywords';

/**
* @event SearchEvent The event that is triggered before a search is performed.
*/
Expand Down Expand Up @@ -168,6 +174,18 @@
}
}

if ($this->hasEventHandlers(self::EVENT_BEFORE_DELETE_KEYWORDS)) {
$event = new DeleteKeywordsEvent([
'element' => $element,
'updateFields' => $updateFields,

Check failure on line 180 in src/services/Search.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Undefined variable: $updateFields
'ignoreFieldIds' => $ignoreFieldIds,
]);

$this->trigger(self::EVENT_BEFORE_DELETE_KEYWORDS, $event);
$updateFields = $event->updateFields;
$ignoreFieldIds = $event->ignoreFieldIds;
}

// Clear the element’s current search keywords
$deleteCondition = [
'elementId' => $element->id,
Expand Down
Loading