Magento 2.1添加了新的组件类型问题


15

我添加了一个称为的新表单元素类型Company\Module\Data\Form\Element\PdfButtonversion EE 2.0.7并在Magento\Catalog\Block\Adminhtml\Product\Edit\Tabs产品编辑管理页面的自定义标签(通过覆盖添加)中使用了此元素。

升级到Magento EE 2.1之后,自定义标签消失了。为了使其显示,我添加了一个修饰符以在产品页面中创建一个新选项卡。通过将修改器复制到供应商文件中并将其注入到模块中,我成功添加了新选项卡。

但是,当我想使用自定义元素时。因此,在元数组的children字段中,添加了以下代码:

$children[$website['id']] = [
                'arguments' => [
                    'data' => [
                        'config' => [
                            'dataType' => Form\Element\DataType\Number::NAME,
                            'componentType' => Form\Field::NAME,
                            'formElement' => Form\Element\Wysiwyg2::NAME,
                            'description' => __($website['name']),
                            'tooltip' => $tooltip,
                            'sortOrder' => $sortOrder,
                            'dataScope' => 'website_ids.' . $website['id'],
                            'label' => "Pdf Upload",
                            'valueMap' => [
                                'true' => (string)$website['id'],
                                'false' => '0',
                            ],
                            'value' => $isChecked ? (string)$website['id'] : '0',
                        ],
                    ],
                ],
            ];

之后,我复制了Form\Element\Wysiwyg并将其粘贴为Form\Element\Wysiwyg2。在Wysiwyg2类中:

<?php

/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Ui\Component\Form\Element;

use Magento\Framework\Data\Form\Element\Editor;
use Magento\Framework\Data\Form;
use Magento\Framework\Data\FormFactory;
use Magento\Framework\DataObject;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Wysiwyg\ConfigInterface;

/**
 * Class Input
 */
class Wysiwyg2 extends AbstractElement
{
    const NAME = 'wysiwyg2';

    /**
     * @var Form
     */
    protected $form;

    /**
     * @var Editor
     */
    protected $editor;

    /**
     * @param ContextInterface $context
     * @param FormFactory $formFactory
     * @param ConfigInterface $wysiwygConfig
     * @param array $components
     * @param array $data
     * @param array $config
     */
    public function __construct(
        ContextInterface $context,
        FormFactory $formFactory,
        ConfigInterface $wysiwygConfig,
        array $components = [],
        array $data = [],
        array $config = []
    ) {
        $wysiwygConfigData = isset($config['wysiwygConfigData']) ? $config['wysiwygConfigData'] : [];
        $this->form = $formFactory->create();
        $this->editor = $this->form->addField(
            $context->getNamespace() . '_' . $data['name'],
            'Magento\Framework\Data\Form\Element\Editor',
            [
                'force_load' => true,
                'rows' => 20,
                'name' => $data['name'],
                'config' => $wysiwygConfig->getConfig($wysiwygConfigData),
                'wysiwyg' => isset($config['wysiwyg']) ? $config['wysiwyg'] : null,
            ]
        );
        $data['config']['content'] = $this->editor->getElementHtml();

        parent::__construct($context, $components, $data);
    }

    /**
     * Get component name
     *
     * @return string
     */
    public function getComponentName()
    {
        return static::NAME;
    }}

但是有一个错误,我不知道应该在哪里添加名称。

1 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): The requested component ("wysiwyg2") is not found. Before using, you must add the implementation.

Exception #0 (Magento\Framework\Exception\LocalizedException): The requested component ("wysiwyg2") is not found. Before using, you must add the implementation.
#0 /var/www/vhosts/plchk/vendor/magento/module-ui/Model/Manager.php(207): Magento\Framework\View\Element\UiComponent\Config\Provider\Component\Definition->getComponentData('wysiwyg2')
#1 /var/www/vhosts/plchk/vendor/magento/framework/View/Element/UiComponentFactory.php(187): Magento\Ui\Model\Manager->createRawComponentData('wysiwyg2')
#2 /var/www/vhosts/plchk/vendor/magento/module-ui/Component/Form/Field.php(82): Magento\Framework\View\Element\UiComponentFactory->create(1, 'wysiwyg2', Array)
#3 /var/www/vhosts/plchk/vendor/magento/framework/View/Layout/Generator/UiComponent.php(148): Magento\Ui\Component\Form\Field->prepare()
#4 /var/www/vhosts/plchk/vendor/magento/framework/View/Layout/Generator/UiComponent.php(145): Magento\Framework\View\Layout\Generator\UiComponent->prepareComponent(Object(Magento\Ui\Component\Form\Field))

Answers:


0

据我了解,您正在尝试添加名为的全新Ui组件类型wysiwyg2

但不幸的是,存在有关添加新的Ui Component类型的问题(是,只是另一种)。您可以在此处查看原始问题。

让我们更深入地了解Magento 2如何处理产品形式的Ui组件。

供应商/ magento /模块目录/ Ui / DataProvider /产品/表单/修饰符/Eav.php

/**
 * Add wysiwyg properties
 *
 * @param ProductAttributeInterface $attribute
 * @param array $meta
 * @return array
 */
private function customizeWysiwyg(ProductAttributeInterface $attribute, array $meta)
{
    if (!$attribute->getIsWysiwygEnabled()) {
        return $meta;
    }

    $meta['arguments']['data']['config']['formElement'] = WysiwygElement::NAME;
    $meta['arguments']['data']['config']['wysiwyg'] = true;
    $meta['arguments']['data']['config']['wysiwygConfigData'] = [
        'add_variables' => false,
        'add_widgets' => false,
        'add_directives' => true,
        'use_container' => true,
        'container_class' => 'hor-scroll',
    ];

    return $meta;
}

在里面 public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupCode, $sortOrder)

第633行(每个版本可能有所不同)

        case 'textarea':
            $meta = $this->customizeWysiwyg($attribute, $meta);
            break;

如您所见,customizeWysiwyg()硬编码formElementwysiwyg

如果您想wysiwyg2工作,则需要编写一个插件setupAttributeMeta()来添加类似$meta = $this->customizeWysiwyg2($attribute, $meta);

但我不鼓励这样做,您可以为创建一个首选项\Magento\Ui\Component\Form\Element\Wysiwyg,然后在构造函数中可以执行以下操作

/**
 * Wysiwyg constructor.
 *
 * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context
 * @param \Magento\Framework\Data\FormFactory                          $formFactory
 * @param \Magento\Ui\Component\Wysiwyg\ConfigInterface                $wysiwygConfig
 * @param array                                                        $components
 * @param array                                                        $data
 * @param array                                                        $config
 */
public function __construct(
    ContextInterface $context,
    FormFactory $formFactory,
    ConfigInterface $wysiwygConfig,
    array $components = [],
    array $data = [],
    array $config = []
) {
    // Override the component for the WYSIWYG
    // This is not done using definition.xml due to https://github.com/magento/magento2/issues/5647
    $data['config']['component'] = 'Stackoverflow_Toan/js/form/element/wysiwyg';

    // Override the templates to include our KnockoutJS code
    $data['config']['template'] = 'Stackoverflow_Toan/wysiwyg';
    $data['config']['elementTmpl'] = 'Stackoverflow_Toan/wysiwyg';

    parent::__construct($context, $formFactory, $wysiwygConfig, $components, $data, $config);
}

通过这种方式,您可以拥有自己的jsComponent,敲除模板..etc并准备自定义为所需的任何内容。

希望这可以帮助 :)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.