12

I added a new form element type called Company\Module\Data\Form\Element\PdfButton, when it is version EE 2.0.7 and used this element in a custom tab(added by overriding Magento\Catalog\Block\Adminhtml\Product\Edit\Tabs) of product edit admin page.

After upgrading to Magento EE 2.1, the custom tab vanished. To make it appear, I added a modifier to make a new tab in product page. I successfully added a new tab by copying the modifier in vendor file and inject it to my module.

But, when I want to use my custom element. So, in the children field of the meta array, I added following code:

$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',
                        ],
                    ],
                ],
            ];

after this, I copied the Form\Element\Wysiwyg and paste it as Form\Element\Wysiwyg2. In the Wysiwyg2 Class:

<?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;
    }}

But there is an error and I don't know where I should add the name to that.

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

As far as my understanding, you are trying to add the whole new Ui component type which call wysiwyg2.

But unfortunately, there is a known issue about adding new Ui Component type (yes, just another one). You can check the original issue overhere.

Let take a look deeper into how Magento 2 dealing with the Ui components in product form.

vendor/magento/module-catalog/Ui/DataProvider/Product/Form/Modifier/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;
}

And inside public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupCode, $sortOrder)

Line 633 (may be different for each version)

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

As you can see, customizeWysiwyg() hard-coded formElement to wysiwyg.

If you want to wysiwyg2 working, you need write a plugin for setupAttributeMeta() to add something like $meta = $this->customizeWysiwyg2($attribute, $meta);

But I do not encourage this, you can just create a preference for \Magento\Ui\Component\Form\Element\Wysiwyg, then inside the constructor you can do something like

/**
 * 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);
}

By this way, you can have your own jsComponent, knockout templates..etc and ready to customise to whatever you want.

Hope this helps :)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.