-
Notifications
You must be signed in to change notification settings - Fork 9.4k
[Widgets] Mass-action delete for Widgets #20765
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
Merged
magento-engcom-team
merged 8 commits into
magento:2.3-develop
from
vasilii-b:2.3-widgets-bulk-delete
Aug 30, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
073121a
Added massaction and delete option to admin widget instances page
4376b91
Added possibility to mass-delete widget instances
7d92ade
Added adjustments as per maintainer's code review
0de0857
Merge remote-tracking branch 'origin/2.3-develop' into 2.3-widgets-bu…
2fd6ef1
magento/magento:20764 - Mass-action delete for Widgets
8225526
Add void strict type to new model
sidolov 8746079
magento/magento2#20765: Static test fix.
engcom-Foxtrot b56c3c4
Merge remote-tracking branch 'mainline/2.3-develop' into 2.3-widgets-…
p-bystritsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
app/code/Magento/Widget/Controller/Adminhtml/Widget/Instance/MassDelete.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
/** | ||
* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Widget\Controller\Adminhtml\Widget\Instance; | ||
|
||
use Magento\Backend\App\Action; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Backend\Model\View\Result\Redirect; | ||
use Magento\Framework\App\Action\HttpPostActionInterface; | ||
use Magento\Framework\App\Response\RedirectInterface; | ||
use Magento\Framework\Controller\ResultFactory; | ||
use Magento\Framework\Controller\ResultInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Widget\Model\DeleteWidgetById; | ||
|
||
/** | ||
* Class MassDelete | ||
*/ | ||
class MassDelete extends Action implements HttpPostActionInterface | ||
{ | ||
/** | ||
* Authorization level of a basic admin session | ||
* | ||
* @see _isAllowed() | ||
*/ | ||
const ADMIN_RESOURCE = 'Magento_Widget::widget_instance'; | ||
|
||
/** | ||
* @var DeleteWidgetById | ||
*/ | ||
private $deleteWidgetById; | ||
|
||
/** | ||
* @param Context $context | ||
* @param DeleteWidgetById $deleteWidgetById | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
DeleteWidgetById $deleteWidgetById | ||
) { | ||
parent::__construct($context); | ||
$this->deleteWidgetById = $deleteWidgetById; | ||
} | ||
|
||
/** | ||
* Execute action | ||
* | ||
* @return Redirect | ||
* @throws \Exception | ||
*/ | ||
public function execute(): Redirect | ||
{ | ||
$deletedInstances = 0; | ||
$notDeletedInstances = []; | ||
/** @var array $instanceIds */ | ||
$instanceIds = $this->getInstanceIds(); | ||
|
||
if (!count($instanceIds)) { | ||
$this->messageManager->addErrorMessage(__('No widget instance IDs were provided to be deleted.')); | ||
|
||
/** @var Redirect $resultRedirect */ | ||
$resultRedirect = $this->getResultPage(); | ||
|
||
return $resultRedirect->setPath('*/*/'); | ||
} | ||
|
||
foreach ($instanceIds as $instanceId) { | ||
try { | ||
$this->deleteWidgetById->execute((int)$instanceId); | ||
$deletedInstances++; | ||
} catch (NoSuchEntityException $e) { | ||
$notDeletedInstances[] = $instanceId; | ||
} | ||
} | ||
|
||
if ($deletedInstances) { | ||
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been deleted.', $deletedInstances)); | ||
} | ||
|
||
if (count($notDeletedInstances)) { | ||
$this->messageManager->addErrorMessage( | ||
__( | ||
'Widget(s) with ID(s) %1 were not found', | ||
trim(implode(', ', $notDeletedInstances)) | ||
) | ||
); | ||
} | ||
|
||
/** @var Redirect $resultRedirect */ | ||
$resultRedirect = $this->getResultPage(); | ||
|
||
return $resultRedirect->setPath('*/*/'); | ||
} | ||
|
||
/** | ||
* Get instance IDs. | ||
* | ||
* @return array | ||
*/ | ||
private function getInstanceIds(): array | ||
{ | ||
$instanceIds = $this->getRequest()->getParam('delete'); | ||
|
||
if (!is_array($instanceIds)) { | ||
return []; | ||
} | ||
|
||
return $instanceIds; | ||
} | ||
|
||
/** | ||
* Get result page. | ||
* | ||
* @return ResultInterface|null | ||
*/ | ||
private function getResultPage(): ?ResultInterface | ||
{ | ||
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Widget\Model; | ||
|
||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Widget\Model\ResourceModel\Widget\Instance as InstanceResourceModel; | ||
use Magento\Widget\Model\Widget\InstanceFactory as WidgetInstanceFactory; | ||
use Magento\Widget\Model\Widget\Instance as WidgetInstance; | ||
|
||
/** | ||
* Class DeleteWidgetById | ||
*/ | ||
class DeleteWidgetById | ||
{ | ||
/** | ||
* @var InstanceResourceModel | ||
*/ | ||
private $resourceModel; | ||
|
||
/** | ||
* @var WidgetInstanceFactory|WidgetInstance | ||
*/ | ||
private $instanceFactory; | ||
|
||
/** | ||
* @param InstanceResourceModel $resourceModel | ||
* @param WidgetInstanceFactory $instanceFactory | ||
*/ | ||
public function __construct( | ||
InstanceResourceModel $resourceModel, | ||
WidgetInstanceFactory $instanceFactory | ||
) { | ||
$this->resourceModel = $resourceModel; | ||
$this->instanceFactory = $instanceFactory; | ||
} | ||
|
||
/** | ||
* Delete widget instance by given instance ID | ||
* | ||
* @param int $instanceId | ||
* @return void | ||
* @throws \Exception | ||
*/ | ||
public function execute(int $instanceId) : void | ||
{ | ||
$model = $this->getWidgetById($instanceId); | ||
|
||
$this->resourceModel->delete($model); | ||
} | ||
|
||
/** | ||
* Get widget instance by given instance ID | ||
* | ||
* @param int $instanceId | ||
* @return WidgetInstance | ||
* @throws NoSuchEntityException | ||
*/ | ||
private function getWidgetById(int $instanceId): WidgetInstance | ||
{ | ||
/** @var WidgetInstance $widgetInstance */ | ||
$widgetInstance = $this->instanceFactory->create(); | ||
|
||
$this->resourceModel->load($widgetInstance, $instanceId); | ||
|
||
if (!$widgetInstance->getId()) { | ||
throw NoSuchEntityException::singleField('instance_id', $instanceId); | ||
} | ||
|
||
return $widgetInstance; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.