Magento 2.1 การเพิ่มปัญหาประเภทองค์ประกอบใหม่


16

ฉันเพิ่มประเภทองค์ประกอบแบบฟอร์มใหม่ที่เรียกว่าCompany\Module\Data\Form\Element\PdfButtonเมื่อเป็นversion EE 2.0.7และใช้องค์ประกอบนี้ในแท็บที่กำหนดเอง (เพิ่มโดยการแทนที่Magento\Catalog\Block\Adminhtml\Product\Edit\Tabs) ของหน้าผู้ดูแลระบบแก้ไขผลิตภัณฑ์

หลังจากอัปเกรดเป็น Magento EE 2.1 แล้วแท็บที่กำหนดเองก็หายไป เพื่อให้มันปรากฏขึ้นฉันได้เพิ่มตัวปรับแต่งเพื่อสร้างแท็บใหม่ในหน้าผลิตภัณฑ์ ฉันเพิ่มแท็บใหม่โดยการคัดลอกโมดิฟายเออร์ในไฟล์ผู้ขายและฉีดไปยังโมดูลของฉัน

แต่เมื่อฉันต้องการใช้องค์ประกอบที่กำหนดเองของฉัน ดังนั้นในฟิลด์ children ของ meta array ฉันเพิ่มรหัสต่อไปนี้:

$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))

คำตอบ:


1

เท่าที่ความเข้าใจของฉันคุณกำลังพยายามที่จะเพิ่มใหม่ทั้งหมด Ui wysiwyg2ประเภทส่วนประกอบที่โทร

แต่น่าเสียดายที่มีปัญหาเกี่ยวกับการเพิ่มประเภทคอมโพเนนต์ Ui ใหม่ (ใช่อีกอันหนึ่ง) คุณสามารถตรวจสอบปัญหาเดิมoverhere

ลองดูรายละเอียดเพิ่มเติมเกี่ยวกับวิธีที่ Magento 2 จัดการกับส่วนประกอบ 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 เทมเพลตที่น่าพิศวง .. และพร้อมที่จะปรับแต่งให้เข้ากับสิ่งที่คุณต้องการ

หวังว่าจะช่วย :)

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.