'Magento 2 overriding ui component product form xml issue
I have applied this solution for adding value to the input https://magento.stackexchange.com/questions/236797/set-default-value-for-date-in-ui-component
but I have two issues
here is my code:
<?php
namespace Bnaia\SkuVendor\Ui\Component\Form\Element;
use Magento\Framework\App\Request\Http;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Vnecoms\Vendors\Model\Session;
class Input extends \Magento\Ui\Component\Form\Element\Input
{
protected $_vendorSession;
protected $_urlInterface;
protected $_product;
public function __construct(
\Magento\Framework\View\Element\UiComponent\context $context,
Http $urlInterface,
ProductRepositoryInterface $productRepository,
Session $vendorSession
) {
$this->_vendorSession = $vendorSession;
$this->_urlInterface = $urlInterface;
$this->_productRepository = $productRepository;
parent::__construct($context);
}
public function prepare()
{
parent::prepare();
$vendorId = $this->_vendorSession->getVendor()->getVendorId();
$product_id = $this->_urlInterface->getParam('id');
$product = $this->_productRepository->getById($product_id);
$sku = $product->getSku();
$config = $this->getData('config');
\Magento\Framework\App\ObjectManager::getInstance()
->get(\Psr\Log\LoggerInterface::class)->debug('message ' . print_r($sku . '_' .$vendorId,true));
if(isset($config['dataScope']) && $config['dataScope']=='sku'){
$config['default']= $sku .'_'. $vendorId;
$this->setData('config', (array)$config);
}
}
}
first: when I used constructor to use methods from other modules the input filed disappeared. but It didn't disappear when I have used the same methods using objectmanager
this is sample of working code
<?php
namespace Bnaia\SkuVendor\Ui\Component\Form\Element;
//use Vnecoms\Vendors\Model\Session;
class Input extends \Magento\Ui\Component\Form\Element\Input
{
public function prepare()
{
parent::prepare();
$urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\Request\Http');
$vendorIds = \Magento\Framework\App\ObjectManager::getInstance()->get('Vnecoms\Vendors\Model\Session');
$vendor = $vendorIds->getVendor();
$product_id = $urlInterface->getParam('id');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);
$sku = $product->getSku();
$vendorId = $product->getVendorId();
$config = $this->getData('config');
\Magento\Framework\App\ObjectManager::getInstance()
->get(\Psr\Log\LoggerInterface::class)->debug('message' . print_r( ' test '. $vendor . $vendorId,true));
if(isset($config['dataScope']) && $config['dataScope']=='sku'){
$config['default']= $sku .'_' ;
$this->setData('config', (array)$config);
}
}
}
and as we know using object manager is not recommended calling a class methods.
second issue.
I got the SKU input duplicated I can't override the existing sku input value
here is the xml form
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="product-details">
<field name="sku" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="class" xsi:type="string">Bnaia\SkuVendor\Ui\Component\Form\Element\Input</item>
<item name="sortOrder" xsi:type="number">10</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
<item name="label" xsi:type="string" translate="true">SKU</item>
<item name="dataScope" xsi:type="string">sku</item>
<item name="scopeLabel" xsi:type="string">[GLOBAL]</item>
</item>
</argument>
</field>
</fieldset>
</form>
I need guide thanks for help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
