Magento 2覆盖自定义模块

时间:2019-03-01 11:44:45

标签: php magento magento2.2

因此,总而言之,我试图覆盖第三方模块,以便在admin Ui上包含一个额外的下拉菜单

此处包含第三方文件     Prince / Productattach / Block / Adminhtml / Productattach / Edit / Tab / Main.php

namespace Prince\Productattach\Block\Adminhtml\Productattach\Edit\Tab;

 /**
 *Class Main
 *@package Prince\Productattach\Block\Adminhtml\Productattach\Edit\Tab
 */

 class Main extends \Magento\Backend\Block\Widget\Form\Generic implements 
 \Magento\Backend\Block\Widget\Tab\TabInterface
    {
/**
 * @var \Magento\Store\Model\System\Store
 */
private  $systemStore;

/**
 * @var \Magento\Customer\Model\ResourceModel\Group\Collection
 */
private $customerCollection;

/**
 * Main constructor.
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Framework\Registry $registry
 * @param \Magento\Framework\Data\FormFactory $formFactory
 * @param \Magento\Store\Model\System\Store $systemStore
 * @param \Magento\Customer\Model\ResourceModel\Group\Collection $customerCollection
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Store\Model\System\Store $systemStore,
    \Magento\Customer\Model\ResourceModel\Group\Collection $customerCollection,
    array $data = []
) {
    $this->_systemStore = $systemStore;
    $this->_customerCollection = $customerCollection;
    parent::__construct($context, $registry, $formFactory, $data);
}

/**
 * Prepare form
 *
 * @return $this
 */
public function _prepareForm()
{

    $model = $this->_coreRegistry->registry('productattach');

    /*
     * Checking if user have permissions to save information
     */
    if ($this->_isAllowedAction('Prince_Productattach::save')) {
        $isElementDisabled = false;
    } else {
        $isElementDisabled = true;
    }

    /** @var \Magento\Framework\Data\Form $form */
    $form = $this->_formFactory->create();

    $form->setHtmlIdPrefix('productattach_main_');

    $fieldset = $form->addFieldset(
        'base_fieldset',
        ['legend' => __('Attachment Information')]
    );

    $customerGroup = $this->customerCollection->toOptionArray();

    if ($model->getId()) {
        $fieldset->addField('productattach_id', 'hidden', ['name' => 'productattach_id']);
    }

    $fieldset->addField(
        'name',
        'text',
        [
            'name' => 'name',
            'label' => __('Attachment Name'),
            'title' => __('Attachment Name'),
            'required' => true,
            'disabled' => $isElementDisabled
        ]
    );

    $fieldset->addField(
        'description',
        'textarea',
        [
            'name' => 'description',
            'label' => __('Description'),
            'title' => __('Description'),
            'disabled' => $isElementDisabled
        ]
    );

    $fieldset->addField(
        'files',
        'file',
        [
            'name' => 'file',
            'label' => __('File'),
            'title' => __('File'),
            'required' => false,
            'note' => 'File size must be less than 2 Mb.', // TODO: show ACCTUAL file-size
            'disabled' => $isElementDisabled
        ]
    );

    $fieldset->addType(
        'uploadedfile',
        \Prince\Productattach\Block\Adminhtml\Productattach\Renderer\FileIconAdmin::class
    );

    $fieldset->addField(
        'file',
        'uploadedfile',
        [
            'name'  => 'uploadedfile',
            'label' => __('Uploaded File'),
            'title' => __('Uploaded File'),

        ]
    );

    $fieldset->addField(
        'url',
        'text',
        [
            'name' => 'url',
            'label' => __('URL'),
            'title' => __('URL'),
            'required' => false,
            'disabled' => $isElementDisabled,
            'note' => 'Upload file or Enter url'
        ]
    );

    $fieldset->addField(
        'customer_group',
        'multiselect',
        [
            'name' => 'customer_group[]',
            'label' => __('Customer Group'),
            'title' => __('Customer Group'),
            'required' => true,
            'value' => [0,1,2,3], // todo: preselect ALL customer groups, not just 0-3
            'values' => $customerGroup,
            'disabled' => $isElementDisabled
        ]
    );

    $fieldset->addField(
        'store',
        'multiselect',
        [
            'name' => 'store[]',
            'label' => __('Store'),
            'title' => __('Store'),
            'required' => true,
            'value' => [0],
            'values' => $this->systemStore->getStoreValuesForForm(false, true),
            'disabled' => $isElementDisabled
        ]
    );

    $fieldset->addField(
        'active',
        'select',
        [
            'name' => 'active',
            'label' => __('Active'),
            'title' => __('Active'),
            'value' => 1,
            'options' => ['1' => __('Yes'), '0' => __('No')],
            'disabled' => $isElementDisabled
        ]
    );

    $this->_eventManager->dispatch('adminhtml_productattach_edit_tab_main_prepare_form', ['form' => $form]);

    if ($model->getId()) {
        $form->setValues($model->getData());
    }

    $this->setForm($form);

    return parent::_prepareForm();
}

/**
 * Prepare label for tab
 *
 * @return string
 */
public function getTabLabel()
{
    return __('Attachment Information');
}

/**
 * Prepare title for tab
 *
 * @return string
 */
public function getTabTitle()
{
    return __('Attachment Information');
}

/**
 * {@inheritdoc}
 */
public function canShowTab()
{
    return true;
}

/**
 * {@inheritdoc}
 */
public function isHidden()
{
    return false;
}

/**
 * Check permission for passed action
 *
 * @param string $resourceId
 * @return bool
 */
public function _isAllowedAction($resourceId)
{
    return $this->_authorization->isAllowed($resourceId);
}
}

