0

I have created admin form using ui-component in backend, I have two fieldsets one(general) is containing noraml text fields another one(upload) is containing image uploder fields(small,large,extra large and medium images).It was working good for new action, But While clicking edit It fetches only the text field values from DB which is inside in general fieldset, but failed to fetch images which is inside in upload fieldset How do I overcome it . Save.php

 $id = $this->getRequest()->getParam('id');
        try {
            $modelData = $this->model->create();
            //If gets id loads the model
            if ($id) {
                $modelData = $modelData->load($id);
            }

            $formData = $this->getRequest()->getParam('general');
            $imageData = $this->getRequest()->getParam('upload');

            $user = $this->getCurrentUser();

            if (empty($formData ['id'])) {

                $formData['id'] = null;
                // update the current user name for both created and updated
                $formData['created_by'] = $user;
                $formData['updated_by'] = $user;
            }

            $formData['updated_by'] = $user;
//For small image
            if (isset($imageData['small_image'])) {

                $formData['small_image'] = $imageData['small_image'][0]['name'];
            } else {
                $formData['small_image'] = null;
            }
//For medium image           
            if (isset($imageData['medium_image'])) {
                $formData['medium_image'] = $imageData['medium_image'][0]['name'];
            } else {
                $formData['medium_image'] = null;
            }
            //For large Image
            if (isset($imageData['large_image'])) {
                $formData['large_image'] = $imageData['large_image'][0]['name'];
            } else {
                $formData['large_image'] = null;
            }
            // For Extra large image
            if (isset($imageData['extra_large_image'])) {
                $formData['extra_large_image'] = $imageData['extra_large_image'][0]['name'];
            } else {
                $formData['extra_large_image'] = null;
            }

           //Set all the data in DB from form
            $modelData->setData($formData);
            $modelData->save();

DataProvider.php

public function getData()
    {
        if (isset($this->_loadedData)) {
            return $this->_loadedData;
        }
        //Get the collection.
        $items = $this->collection->getItems();
        $slide = array();
        foreach ($items as $item) {
            $this->_loadedData[$item->getId()]['general'] = $item->getData();

            if ($item->getSmallImage()) {
                //Get the image and image url.
                $slide['small_image'][0]['name'] = $item->getSmallImage();
                $slide['small_image'][0]['url'] = $this->getMediaUrl() . 'path' . $item->getImage();

                $fullData = $this->_loadedData;
                $this->_loadedData[$item->getId()]['general'] = array_merge($fullData[$item->getId()]['general'], $slide);
            }
        }
        return $this->_loadedData;
    }

    public function getMediaUrl()
    {
        //Get the url for the image.
        $mediaUrl = $this->_storeManager->getStore()
                ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
        return $mediaUrl;
    }

I have done this by reffering below url Magento 2: Implement UI Fileuploader

  • Check my updated answer and Let me know if you have any issue. – Prince Patel Feb 1 '18 at 10:12
  • See the updated question, I have added my save controller then you can understand the problem what i am facing – Jaisa Feb 1 '18 at 10:17
  • @PrincePatel, Can you understand – Jaisa Feb 1 '18 at 10:19
2

You final getData() look like this

public function getData()
{
    if (isset($this->_loadedData)) {
        return $this->_loadedData;
    }
    //Get the collection.
    $items = $this->collection->getItems();
    $slide = array();
    foreach ($items as $item) {
        $this->_loadedData[$item->getId()]['general'] = $item->getData();

        if ($item->getSmallImage()) {
            //Get the image and image url.
            $slide['small_image'][0]['name'] = $item->getSmallImage();
            $slide['small_image'][0]['url'] = $this->getMediaUrl() . 'path' . $item->getSmallImage();

            $fullData = $this->_loadedData;
            $this->_loadedData[$item->getId()]['general'] = array_merge($fullData[$item->getId()]['general'], $slide);
        }

        /* second field
        if ($model->getLargeImage()) {
            $slide['large_image'][0]['name'] = $item->getLargeImage();
            $slide['large_image'][0]['url'] = $this->getMediaUrl() . 'path' . $item->getLargeImage();

            $fullData = $this->loadedData;
            $this->_loadedData[$item->getId()]['general'] = array_merge($fullData[$item->getId()]['general'], $slide);
        }*/


    }
    return $this->_loadedData;
}

public function getMediaUrl()
{
    //Get the url for the image.
    $mediaUrl = $this->_storeManager->getStore()
            ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    return $mediaUrl;
}

Please change $slide['small_image'][0]['url'] as above

  • Thank you for your response. I have set form fields in two fieldsets named general and upload. In above code only fetches the values of general field but failed to fetch the image. But If I put 'upload ' instead of 'general' , It will fetch only the image not general fieldset values. – Jaisa Feb 1 '18 at 10:15
  • Please see the updated post then answer. Thank you – Jaisa Feb 1 '18 at 10:30

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.