0

I have a form, see image below.

enter image description here

I have the following data in the database, see image below

enter image description here

I originally wanted to have the structure like below in the custom table

> id | meta_key   |  meta_value
> ------------------------------- 
> 1  | first_name |  Absolute
> ------------------------------
> 2  | last_name  |  Idiot

But I don't know how to do this with ui_component (update, retrieve data)? (if someone knows how to do this. please provide some info)

So I resorted to what I know of

My current problem, when I load the form, I don't want to pass id/1 in my URL instead, I want the data from id 1 to be loaded by default.

Assuming the only needed codes are ui_component_form and data provider I have provided them below.

DataProvider.php

<?php
namespace Absolute\Idiot\Model\Sample1;
use Absolute\Idiot\Model\ResourceModel\Sample1\CollectionFactory;
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $contactCollectionFactory,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $contactCollectionFactory->create();
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }

        $items = $this->collection->getItems();
        $this->loadedData = array();
        foreach ($items as $user) {
            $this->loadedData[$user->getId()]['sample1'] = $user->getData();
        }

        return $this->loadedData;

    }
}

idiot_sample1_form.xml (ui_component)


     <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">idiot_sample1_form.sample1_form_data_source</item>
            <item name="deps" xsi:type="string">idiot_sample1_form.sample1_form_data_source</item>
        </item>
      <item name="label" xsi:type="string" translate="true">Sample1</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">idiot_sample1_form</item>
        </item>

      <item name="template" xsi:type="string">templates/form/collapsible</item>

        <item name="buttons" xsi:type="array">
            <item name="save" xsi:type="array">
                <item name="name" xsi:type="string">save</item>
                <item name="label" xsi:type="string" translate="true">Save Sample Data</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">*/*/saveAction</item>
            </item>
        </item>

    </argument>


    <dataSource name="sample1_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Absolute\Idiot\Model\Sample1\DataProvider</argument>
            <argument name="name" xsi:type="string">sample1_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">sample_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>
        </argument>
    </dataSource>

    <fieldset name="sample1_details">

        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="label" xsi:type="string" translate="true">Sample1 Details</item>
                <item name="sortOrder" xsi:type="number">20</item>
            </item>
        </argument>

        <field name="sample_id" formElement="input" sortOrder="10">
            <settings>
                <visible>true</visible>
                <elementTmpl>ui/form/element/input</elementTmpl>
                <label translate="true">Sample Id</label>
            </settings>
        </field>

        <field name="first_name" formElement="input" sortOrder="20">
            <settings>
                <visible>true</visible>
                <elementTmpl>ui/form/element/input</elementTmpl>
                <label translate="true">First Name</label>
            </settings>
        </field>

        <field name="last_name" formElement="input" sortOrder="30">
            <settings>
                <visible>true</visible>
                <elementTmpl>ui/form/element/input</elementTmpl>
                <label translate="true">Last Name</label>
            </settings>
        </field>



    </fieldset>
</form>

1 Answer 1

0

In your listing.xml uicompnent file set actionsColumn node to set URL of edit and delete.

here my view/adminhtml/ui_component/sample_listing.xml

<actionsColumn name="actions" class="Vendor\Module\Ui\Component\Listing\Column\PR\Actions">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">true</item>
                    <item name="resizeDefaultWidth" xsi:type="string">107</item>
                    <item name="indexField" xsi:type="string">id</item>
                    <item name="sortOrder" xsi:type="number">7</item>
                </item>
            </argument>
        </actionsColumn>

set correct class and ID in this file class="Vendor\Module\Ui\Component\Listing\Column\PR\Actions" and indexField ID

Vendor\Module\Ui\Component\Listing\Column\PR\Actions
<?php
namespace Vendor\Module\Ui\Component\Listing\Column\PR;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder;
use Magento\Framework\UrlInterface;

class Actions extends Column
{

    const URL_PATH_EDIT = 'pbvreview/productreview/edit';
    const URL_PATH_DELETE = 'pbvreview/productreview/delete';

    protected $actionUrlBuilder;
    protected $urlBuilder;
    private $editUrl;

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        UrlBuilder $actionUrlBuilder,
        UrlInterface $urlBuilder,
        array $components = [],
        array $data = [],
        $editUrl = self::URL_PATH_EDIT
    ) {
        $this->urlBuilder = $urlBuilder;
        $this->actionUrlBuilder = $actionUrlBuilder;
        $this->editUrl = $editUrl;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $name = $this->getData('name');
                if (isset($item['id'])) {
                    $item[$name]['edit'] = [
                        'href' => $this->urlBuilder->getUrl($this->editUrl, ['id' => $item['id']]),
                        'label' => __('Edit')
                    ];
                    $item[$name]['delete'] = [
                        'href' => $this->urlBuilder->getUrl(self::URL_PATH_DELETE, ['id' => $item['id']]),
                        'label' => __('Delete'),
                        'confirm' => [
                            'title' => __('Delete ${ $.$data.title }'),
                            'message' => __('Are you sure you wan\'t to delete a ${ $.$data.title } record?')
                        ]
                    ];
                }
            }
        }

        return $dataSource;
    }
}
6
  • Thankyou for answer. I am relatively new to magento. But can i pass the id directly from the form via xml directly to the dataprovider. I don't want to list the data in grid or table. May 17, 2020 at 7:54
  • try to implement Ui Component step by step so you can learn properly
    – Devidas
    May 17, 2020 at 8:01
  • techjeffyu.com/blog/…
    – Devidas
    May 17, 2020 at 8:03
  • Thanks for the links. Sorry if i am wrong, After checking your code i think what you have provided is the code for grid view/ Table. I don't want table view with action columns like edit/delete. when i click the admin menu of my custom extension, a form is provided, Please look at second structure of database i have provided in the original question. Maybe u will understand what i meant. Its like a configuration form for an extension so i don't need grid view. May 18, 2020 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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