首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Magento 2覆盖自定义模块

Magento 2覆盖自定义模块
EN

Stack Overflow用户
提问于 2019-03-01 19:44:45
回答 1查看 1.1K关注 0票数 0

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

这是Prince/Productattach/Block/Adminhtml/Productattach/Edit/Tab/Main.php上的第三方文件

代码语言:javascript
复制
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,以覆盖该类。

代码语言:javascript
复制
<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中添加的额外字段

代码语言:javascript
复制
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);
}
}

然而,我一直收到错误

代码语言:javascript
复制
Incompatible argument type: Required type: \Magento\Store\Model\System\Store. Actual type: array; 

我尝试过刷新缓存和重新建立索引,但没有成功。有人能告诉我我哪里做错了吗?也乐于倾听任何替代的方式来完成同样的事情。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-03-04 09:03:44

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54943992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档