我已经在模块设置和di.xml中配置了通常的加号,以覆盖该类。

<preference for="Prince\Productattach\Block\Adminhtml\Productattach\Edit\Tab" type="Vendor\Filecategory\Block\Adminhtml\Productattach\Edit\Tab"/>

然后是带有名称空间的类的精确副本,并在以下位置添加我的额外字段 供应商/文件类别/阻止/Adminhtml/Productattach/Edit/Tab/Main.php

namespace Vendor\Filecategory\Block\Adminhtml\Productattach\Edit\Tab;

use \Prince\Productattach\Block\Adminhtml\Productattach\Edit\Tab\Main as Main;


 class MainExt extends Main
 {
/**
 * @var \Magento\Store\Model\System\Store
 */
private  $systemStore;

/**
 * @var \Magento\Customer\Model\ResourceModel\Group\Collection
 */
private $customerCollection;

/**
 * Main constructor.
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Framework\Registry $registry
 * @param \Magento\Framework\Data\FormFactory $formFactory
 * @param \Magento\Store\Model\System\Store $systemStore
 * @param \Magento\Customer\Model\ResourceModel\Group\Collection $customerCollection
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Store\Model\System\Store $systemStore,
    \Magento\Customer\Model\ResourceModel\Group\Collection $customerCollection,
    array $data = []
) {
    $this->systemStore = $systemStore;
    $this->customerCollection = $customerCollection;
    parent::__construct($context, $registry, $formFactory, $data, $systemStore);
}

/**
 * Prepare form
 *
 * @return $this
 */
public function _prepareForm()
{

    $model = $this->_coreRegistry->registry('productattach');

    /*
     * Checking if user have permissions to save information
     */
    if ($this->_isAllowedAction('Prince_Productattach::save')) {
        $isElementDisabled = false;
    } else {
        $isElementDisabled = true;
    }

    /** @var \Magento\Framework\Data\Form $form */
    $form = $this->_formFactory->create();

    $form->setHtmlIdPrefix('productattach_main_');

    $fieldset = $form->addFieldset(
        'base_fieldset',
        ['legend' => __('Attachment Information')]
    );

    $customerGroup = $this->customerCollection->toOptionArray();

    if ($model->getId()) {
        $fieldset->addField('productattach_id', 'hidden', ['name' => 'productattach_id']);
    }

    $fieldset->addField(
        'name',
        'text',
        [
            'name' => 'name',
            'label' => __('Attachment Name'),
            'title' => __('Attachment Name'),
            'required' => true,
            'disabled' => $isElementDisabled
        ]
    );

    $fieldset->addField(
        'Category',
        'select',
        [
            'name' => 'Category',
            'label' => __('Category'),
            'title' => __('Category'),
            'value' => 0,
            'options' => ['0' => __('Technical Specification'), '1' => __('Installation Instructions')],
        ]
    );

    $fieldset->addField(
        'description',
        'textarea',
        [
            'name' => 'description',
            'label' => __('Description'),
            'title' => __('Description'),
            'disabled' => $isElementDisabled
        ]
    );

    $fieldset->addField(
        'files',
        'file',
        [
            'name' => 'file',
            'label' => __('File'),
            'title' => __('File'),
            'required' => false,
            'note' => 'File size must be less than 2 Mb.', // TODO: show ACCTUAL file-size
            'disabled' => $isElementDisabled
        ]
    );

    $fieldset->addType(
        'uploadedfile',
        \Prince\Productattach\Block\Adminhtml\Productattach\Renderer\FileIconAdmin::class
    );

    $fieldset->addField(
        'file',
        'uploadedfile',
        [
            'name'  => 'uploadedfile',
            'label' => __('Uploaded File'),
            'title' => __('Uploaded File'),

        ]
    );

    $fieldset->addField(
        'url',
        'text',
        [
            'name' => 'url',
            'label' => __('URL'),
            'title' => __('URL'),
            'required' => false,
            'disabled' => $isElementDisabled,
            'note' => 'Upload file or Enter url'
        ]
    );

    $fieldset->addField(
        'customer_group',
        'multiselect',
        [
            'name' => 'customer_group[]',
            'label' => __('Customer Group'),
            'title' => __('Customer Group'),
            'required' => true,
            'value' => [0,1,2,3], // todo: preselect ALL customer groups, not just 0-3
            'values' => $customerGroup,
            'disabled' => $isElementDisabled
        ]
    );

    $fieldset->addField(
        'store',
        'multiselect',
        [
            'name' => 'store[]',
            'label' => __('Store'),
            'title' => __('Store'),
            'required' => true,
            'value' => [0],
            'values' => $this->systemStore->getStoreValuesForForm(false, true),
            'disabled' => $isElementDisabled
        ]
    );

    $fieldset->addField(
        'active',
        'select',
        [
            'name' => 'active',
            'label' => __('Active'),
            'title' => __('Active'),
            'value' => 1,
            'options' => ['1' => __('Yes'), '0' => __('No')],
            'disabled' => $isElementDisabled
        ]
    );

    $this->_eventManager->dispatch('adminhtml_productattach_edit_tab_main_prepare_form', ['form' => $form]);

    if ($model->getId()) {
        $form->setValues($model->getData());
    }

    $this->setForm($form);

    return parent::_prepareForm();
}

/**
 * Prepare label for tab
 *
 * @return string
 */
public function getTabLabel()
{
    return __('Attachment Information');
}

/**
 * Prepare title for tab
 *
 * @return string
 */
public function getTabTitle()
{
    return __('Attachment Information');
}

/**
 * {@inheritdoc}
 */
public function canShowTab()
{
    return true;
}

/**
 * {@inheritdoc}
 */
public function isHidden()
{
    return false;
}

/**
 * Check permission for passed action
 *
 * @param string $resourceId
 * @return bool
 */
public function _isAllowedAction($resourceId)
{
    return $this->_authorization->isAllowed($resourceId);
}
}

但是我仍然遇到错误

Incompatible argument type: Required type: \Magento\Store\Model\System\Store. Actual type: array; 

我尝试过刷新缓存和重新索引,但是没有运气。请有人能告诉我我在这里做错了什么吗?也乐于聆听完成同一件事的其他方法。

谢谢。

1 个答案:

答案 0 :(得分:0)

我设法通过创建插件类而不是覆盖整个类来完成此操作。我已为所有有兴趣将表单字段添加到附加到第三方模块的现有表单中的人添加了我关注的帖子。

https://magento.stackexchange.com/questions/174209/magento-2-add-new-field-to-magento-user-admin-form

相关问题