magento 2 clear previous searched string of ui grid after admin logout
-
11-05-2021 - |
Solution
This code will clear all ui grid past search string when admin logout.
Create file on the below location :
app/code/[Vendor]/[Module]/etc/adminhtml/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Backend\Controller\Adminhtml\Auth\Logout">
<plugin name="clear_active_filters_fron_ui_grid" type="[Vendor]\[Module]\Plugin\Controller\Adminhtml\Auth\Logout" sortOrder="1"/>
</type>
</config>
Now create a plugin to remove past search result. app/code/[Vendor]/[Module]/Plugin/Controller/Adminhtml/Auth/Logout.php
<?php
/**
* DISCLAIMER
*
* It will impact on all ui grid past search.
*
* @author vikas kalal
* @package General
*/
namespace [Vendor]\[Module]\Plugin\Controller\Adminhtml\Auth;
use Magento\Backend\Model\Auth\Session;
class Logout
{
const BOOKMARK_NAMESPACE = 'current';
/**
* @var \Magento\Ui\Model\BookmarkFactory $bookmarkFactory
*/
private $bookmarkFactory;
/**
* @param \Magento\Ui\Model\BookmarkFactory $bookmarkFactory
* @param Session $authSession
*/
public function __construct(
\Magento\Ui\Model\BookmarkFactory $bookmarkFactory,
Session $authSession
) {
$this->bookmarkFactory = $bookmarkFactory;
$this->authSession = $authSession;
}
/**
* Before Admin Logout Plugin
*
* @param \Magento\Backend\Controller\Adminhtml\Auth\Logout $subject
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeExecute(
\Magento\Backend\Controller\Adminhtml\Auth\Logout $subject
) {
$currentAdminUserId = $this->getCurrentUserId() ?? '';
if ($currentAdminUserId != '') {
$bookmarkCollection = $this->bookmarkFactory->create()->getCollection();
$bookmarkCollection->addFieldToFilter('identifier', self::BOOKMARK_NAMESPACE);
$bookmarkCollection->addFieldToFilter('user_id', $currentAdminUserId);
$bookmarkCollection->walk('delete');
}
}
/**
* Get Current Admin User
*
* @return int
*/
public function getCurrentUserId()
{
return $this->authSession->getUser()->getId();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange