I'm developing extension for magento 2.1.3 ce. I want to add a link to each product in the products grid in admin-panel:
I want to have a link in this column, how to change my extension to have a link instead of plain text?
My Magento extension code (app\code\MyCompany\ExampleAdminNewPage\view\adminhtml\ui_component\product_listing.xml):
<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
<column name="sku">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Custom Field2</item>
<item name="sortOrder" xsi:type="number">75</item>
</item>
</argument>
</column>
</columns>
</listing>
in pseudo code what i want is:
<column name="{http://mysite/}"+"sku">
If the URL is internal, try this:
Step 1: config the column like this:
<column name="custom_field2" class="MyCompany\ExampleAdminNewPage\Ui\Component\Listing\Column\HTMLLink">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<!-- other configurations -->
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
</item>
</argument>
</column>
Step 2: create file <Magento_ROOT>/app/code/MyCompany\ExampleAdminNewPage/Ui/Component/Listing/Column/HtmlLink.php
use Magento\Framework\Escaper;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
class HTMLLink extends Column{
protected $escaper;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
Escaper $escaper,
array $components = [],
array $data = []
) {
$this->escaper = $escaper;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
public function prepareDataSource(array $dataSource){
if (isset($dataSource['data']['items'])) {
$fieldName = $this->getData('name');
foreach ($dataSource['data']['items'] as & $item) {
$html = 'product';
$item[$fieldName] = $this->escaper->escapeHtml($html, ['a']);
}
}
return $dataSource;
}
}
Related
I tried to add a value in my UI form which is in xml type but I didn't succeed with the methods I found.
For example: this is my code
<field name="client_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">ClientId</item>
<item name="value" xsi:type="string" translate="true">Module\Core\DataObjects</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">smartbusiness</item>
<item name="dataScope" xsi:type="string">client_id</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
<item name="min_text_length" xsi:type="number">10</item>
<item name="max_text_length" xsi:type="number">250</item>
<item name="no-whitespace" xsi:type="boolean">true</item>
</item>
</item>
</argument>
</field>
And in my custom class I have that
<?php
namespace Module\Core\DataObjects;
use Module\Core\Model\SettingsFactory;
use Magento\Framework\App\ObjectManager;
class SettingsData
{
protected array $setting = [];
public function __construct()
{
$objectManager = ObjectManager::getInstance();
$modelFactory = $objectManager->get(SettingsFactory::class);
$collection = $modelFactory->create()->getCollection();
$collection->getSelect()->limit(1);
if ($collection->getSize()) {
$this->setting = $collection->getData()[0];
}
}
public function getData()
{
return $this->getClientId();
}
}
Any ideas how I could try?
I also tried to put class="Name of my class" in value, but it didn't work
I tried the above method + to attach the class name in the class attribute
Step 1:- Create a ui_form in
view/adminhtml/ui_component/sunarc_demo_type_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/Ui/etc/ui_configuration.xsd">
<!--main part of the grid-->
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<!--Define where to find the data sources-->
<item name="provider" xsi:type="string">sunarc_demo_type_form.sunarc_demo_type_form_data_source</item>
<item name="deps" xsi:type="string">sunarc_demo_type_form.sunarc_demo_type_form_data_source</item>
</item>
<item name="label" xsi:type="string" translate="true">Add Dummy Data</item>
<item name="layout" xsi:type="array">
<item name="type" xsi:type="string">tabs</item>
</item>
<!-- <item name="buttons" xsi:type="array">
<item name="back" xsi:type="string">Sunarc\Enquiry\Block\Adminhtml\Type\Edit\BackButton</item>
<item name="reset" xsi:type="string">Sunarc\Enquiry\Block\Adminhtml\Type\Edit\ResetButton</item>
<item name="save" xsi:type="string">Sunarc\Enquiry\Block\Adminhtml\Type\Edit\SaveButton</item> -->
<!-- </item> -->
</argument>
<dataSource name="sunarc_demo_type_form_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">Sunarc\Demo\Model\Ui\DataProvider</argument>
<argument name="name" xsi:type="string">sunarc_demo_type_form_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">entity_id</argument>
<argument name="requestFieldName" xsi:type="string">id</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
<item name="submit_url" xsi:type="url" path="*/*/save"/>
</item>
</argument>
</dataSource>
<fieldset name="demo">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Add/Edit Type</item>
</item>
</argument>
<!-- This field represents form id and is hidden -->
<field name="entity_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="visible" xsi:type="boolean">false</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">type</item>
</item>
</argument>
</field>
<field name="demo_data">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Demo Data</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="dataType" xsi:type="string">text</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">type</item>
</item>
</argument>
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
</settings>
</field>
<!-- IF YOU WANT TO SHOW STATIC VALUE -->
<field name="text_example" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="default" xsi:type="string">SAMPLE TEXT HERE</item>
</item>
</argument>
<settings>
<label translate="true">SAMPLE TEXT</label>
<visible>true</visible>
<disabled>false</disabled>
<elementTmpl>ui/form/element/text</elementTmpl>
</settings>
</field>
</fieldset>
</form>
Step 2:- Create a DataSource File in Model/Ui/DataProvider.php
<?php
namespace Sunarc\Demo\Model\Ui;
use Sunarc\Demo\Model\ResourceModel\Demo\CollectionFactory;
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
protected $loadedData;
/**
* #param string $name
* #param string $primaryFieldName
* #param string $requestFieldName
* #param CollectionFactory $typeCollectionFactory
* #param array $meta
* #param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
CollectionFactory $typeCollectionFactory,
array $meta = [],
array $data = []
) {
$this->collection = $typeCollectionFactory->create();
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}
/**
* Get data
*
* #return array
*/
public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
$items = $this->collection->getItems();
foreach ($items as $item) {
$this->loadedData[$item->getData('entity_id')]['demo']['demo_data'] = $item->getData('demo_data');
}
return $this->loadedData;
// return [];
}
}
Click Here to check the input
I created a module to add 2 columns in sales grid, shipping_information and tracking number.
The columns appears but when I tried to filter by Order ID in admin order page, example with 973, I got this error
main.CRITICAL: Item (Magento\Framework\View\Element\UiComponent\DataProvider\Document\Interceptor) with the same ID "6245" already exists. {"exception":"[object] (Exception(code: 0): Item (Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\Document\\Interceptor) with the same ID \"6245\" already exists. at /home/xxxx/public_html/vendor/magento/framework/Data/Collection.php:404)"}
Also this error
Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous
But when I tried with another ID, the error disappear
In Vendor/Module/Model/ResourceModel/Order/Grid/Collection.php
namespace Wetag\AdditionalOrderFields\Model\ResourceModel\Order\Grid;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;
use Psr\Log\LoggerInterface as Logger;
/**
* Order grid extended collection
*/
class Collection extends OriginalCollection
{
protected $helper;
public function __construct(
EntityFactory $entityFactory,
Logger $logger,
FetchStrategy $fetchStrategy,
EventManager $eventManager,
$mainTable = 'sales_order_grid',
$resourceModel = \Magento\Sales\Model\ResourceModel\Order::class
)
{
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
}
protected function _renderFiltersBefore()
{
$joinTable = $this->getTable('sales_order');
$joinTable_track = $this->getTable('sales_shipment_track');
$this->getSelect()->joinLeft($joinTable, 'main_table.entity_id = sales_order.entity_id', ['shipping_information'])->distinct();
$this->getSelect()->joinLeft($joinTable_track, 'sales_order.entity_id = sales_shipment_track.order_id', ['track_number'])->distinct();
parent::_renderFiltersBefore();
}
protected function _initSelect() {
$this->addFilterToMap('increment_id', 'main_table.increment_id');
$this->addFilterToMap('status', 'main_table.status');
parent::_initSelect();
return $this;
}
}
In Vendor/Module/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="sales_order_grid_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Order\Grid\Collection</item>
</argument>
</arguments>
</type>
<type name="Vendor\Module\Model\ResourceModel\Order\Grid\Collection">
<arguments>
<argument name="mainTable" xsi:type="string">sales_order_grid</argument>
<argument name="resourceModel" xsi:type="string">Magento\Sales\Model\ResourceModel\Order</argument>
</arguments>
</type>
</config>
In etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0">
<sequence>
<module name="Magento_sales" />
</sequence>
</module>
</config>
In Vendor/Module/view/adminhtml/ui_component/sales_order_grid.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="sales_order_columns">
<column name="shipping_information">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Shipping method</item>
</item>
</argument>
</column>
<column name="track_number">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Tracking number</item>
</item>
</argument>
</column>
</columns>
</listing>
I like to use the field shipping_information in order_sales_grid table, I didn’t get it without making a left join, I like to select this field without join
I solved the issue by grouping by entity_id ->group('main_table.entity_id') to remove duplicaded rows and delete LeftJoin to Sales_order table since shipping_information already exists in sales_order_grid table
protected function _renderFiltersBefore()
{
$joinTable_track = $this->getTable('sales_shipment_track');
$this->getSelect()->joinLeft($joinTable_track, 'main_table.entity_id = sales_shipment_track.order_id', ['track_number'])->group('main_table.entity_id');
parent::_renderFiltersBefore();
}
How can I load the data from customer DataProvider plugin into my ui_component dynamicRows?
I have created a new tab in the admin customer edit form, and added my ui_component, which seems to work well. But I can't populate the data already stored in my database.
I have tried to add xml for DataSource, but I think that has conflict with original customer_form.xml DataSource "customer_form.customer_form_data_source" because when I add DataSource, the page no longer loads.
dataSource XML:
<dataSource name="mycustomrows">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">MyVendor\MyModule\Model\Customer\DataProvider</argument>
<argument name="primaryFieldName" xsi:type="string">id</argument>
<argument name="requestFieldName" xsi:type="string">id</argument>
<argument name="meta" xsi:type="array">
<item name="myshipping" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">My Custom Rows</item>
</item>
</item>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
</item>
</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
</item>
</argument>
</dataSource>
dataProvider for dataSource:
<?php
namespace MyVendor\MyModule\Model\Customer;
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
protected $loadedData;
protected $collectionFactory;
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
\MyVendor\MyModule\Model\ResourceModel\Row\Collection $collection,
\MyVendor\MyModule\Model\ResourceModel\Row\CollectionFactory $collectionFactory,
array $meta = [],
array $data = []
) {
$this->collection = $collection;
$this->collectionFactory = $collectionFactory;
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}
public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
$collection = $this->collectionFactory->create();
// add Filter for Customer ID
//->setOrder('position', 'ASC');
$items = $collection->getItems();
foreach ($items as $item) {
$this->loadedData[$item->getId()] = $item->getData();
}
return $this->loadedData;
}
}
I believe the data has to be loaded with Plugin for "Magento\Customer\Model\Customer\DataProvider" as I am doing below, but I can't seem to get populate rows on load.
view/adminhtml/ui_component/customer_form.xml
<?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="mycustomrows">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">My Custom Rows</item>
<item name="sortOrder" xsi:type="number">10</item>
</item>
</argument>
<dynamicRows name="mycustomrows">
<settings>
<addButtonLabel translate="true">Add Row</addButtonLabel>
<additionalClasses>
<class name="admin__field-wide">true</class>
</additionalClasses>
<componentType>dynamicRows</componentType>
</settings>
<container name="record" component="Magento_Ui/js/dynamic-rows/record">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="isTemplate" xsi:type="boolean">true</item>
<item name="is_collection" xsi:type="boolean">true</item>
<item name="componentType" xsi:type="string">container</item>
<item name="positionProvider" xsi:type="string">position</item>
</item>
</argument>
<field name="mycustomrows_select1" formElement="select" sortOrder="10">
<settings>
<dataType>text</dataType>
<label translate="true">Select Field 1</label>
<disabled>false</disabled>
<dataScope>mycustomrows_select1</dataScope>
</settings>
<formElements>
<select>
<settings>
<options class="MyVendor\MyModule\Model\Source\SelectFieldOne"/>
</settings>
</select>
</formElements>
</field>
<field name="mycustomrows_input1" sortOrder="20" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">customer</item>
</item>
</argument>
<settings>
<label translate="true">Text Field 1</label>
<dataType>text</dataType>
<dataScope>mycustomrows_text1</dataScope>
</settings>
</field>
<actionDelete sortOrder="30">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="componentType" xsi:type="string">actionDelete</item>
<item name="dataType" xsi:type="string">text</item>
<item name="fit" xsi:type="boolean">false</item>
<item name="label" xsi:type="string">Actions</item>
<item name="sortOrder" xsi:type="string">500</item>
<item name="additionalClasses" xsi:type="string">data-grid-actions-cell</item>
<item name="template" xsi:type="string">Magento_Backend/dynamic-rows/cells/action-delete</item>
</item>
</argument>
</actionDelete>
</container>
</dynamicRows>
</fieldset>
</form>
etc/adminhtml/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Customer\Model\Customer\DataProvider">
<plugin name="customer_get_mycustomrows" type="MyVendor\MyModule\Model\Customer\DataProvider" sortOrder="5" />
</type>
</config>
Model/Customer/DataProvider.php
<?php
namespace MyVendor\MyModule\Model\Customer;
class DataProvider
{
protected $collection;
protected $collectionFactory;
protected $loadedData;
public function __construct(
\MyVendor\MyModule\Model\ResourceModel\Row\Collection $collection,
\MyVendor\MyModule\Model\ResourceModel\Row\CollectionFactory $collectionFactory,
array $data = []
) {
$this->collection = $collection;
$this->collectionFactory = $collectionFactory;
}
public function afterGetData(\Magento\Customer\Model\Customer\DataProvider $subject, $result)
{
if($result){
$customer_id = key($result);
$customerData = $result[$customer_id]['customer'];
if(is_null($this->loadedData)) {
$this->loadedData = array();
$collection = $this->collectionFactory->create(); //->setOrder('position', 'ASC');
$items = $collection->getItems();
foreach ($items as $item) {
$this->loadedData[] = $item->getData();
}
}
$result[$customer_id]['customer']["mycustomrows"] = $this->loadedData;
}
return $result;
}
}
I can't seem to get any records populated when I load a customer edit page.
I'm trying to make an anchor link in the product grid view.
I have tried this using UI component, below are the detailed view
xml code:
<column name="column_name" class="Vendor\Module\Ui\Component\Listing\Column\link">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Label</item>
<item name="sortOrder" xsi:type="number">50</item>
</item>
</argument>
</column>
datasource code:
$html = ''. __('Label').'';
$item[$this->getData('name')] = $html;
Getting html like this: Label
I want 'Label' with the anchor link.
I can not understand why the error
Could not save the brand: Notice: Array to string conversion in C:\OpenServer\domains\localhost\2016\mercury_61189_222_007\vendor\magento\zendframework1\library\Zend\Db\Statement\Pdo.php on line 228
on the idea of all done right, here is the code of the module
<field name="website_id_2">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Magento\Store\Model\ResourceModel\Website\Collection</item>
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Website 2</item>
<item name="formElement" xsi:type="string">multiselect</item>
<item name="source" xsi:type="string">page</item>
<item name="dataScope" xsi:type="string">website_id_2</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
</item>
</argument>
</field>
Can someone know the nuances?
I decided my question.
It was necessary for the save function to execute () to write 2 lines to preserve the array
$data['website_id_2']=implode(',',$data['website_id_2']);
$model->setData($data)->setId($this->getRequest()->getParam('id'));