0

How can i override the block's protected method and remove fields from form

namespace NameSpace\ModuleName\Block\Adminhtml\Sales\Order\Edit\Form;

use Magento\Backend\Block\Template; use Magento\Backend\Block\Template\Context; use Magento\Framework\View\Result\PageFactory; use NameSpace\ModuleName\Helper\Data; use NameSpace\ModuleName\Model\Order; use NameSpace\ModuleName\Model\Quote;

class Info extends Template {

protected $quote;


protected $order;

protected $helperData;

protected $formFactory;

protected $form;

protected $orderRepository;

protected $orderStatusConfig;

protected $orderStateConfig;


public function __construct(
    Context $context,
    PageFactory $resultPageFactory,
    Data $helperData,
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Sales\Model\Config\Source\Order\Status $orderStatusConfig,
    \ModuleName\ModuleName\Model\Config\Source\Order\State $orderStateConfig,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->helperData        = $helperData;
    $this->formFactory       = $formFactory;
    $this->orderRepository   = $orderRepository;
    $this->orderStatusConfig = $orderStatusConfig;
    $this->orderStateConfig  = $orderStateConfig;
}


public function getForm()
{
    if ($this->form === null) {
        $this->form = $this->formFactory->create();
        $this->prepareForm();
    }

    return $this->form;
}


public function getQuote()
{
    return $this->helperData->getQuote();
}


public function setQuote($quote)
{
    $this->quote = $quote;

    return $this;
}


public function getOrder()
{
    $order = $this->helperData->getOrder();
    if (!$order) {
        $orderId = $this->getRequest()->getParam('order_id');
        $order   = $this->orderRepository->get($orderId);
    }

    return $order;
}


public function setOrder($order)
{
    $this->order = $order;

    return $this;
}

protected function prepareForm()
{
    $fieldset = $this->form->addFieldset('main', []);



    $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
    $timeFormat = $this->_localeDate->getTimeFormat(\IntlDateFormatter::MEDIUM);
    $fieldset->addType(
        'custom_date',
        \MageWorx\OrderEditor\Block\Adminhtml\Form\Element\CustomDate::class
    );
    $fieldset->addField(
        'created_at',
        'custom_date',
        [
            'input_format' => \Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT,
            'name'         => 'created_at',
            'label'        => __('Order Date'),
            'title'        => __('Order Date'),
            'required'     => true,
            'date_format'  => $dateFormat,
            'time_format'  => $timeFormat
        ]
    );

    $statusOptions = $this->orderStatusConfig->toOptionArray();
    array_shift($statusOptions);
    $fieldset->addField(
        'status',
        'select',
        [
            'name'     => 'status',
            'label'    => __('Status'),
            'title'    => __('Status'),
            'values'   => $statusOptions,
            'required' => true,
        ]
    );

    $fieldset->addField(
        'state',
        'select',
        [
            'name'     => 'state',
            'label'    => __('State'),
            'title'    => __('State'),
            'values'   => $this->orderStateConfig->toOptionArray(),
            'required' => true,
        ]
    );

    $this->form->addFieldNameSuffix('order[info]');
    $this->form->setValues($this->getFormValues());

    return $this;
}

public function getFormValues()
{
    $order = $this->getOrder();

    return [
        'increment_id' => $order->getIncrementId(),
        'created_at'   => $order->getCreatedAt(),
        'status'       => $order->getStatus(),
        'state'        => $order->getState(),
    ];
}


public function getSubmitButtonHtml()
{
    $html = $this->getLayout()->createBlock(\Magento\Backend\Block\Widget\Button::class)
                 ->setData(
                     [
                         'id'    => 'info-submit',
                         'label' => __('Submit'),
                         'type'  => 'button',
                         'class' => 'edit primary',
                         'style' => 'margin-top: 1em; float:right;',
                     ]
                 )
                 ->toHtml();

    return $html;
}


public function getCancelButtonHtml()
{
    $html = $this->getLayout()->createBlock(\Magento\Backend\Block\Widget\Button::class)
                 ->setData(
                     [
                         'label' => __('Cancel'),
                         'id'    => 'info-cancel',
                         'class' => 'edit primary',
                         'type'  => 'button',
                         'style' => 'margin-top: 1em; float:left;',
                     ]
                 )
                 ->toHtml();

    return $html;
}


public function getNotice()
{
    return __(
        'Note that changing the order\'s details won\'t recalculate the taxes and discounts.
        You should adjust the prices manually if necessary.'
    );
}

}

3
  • Could you share which module are you trying to override?, your code is not clear enough
    – Romer Rios
    Feb 10, 2021 at 0:27
  • In this "protected function prepareForm()" i want to remove state field, this block function Feb 10, 2021 at 4:26
  • @RomerRios update the code i ' $fieldset->addField( 'state', 'select', [ 'name' => 'state', 'label' => __('State'), 'title' => __('State'), 'values' => $this->orderStateConfig->toOptionArray(), 'required' => true, ] );' Feb 10, 2021 at 4:36

1 Answer 1

0

Here is one sample example for remove field from prepareForm

In etc/di.xml

<type name="Mageplaza\Blog\Block\Adminhtml\Post\Edit\Tab\Post">
        <plugin name="recipe_post_form" type="vendor\module\Block\Adminhtml\Post\Edit\Tab\Post"/></type>

and in plugin class

<?php

declare(strict_types=1);

namespace vendor\module\Block\Adminhtml\Post\Edit\Tab;

use Mageplaza\Blog\Block\Adminhtml\Post\Edit\Tab\Post as MageplazaPost;

class Post 
{
    /**
     * Get form HTML
     *
     * @return string
     */
    public function aroundGetFormHtml(
        MageplazaPost $subject,
        \Closure $proceed
    )
    {
        $form = $subject->getForm();
        if (is_object($form)) {
            $fieldset = $form->getElement('base_fieldset');
            $fieldset->removeField('categories_ids');
            $subject->setForm($form);
        }

        return $proceed();
    }
}

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.