Skip to content

[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
Merged
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
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);
}
}
76 changes: 76 additions & 0 deletions app/code/Magento/Widget/Model/DeleteWidgetById.php
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
<argument name="default_dir" xsi:type="string">ASC</argument>
<argument name="dataSource" xsi:type="object" shared="false">Magento\Widget\Model\ResourceModel\Widget\Instance\Collection</argument>
</arguments>
<block class="Magento\Backend\Block\Widget\Grid\Massaction" name="adminhtml.widget.instance.grid.massactions" as="grid.massaction">
<arguments>
<argument name="massaction_id_field" xsi:type="string">instance_id</argument>
<argument name="form_field_name" xsi:type="string">delete</argument>
<argument name="use_select_all" xsi:type="string">1</argument>
<argument name="options" xsi:type="array">
<item name="delete" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Delete</item>
<item name="url" xsi:type="string">*/*/massDelete</item>
<item name="selected" xsi:type="string">0</item>
</item>
</argument>
</arguments>
</block>
<block class="Magento\Backend\Block\Widget\Grid\ColumnSet" name="adminhtml.widget.instance.grid.columnSet" as="grid.columnSet">
<arguments>
<argument name="rowUrl" xsi:type="array">
Expand Down