Magento 2覆盖自定义模块

问题描述 投票:0回答:1

总而言之,我试图覆盖第三方模块,在管理员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"/>

然后,在Vendor / Filecategory / Block / 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; 

我尝试过flush cache和reindex,但没有运气。请有人能告诉我这里做错了什么吗?也乐于听取完成同样事情的任何其他方法。

提前致谢。

php magento magento2.2
1个回答
0
投票

我设法通过创建一个插件类而不是覆盖整个类来完成它。我已经附上了我所关注的帖子,任何有兴趣将表单字段添加到附加到第三部分模块的现有表单的人。

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

© www.soinside.com 2019 - 2024. All rights reserved.