0

I want to display uploaded file in admin edit form using ui component in magento2

How Can i Get this?

enter image description here

and here is fildset.php

     <?php  
        namespace Vendor\Module\Ui\Component\Form;


     use Magento\Framework\View\Element\UiComponent\ContextInterface;  
     use Magento\Framework\View\Element\UiComponentInterface;  
      use Magento\Ui\Component\Form\FieldFactory;  
     use Magento\Ui\Component\Form\Fieldset as BaseFieldset;

     class Fieldset extends BaseFieldset  
    {

      private $fieldFactory;

     public function __construct(
        ContextInterface $context,
         array $components = [],
  array $data = [],
  FieldFactory $fieldFactory,
  \Vendor\Module\Model\Projectsubmit $modelProjectsubmit,
  \Magento\Framework\Session\SessionManagerInterface $coreSession,
  \Magento\Framework\App\Request\Http $request,
  \Magento\Store\Model\StoreManagerInterface  $storeManager   
           )
      {
            parent::__construct($context, $components, $data);
            $this->_coreSession = $coreSession;
            $this->_request = $request;
          $this->_storeManager = $storeManager;
       }


       public function getChildComponents()
      {

      $fields=$this->getFields();

         foreach ($fields as $k => $fieldConfig) {

        $fieldInstance = $this->fieldFactory->create();
      $name = $fieldConfig['name'];

      $fieldInstance->setData(
          [
              'config' => $fieldConfig,
              'name' => $name
          ]
      );

      $fieldInstance->prepare();
      $this->addComponent($name, $fieldInstance);
  }
    $this->_coreSession->setMaincategory('');
    $this->_coreSession->setChildcategory('');
  return parent::getChildComponents();
         }


     function getFields(){
  $id=$this->_request->getParam('projectsubmit_id');

  $getCategoryData=$this->_modelProjectsubmit->getCollection()->getData();
  foreach($getCategoryData as $key=>$value){
      if($value['projectsubmit_id']==$id)
      {
          $mainCategory=$value['main_category'];
          $childCategory=$value['child_category'];
          $uploadFile=$value['upload_file'];
      }

  }
  $after_html='';

    if($uploadFile != '')
    {
      $result = explode(",",$uploadFile);    
      foreach ($result as $value)
      {
          $path = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)."product-popup/".$value;

          // $after_html.= '<a href="'.$path.'" target ="_blank">
          //             <img height="22" width="22" class="small-image-preview v-middle" alt="'.$value.'" title="'.$value.'" id="slider" src="'.$path.'"/></a>';
      }
    }

  $formFields = $this->_modelProjectsubmit->defineFormFields();

    $currentform = $formFields[$mainCategory][$childCategory];  

    foreach($currentform as $key => $formfield) :  
    if(!(isset($formfield['type']))) 
    continue; 


      if($formfield['type'] == "text") {
         $fields[]= [
                'label'     => strip_tags($formfield['label']),
                'disabled'  => true,
                'name'      => $formfield['key'],
                'formElement' => 'input',
          ];  
          } 
      elseif($formfield['type'] == "radio") {


          if($formfield['key'] == "color_preferences") {
         $fields[]= [
                'label'     => 'Color Preferences',
                'disabled'  => true,
                'name'      => 'color_preferences',
                'formElement' =>'input',
          ]; 

          }
           else
          {
               $fields[]= [
                'label'     => strip_tags($formfield['label']),
                'disabled'  => true,
                'name'      => $formfield['key'],
                'formElement' =>'select',
                'options'    => $options,
            ]; 

                   // die("<br> in".__FILE__." : ".__LINE__);

         }
       if(isset($formfield['extratype']) && $formfield['extratype'] == "text") {
               $fields[]= [
                'label'     => "Other Info",
                'disabled'  => true,
                'name'      => $formfield['extrakey'],
                'formElement' =>'input',
            ]; 
       }
 }
   elseif($formfield['type'] == "textarea") {
          $fields[]= [
                'label'     => strip_tags($formfield['label']),
                'disabled'  => true,
                'name'      => $formfield['key'],
                'formElement' =>'textarea',
            ];  
    }
     **$fields[]= [
                'label'     => 'File',
                'disabled'  => true,
                'name'      => 'upload_file',
                'formElement' =>'fileUploader',
                'url' => $after_html
            ];**    ***(i need to set my all logic here)***

    endforeach;
    return $fields;

     }
         }

$fields[]= [ 'label' => 'File', 'disabled' => true, 'name' => 'upload_file', 'formElement' =>'fileUploader', 'url' => $after_html ]; please check this part

3
  • Can you please explain more or attach screenshot ? Nov 12, 2018 at 5:19
  • 1
    @RohanHapani PLease check attached. I am creating this form with dynamic content. Nov 12, 2018 at 5:25
  • @RutveeSojitra Have you checked reference module in my below answer? Nov 12, 2018 at 7:16

1 Answer 1

0

Checkout below sample image upload module to achieve your things. i think this module is fit to your requirement.

Magento 2 sample module demonstrating how to upload an image file within a grid and form UI Component.

I hope it helps.

Note: Here i give you sample module so you can take a reference and build your module or customize your module.

3
  • Yes but it's the different case. I am generating form dynamically Nov 12, 2018 at 8:19
  • Dynamic means, can you explain more? Nov 12, 2018 at 8:41
  • i have added fieldset.php file that hoe m i generating dynamic form field Nov 12, 2018 at 8:52

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.