Skip to content

Commit 4376b91

Browse files
author
Vasilii
committed
Added possibility to mass-delete widget instances
1 parent 073121a commit 4376b91

File tree

4 files changed

+213
-0
lines changed

4 files changed

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

app/code/Magento/Widget/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@
3434
<plugin name="widget-layout-update-plugin"
3535
type="Magento\Widget\Model\ResourceModel\Layout\Plugin" sortOrder="10"/>
3636
</type>
37+
<preference for="Magento\Widget\Api\DeleteWidgetInstanceByIdInterface"
38+
type="Magento\Widget\Model\DeleteWidgetInstanceById" />
3739
</config>

0 commit comments

Comments
 (0)