Skip to content

Commit a5a07b8

Browse files
authored
ENGCOM-5614: [Widgets] Mass-action delete for Widgets #20765
2 parents 369d426 + 9669786 commit a5a07b8

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\Widget\Controller\Adminhtml\Widget\Instance;
10+
11+
use Magento\Backend\App\Action;
12+
use Magento\Backend\App\Action\Context;
13+
use Magento\Backend\Model\View\Result\Redirect;
14+
use Magento\Framework\App\Action\HttpPostActionInterface;
15+
use Magento\Framework\App\Response\RedirectInterface;
16+
use Magento\Framework\Controller\ResultFactory;
17+
use Magento\Framework\Controller\ResultInterface;
18+
use Magento\Framework\Exception\NoSuchEntityException;
19+
use Magento\Widget\Model\DeleteWidgetById;
20+
21+
/**
22+
* Class MassDelete
23+
*/
24+
class MassDelete extends Action implements HttpPostActionInterface
25+
{
26+
/**
27+
* Authorization level of a basic admin session
28+
*
29+
* @see _isAllowed()
30+
*/
31+
const ADMIN_RESOURCE = 'Magento_Widget::widget_instance';
32+
33+
/**
34+
* @var DeleteWidgetById
35+
*/
36+
private $deleteWidgetById;
37+
38+
/**
39+
* @param Context $context
40+
* @param DeleteWidgetById $deleteWidgetById
41+
*/
42+
public function __construct(
43+
Context $context,
44+
DeleteWidgetById $deleteWidgetById
45+
) {
46+
parent::__construct($context);
47+
$this->deleteWidgetById = $deleteWidgetById;
48+
}
49+
50+
/**
51+
* Execute action
52+
*
53+
* @return Redirect
54+
* @throws \Exception
55+
*/
56+
public function execute(): Redirect
57+
{
58+
$deletedInstances = 0;
59+
$notDeletedInstances = [];
60+
/** @var array $instanceIds */
61+
$instanceIds = $this->getInstanceIds();
62+
63+
if (!count($instanceIds)) {
64+
$this->messageManager->addErrorMessage(__('No widget instance IDs were provided to be deleted.'));
65+
66+
/** @var Redirect $resultRedirect */
67+
$resultRedirect = $this->getResultPage();
68+
69+
return $resultRedirect->setPath('*/*/');
70+
}
71+
72+
foreach ($instanceIds as $instanceId) {
73+
try {
74+
$this->deleteWidgetById->execute((int)$instanceId);
75+
$deletedInstances++;
76+
} catch (NoSuchEntityException $e) {
77+
$notDeletedInstances[] = $instanceId;
78+
}
79+
}
80+
81+
if ($deletedInstances) {
82+
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been deleted.', $deletedInstances));
83+
}
84+
85+
if (count($notDeletedInstances)) {
86+
$this->messageManager->addErrorMessage(
87+
__(
88+
'Widget(s) with ID(s) %1 were not found',
89+
trim(implode(', ', $notDeletedInstances))
90+
)
91+
);
92+
}
93+
94+
/** @var Redirect $resultRedirect */
95+
$resultRedirect = $this->getResultPage();
96+
97+
return $resultRedirect->setPath('*/*/');
98+
}
99+
100+
/**
101+
* Get instance IDs.
102+
*
103+
* @return array
104+
*/
105+
private function getInstanceIds(): array
106+
{
107+
$instanceIds = $this->getRequest()->getParam('delete');
108+
109+
if (!is_array($instanceIds)) {
110+
return [];
111+
}
112+
113+
return $instanceIds;
114+
}
115+
116+
/**
117+
* Get result page.
118+
*
119+
* @return ResultInterface|null
120+
*/
121+
private function getResultPage(): ?ResultInterface
122+
{
123+
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
124+
}
125+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Widget\Model;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Widget\Model\ResourceModel\Widget\Instance as InstanceResourceModel;
12+
use Magento\Widget\Model\Widget\InstanceFactory as WidgetInstanceFactory;
13+
use Magento\Widget\Model\Widget\Instance as WidgetInstance;
14+
15+
/**
16+
* Class DeleteWidgetById
17+
*/
18+
class DeleteWidgetById
19+
{
20+
/**
21+
* @var InstanceResourceModel
22+
*/
23+
private $resourceModel;
24+
25+
/**
26+
* @var WidgetInstanceFactory|WidgetInstance
27+
*/
28+
private $instanceFactory;
29+
30+
/**
31+
* @param InstanceResourceModel $resourceModel
32+
* @param WidgetInstanceFactory $instanceFactory
33+
*/
34+
public function __construct(
35+
InstanceResourceModel $resourceModel,
36+
WidgetInstanceFactory $instanceFactory
37+
) {
38+
$this->resourceModel = $resourceModel;
39+
$this->instanceFactory = $instanceFactory;
40+
}
41+
42+
/**
43+
* Delete widget instance by given instance ID
44+
*
45+
* @param int $instanceId
46+
* @return void
47+
* @throws \Exception
48+
*/
49+
public function execute(int $instanceId) : void
50+
{
51+
$model = $this->getWidgetById($instanceId);
52+
53+
$this->resourceModel->delete($model);
54+
}
55+
56+
/**
57+
* Get widget instance by given instance ID
58+
*
59+
* @param int $instanceId
60+
* @return WidgetInstance
61+
* @throws NoSuchEntityException
62+
*/
63+
private function getWidgetById(int $instanceId): WidgetInstance
64+
{
65+
/** @var WidgetInstance $widgetInstance */
66+
$widgetInstance = $this->instanceFactory->create();
67+
68+
$this->resourceModel->load($widgetInstance, $instanceId);
69+
70+
if (!$widgetInstance->getId()) {
71+
throw NoSuchEntityException::singleField('instance_id', $instanceId);
72+
}
73+
74+
return $widgetInstance;
75+
}
76+
}

app/code/Magento/Widget/view/adminhtml/layout/adminhtml_widget_instance_block.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
<argument name="default_dir" xsi:type="string">ASC</argument>
1616
<argument name="dataSource" xsi:type="object" shared="false">Magento\Widget\Model\ResourceModel\Widget\Instance\Collection</argument>
1717
</arguments>
18+
<block class="Magento\Backend\Block\Widget\Grid\Massaction" name="adminhtml.widget.instance.grid.massactions" as="grid.massaction">
19+
<arguments>
20+
<argument name="massaction_id_field" xsi:type="string">instance_id</argument>
21+
<argument name="form_field_name" xsi:type="string">delete</argument>
22+
<argument name="use_select_all" xsi:type="string">1</argument>
23+
<argument name="options" xsi:type="array">
24+
<item name="delete" xsi:type="array">
25+
<item name="label" xsi:type="string" translate="true">Delete</item>
26+
<item name="url" xsi:type="string">*/*/massDelete</item>
27+
<item name="selected" xsi:type="string">0</item>
28+
</item>
29+
</argument>
30+
</arguments>
31+
</block>
1832
<block class="Magento\Backend\Block\Widget\Grid\ColumnSet" name="adminhtml.widget.instance.grid.columnSet" as="grid.columnSet">
1933
<arguments>
2034
<argument name="rowUrl" xsi:type="array">

0 commit comments

Comments
 (0)