1

DeleteButton.php

<?php
namespace Xxx\Faqs\Block\Adminhtml\Faqcat\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
 * Class DeleteButton
 */
class DeleteButton extends GenericButton implements ButtonProviderInterface
{
    /**
     * @return array
     */
    public function getButtonData()
    {
        $data = [];
        if ($this->getId()) {
            $data = [
                'label' => __('Delete'),
                'class' => 'delete',
                'on_click' => 'deleteConfirm(\''
                    . __('Are you sure you want to delete this Category?')
                    . '\', \'' . $this->getDeleteUrl() . '\')',
                'sort_order' => 20,
            ];
        }
        return $data;
    }

    /**
     * @return string
     */
    public function getDeleteUrl()
    {
        return $this->getUrl('*/*/delete', ['id' => $this->getId()]);
    }
}

GenericButton.php

<?php
namespace Xxx\Faqs\Block\Adminhtml\Faqcat\Edit;

use Magento\Search\Controller\RegistryConstants;

/**
 * Class GenericButton
 */
class GenericButton
{
    /**
     * Url Builder
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlBuilder;

    /**
     * Registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $registry;

    /**
     * Constructor
     *
     * @param \Magento\Backend\Block\Widget\Context $context
     * @param \Magento\Framework\Registry $registry
     */
    public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        \Magento\Framework\Registry $registry
    ) {
        $this->urlBuilder = $context->getUrlBuilder();
        $this->registry = $registry;
    }

    /**
     * Return the synonyms group Id.
     *
     * @return int|null
     */
    public function getId()
    {
        $post_id = $this->registry->registry('id');
        return $post_id ? $post_id->getId() : null;
    }

    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = [])
    {
        return $this->urlBuilder->getUrl($route, $params);
    }
}

Delete.php

<?php
namespace Xxx\Faqs\Controller\Adminhtml\Faqcat;

use Magento\Backend\App\Action;

class Delete extends \Magento\Backend\App\Action
{
    public function execute()
    {

         $id = $this->getRequest()->getParam('id');


        if (!($faq = $this->_objectManager->create("Xxx\Faqs\Model\Faqcat")->load($id))) {
            $this->messageManager->addError(__('Unable to proceed. Please, try again.'));
            $resultRedirect = $this->resultRedirectFactory->create();
            return $resultRedirect->setPath('*/*/index', array('_current' => true));

        }
        try{
            $faq->delete();
            $this->messageManager->addSuccess(__('Your FAQ Category has been deleted !'));
        } catch (Exception $e) {
            $this->messageManager->addError(__('Error while trying to delete faq: '));
            $resultRedirect = $this->resultRedirectFactory->create();
            return $resultRedirect->setPath('*/*/index', array('_current' => true));
        }

        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect->setPath('*/*/index', array('_current' => true));
    }
}

But this button not working in admin form in magento2.If any error in my code.

5
  • whats your primary key fieldname of table for this module? Commented Nov 28, 2017 at 6:12
  • id is mytable primary key
    – Mahi M
    Commented Nov 28, 2017 at 6:13
  • do you want to create delete button per row? or mass action? Commented Nov 28, 2017 at 6:17
  • i want create delete button in admin form
    – Mahi M
    Commented Nov 28, 2017 at 6:18
  • @MahalakshmiM check your controller's delete action. Do you get your id in delete action or not? Commented Nov 28, 2017 at 6:21

1 Answer 1

1

Replace GenericButton.php file with below content,

<?php
namespace Xxx\Faqs\Block\Adminhtml\Faqcat\Edit;

use Magento\Backend\Block\Widget\Context;
/**
 * Class GenericButton
 */
class GenericButton
{
    /**
     * @var Context
     */
    protected $context;

    /**
     * @param Context $context
     */
    public function __construct(
        Context $context
    ) {
        $this->context = $context;
    }

    /**
     * Return Epp ID
     *
     * @return int|null
     */
    public function getId()
    {
        return $this->context->getRequest()->getParam('id');
    }

    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = [])
    {
        return $this->context->getUrlBuilder()->getUrl($route, $params);
    }
}

Delete.php file,

<?php
namespace Xxx\Faqs\Controller\Adminhtml\Faqcat;

use Magento\Backend\App\Action;

class Delete extends \Magento\Backend\App\Action
{
    public function execute()
    {
        $id = $this->getRequest()->getParam('id');
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        if ($id) {
            try {
                // init model and delete
                $model = $this->_objectManager->create('OX\ManageFaqs\Model\Faqcat');
                $model->load($id);
                $model->delete();
                // display success message
                $this->messageManager->addSuccess(__('Your FAQ Category has been deleted !'));
                return $resultRedirect->setPath('*/*/');
            } catch (Exception $e) {
                // display error message
                $this->messageManager->addError($e->getMessage());
                // go back to edit form
                return $resultRedirect->setPath('*/*/edit', ['id' => $id]);
        }

        // display error message
        $this->messageManager->addError(__('We can\'t find a epp to delete.'));
        return $resultRedirect->setPath('*/*/');
    }
}

Remove var/generation folder and check.

4
  • But why we use getId->context instance of registry
    – Mahi M
    Commented Nov 28, 2017 at 6:34
  • you can get exact item id using getId method Commented Nov 28, 2017 at 6:37
  • it's working perfectly
    – Mahi M
    Commented Nov 28, 2017 at 6:42
  • issue solved or not for above quesiton? You can close this question. Commented Nov 29, 2017 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.