6

enter image description here

field

<field name="category_id">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Vendor\Blog\Model\Config\Source\CategoryTree</item>
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Category</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="source" xsi:type="string">category_id</item> 
            <item name="component" xsi:type="string">Vender_Blog/js/components/new-category</item>
            <item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</item>
            <item name="dataScope" xsi:type="string">category_id</item>
            <item name="filterOptions" xsi:type="boolean">true</item>
            <item name="chipsEnabled" xsi:type="boolean">true</item>
            <item name="showCheckbox" xsi:type="boolean">true</item>
            <item name="disableLabel" xsi:type="boolean">true</item>
            <item name="multiple" xsi:type="boolean">true</item>
            <item name="levelsVisibility" xsi:type="number">1</item>
            <item name="sortOrder" xsi:type="number">40</item>
            <item name="required" xsi:type="boolean">false</item>
            <item name="listens" xsi:type="array">
                <item name="index=create_category:responseData" xsi:type="string">setParsed</item>
                <item name="newOption" xsi:type="string">toggleOptionSelected</item>
            </item>
        </item>
    </argument>
</field>

and new-category.js code is

define([
'Magento_Ui/js/form/element/ui-select\'], function (Select){
'use strict';
return Select.extend({

    /**
     * Normalize option object.
     *
     * @param {Object} data - Option object.
     * @returns {Object}
     */
    parseData: function (data) {
        return {
            'is_active': data.model['is_active'],
            value: data.model['category_id'],
            label: data.model['category_title']
        };
    }
});});

dataprovider for posts is

<?php 

namespace Vender\Blog\Model\Post
use Vender\Blog\Model\ResourceModel\Post\CollectionFactory;
use Magento\Framework\App\Request\DataPersistorInterface;
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    protected $collection;


    protected $dataPersistor;

    /**
     * @var array
     */
    protected $loadedData;

    protected $_storeManager;

    /**
     * Constructor
     *
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $blockCollectionFactory
     * @param DataPersistorInterface $dataPersistor
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $blogCollectionFactory,
        DataPersistorInterface $dataPersistor,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $blogCollectionFactory->create();
        $this->dataPersistor = $dataPersistor;
        $this->_storeManager=$storeManager;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);

    }

    /**
     * Prepares Meta
     *
     * @param array $meta
     * @return array
     */
    public function prepareMeta(array $meta)
    {
        return $meta;
    }

    /**
     * Get data
     *
     * @return array
     */
public function getData()
   {


      $baseurl =  $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

       if (isset($this->loadedData)) {
           return $this->loadedData;
       }
       $items = $this->collection->getItems();
       /** @var \Magento\Cms\Model\Block $block */
       foreach ($items as $block) {
           $temp = $block->getData();
           if ($temp['featured_img']) :
               $img = [];
           $img[0]['image'] = $temp['featured_img'];
           $img[0]['url'] = $baseurl.$temp['featured_img'];
           $temp['featured_img'] = $img;
           endif;
           $this->loadedData[$block->getId()] = $block->getData();
       }

       $data = $this->dataPersistor->get('blog');
       if (!empty($data)) {
           $block = $this->collection->getNewEmptyItem();
           $block->setData($data);
           $this->loadedData[$block->getId()] = $block->getData();
           $this->dataPersistor->clear('blog');
       } else {
           if ($items) :
               if ($block->getData('featured_img') != null) {
                   $t2[$block->getId()] = $temp;

                   return $t2;
               } else {
                   return $this->loadedData;
               }
           endif;
       }

       return $this->loadedData;
   }

}

dataprovider is showing all other post details except the selected categories,so what is the solution now

  • Try this : set selected valuyes in value: [12,10], parseData: function (data) { return { 'is_active': data.model['is_active'], options: data.model['category_id'], value: [12,10], label: data.model['category_title'] }; } – kunj Jun 6 '18 at 8:43
  • Thanks for reply,problem solved ,got idea from this and i make relation of post table collection with category table using inner join ( 3 tables ) and Hence provided the required field in DataProvider – sheraz khan Jun 30 '18 at 2:59
3

i make custom join and provided the required fields in the Dataprovider

$id = $block->getPost_id(); $selectedCategories=$this->resourcePost->getCatData($id); foreach ($selectedCategories as $key => $value) $this->loadedData[$id]['category_id'][$key] = $value['category_id'];

And the function in

 public function getCatData($id )//get data for selected category w.s.t post and merge it in dataprovider of post
 {
    $categoryArr=[];
   $sql="SELECT category.category_id FROM pme_blog_post 
        as post INNER JOIN vendor_blog_post_category as post_category 
                    ON post.post_id = post_category.post_id 
                INNER JOIN vendor_blog_category as category 
                    ON post_category.category_id = category.category_id WHERE post.post_id IN ($id)";
           $arrays= $this->getConnection()->query($sql);
           foreach ($arrays as $key => $val)
                {
                    $categoryArr[$key]['category_id'] = $val['category_id']; }
            return $categoryArr; 
 }
| improve this answer | |
  • if need help related to this issue, feel free to ask for help – sheraz khan Sep 15 '18 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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