Categories
CMS Magento Mastering Development

How to save data in custom table onclick custom button from Customer Edit page Adminhtml? magento 2

Basically so far I have added a custom button(highlighted in image) on admin/customer/edit page. In order to add the custom button I have overridden

\vendor\magento\module-customer\view\base\ui_component\customer_form.xml TO app\code\Vendor\Customer\view\base\ui_component\customer_form.xml

app\code\Vendor\Customer\view\base\ui_component\customer_form.xml

        <button name="newsignupreview" class="Vendor\Customer\Block\Adminhtml\Edit\NewSignupReviewButton"/>

app\code\Vendor\Customer\etc\di.xml

<preference for="Vendor\Customer\Api\Data\NewSignupReviewInterface" type="Vendor\Customer\Model\Customer\NewSignupReview" />

app\code\Vendor\Customer\Block\Adminhtml\Edit\NewSignupReviewButton.php

<?php

namespace Vendor\Customer\Block\Adminhtml\Edit;

use Vendor\Customer\Api\Data\NewSignupReviewInterface;
use Magento\Customer\Block\Adminhtml\Edit\GenericButton;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class NewSignupReviewButton extends GenericButton implements ButtonProviderInterface
{
    protected $newsignupreview;

    public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        \Magento\Framework\Registry $registry,
        NewSignupReviewInterface $newsignupreview,
        AccountManagementInterface $customerAccountManagement
    ){
        parent::__construct($context, $registry);
        $this->newsignupreview = $newsignupreview;
        $this->customerAccountManagement = $customerAccountManagement;
    }

    public function getButtonData(){
        $customerId = $this->getCustomerId();
        $canModify = $customerId && !$this->customerAccountManagement->isReadonly($this->getCustomerId());
        $data = [];
        if ($customerId && $canModify) {
            $data = [
                'label' => __('Reject'),
                'class' => 'newsignupreview',
                'id' => 'customer-edit-newsignupreview-button',
                'data_attribute' => [

                ],
                'on_click' => '',
                'sort_order' => 15,
            ];
        }
        return $data;
    }

    public function getNewSignupReviewUrl()
    {
        return $this->getUrl('*/*/newsignupreview', ['id' => $this->getCustomerId()]);
    }
}

?>

So if that Reject button is clicked it will insert a record in my custom table with customerid and status as “Rejected” also at the same time it will change the button name from Reject to Unreject.

app\code\Vendor\Customer\Setup\InstallSchema.php

<?php

namespace Vendor\Customer\Setup;

class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface
{

    public function install(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        if (!$installer->tableExists('new_signup_review')) {
            $table = $installer->getConnection()->newTable(
                $installer->getTable('new_signup_review')
            )
                ->addColumn(
                    'entity_id',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => false,
                        'nullable' => false,
                        'primary'  => true,
                        'unsigned' => true,
                    ],
                    'Entity ID'
                )
                ->addColumn(
                    'signup_review',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    255,
                    ['nullable' => false],
                    'Signup Review'
                )
                ->setComment('New Signup Review Table');
            $installer->getConnection()->createTable($table);
        }
        $installer->endSetup();
    }
}

I am not getting any errors or warning so far. My question is what steps should I implement that would call my controller from block and controller will update the custom table with customerid and status along with changing button name to “Unreject”. It would be really helpful if any can guide me through this any suggestions or advice will definitely help me learn magento 2. Thanks

app\code\Vendor\Customer\Api\Data\NewSignupReviewInterface.php

<?php

namespace Vendor\Customer\Api\Data;


interface NewSignupReviewInterface
{
    const ENTITY_ID = 'entity_id';
    const SIGNUP_REVIEW = 'signup_review';

    public function getEntityId();

    public function setEntityId($entityId);

    public function getSignupReview();

    public function setSignupReview($signupreview);

}

app\code\Vendor\Customer\Model\Customer\NewSignupReview.php

<?php

namespace Vendor\Customer\Model\Customer;


use Magento\Framework\Model\AbstractModel;
use Vendor\Customer\Api\Data\NewSignupReviewInterface;
use Vendor\Customer\Model\ResourceModel\NewSignupReview as ResourceModel;


class NewSignupReview extends AbstractModel implements NewSignupReviewInterface
{
    protected $mappedReasons = [];

    protected function _construct()
    {
        $this->_init(ResourceModel::class);
    }

    public function getSignupReview()
    {
        return $this->getData(self::SIGNUP_REVIEW);
    }

    public function setSignupReview($signupreview)
    {
        return $this->setData(self::SIGNUP_REVIEW, $signupreview);
    }

}

enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *