0

hello everyone I want to display the uploaded image through the form in a custom tab in admin grid for that image name is stored in the database through that I want to display the image.

block/tab/Info.php

<?php

 namespace Magneto\BookFreeLayout\Block\Adminhtml\Form\Edit\Tab;

     use Magento\Backend\Block\Widget\Form\Generic;
   use Magento\Backend\Block\Widget\Tab\TabInterface;
    use Magento\Backend\Block\Template\Context;
  use Magento\Framework\Registry;
  use Magento\Framework\Data\FormFactory;
   use Magento\Cms\Model\Wysiwyg\Config;
    // use Tutorial\SimpleNews\Model\System\Config\Status;

      class Info extends Generic implements TabInterface
          {
       /**
        * @var \Magento\Cms\Model\Wysiwyg\Config
         */
       protected $_wysiwygConfig;
         protected $_productRepository;
        /**
          * @var \Tutorial\SimpleNews\Model\Config\Status
       */
protected $_newsStatus;

            /**
           * @param Context $context
          * @param Registry $registry
       * @param FormFactory $formFactory
         * @param Config $wysiwygConfig
         * @param Status $newsStatus
       * @param array $data
 */
public function __construct(
    Context $context,
    Registry $registry,
    FormFactory $formFactory,
    Config $wysiwygConfig,
    \Magento\Catalog\Model\ProductRepository $productRepository,
    // Status $newsStatus,
    array $data = []
) {
    $this->_wysiwygConfig = $wysiwygConfig;
    // $this->_newsStatus = $newsStatus;
    $this->_productRepository = $productRepository;
    parent::__construct($context, $registry, $formFactory, $data);
}

/**
 * Prepare form fields
 *
 * @return \Magento\Backend\Block\Widget\Form
 */
protected function _prepareForm()
{
   /** @var $model \Tutorial\SimpleNews\Model\News */
    $model = $this->_coreRegistry->registry('bookfreelayout_alllayout');

    /** @var \Magento\Framework\Data\Form $form */
    $form = $this->_formFactory->create();
    $form->setHtmlIdPrefix('news_');
    $form->setFieldNameSuffix('news');

    $fieldset = $form->addFieldset(
        'base_fieldset',
        ['legend' => __('General')]
    );

    if ($model->getId()) {
        $fieldset->addField(
            'id',
            'hidden',
            ['name' => 'id']
        );
    }
    $fieldset->addField(
        'name',
        'text',
        [
            'name'        => 'name',
            'label'    => __('name'),
            'required'     => true
        ]
    );
     $fieldset->addField(
        'email',
        'text',
        [
            'name'        => 'email',
            'label'    => __('Email'),
            'required'     => true
        ]
    );
     $fieldset->addField(
        'height',
        'text',
        [
            'height'        => 'height',
            'label'    => __('Height'),
            'required'     => true
        ]
    );
     $fieldset->addField(
        'width',
        'text',
        [
            'name'        => 'width',
            'label'    => __('Width'),
            'required'     => true
        ]
    ); 

   $fieldset->addField(
        'entity_id',
        'text',
        [
            'name'        => 'entity_id',
            'label'    => __('entity_id'),
            'required'     => true,

        ]
    );
     $fieldset->addField(
        'image',
        'text',
        [
            'name'        => 'image',
            'label'    => __('image'),
            'value' => '<img src="' . $imageUrl . '" width="50"/>',
          'renderer'=>'Magneto\BookFreeLayout\Block\Adminhtml\Extension\Grid\Renderer\LogoImage',
            'required'     => true,

        ]
    );  


    $data = $model->getData();
    $form->setValues($data);
    $this->setForm($form);

    return parent::_prepareForm();
}

/**
 * Prepare label for tab
 *
 * @return string
 */
public function getTabLabel()
{
    return __('News Info');
}

/**
 * Prepare title for tab
 *
 * @return string
 */
public function getTabTitle()
{
    return __('News Info');
}

/**
 * {@inheritdoc}
 */
public function canShowTab()
{
    return true;
}

/**
 * {@inheritdoc}
 */
public function isHidden()
{
    return false;
}
}

logoimage.php

<?php

  namespace 
  Magneto\BookFreeLayout\Block\Adminhtml\Extension\Grid\Renderer;

   class LogoImage extends 
  \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer {

protected $_storeManager;

protected $_fileSystem;

protected $_modelData;

public function __construct(
    \Magento\Backend\Block\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\Filesystem $fileSystem,
    \VendorName\ModuleName\Model\YourModel $modelData,
    array $data = []) {
    parent::__construct($context, $data);
    $this->_storeManager = $storeManager;
    $this->_fileSystem = $fileSystem;
    $this->_modelData = $modelData;
    $this->_authorization = $context->getAuthorization();
}
public function render(\Magento\Framework\DataObject $row) {
    $id = $this->_getValue($row);
    $filepath = $this->_fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('images');
    $data = $this->_modelData->load($id);
    foreach($data as $file)
    {   
        echo "<img src='".$filepath."pub/media/images".$file['image']."' height='30' width='30' />";
    }
}

}

through this code form data is displaying now I want to display the uploaded image NOTE:- there are multiple images are stored in the database enter image description here

0

Add this to Your form

$fieldset->addField(
        'contact_image',
        'image',
        [
            'name' => 'contact_image',
            'label' => __('Contact Image'),
            'title' => __('Contact Image'),

            'disabled' => $isElementDisabled
        ]
    ); 
0

Add this field in your form :

$fieldset->addField(
    'image_database_field',
    'label',
    [
        'name' => 'image_database_field',
        'label' => __('Logo Image'),
        'title' => __('Logo Image'),
        'value' => '<img src="' . $imageUrl . '" width="50"/>',
        'renderer'  => 'VendorName\ModuleName\Block\Adminhtml\Extension\Grid\Renderer\LogoImage',
    ]
);

Create LogoImage.php renderer file at VendorName\ModuleName\Block\Adminhtml\Extension\Grid\Renderer and add this below code :

<?php

namespace VendorName\ModuleName\Block\Adminhtml\Extension\Grid\Renderer;

class LogoImage extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer {

    protected $_storeManager;

    protected $_fileSystem;

    protected $_modelData;

    public function __construct(
        \Magento\Backend\Block\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Filesystem $fileSystem,
        \VendorName\ModuleName\Model\YourModel $modelData,
        array $data = []) {
        parent::__construct($context, $data);
        $this->_storeManager = $storeManager;
        $this->_fileSystem = $fileSystem;
        $this->_modelData = $modelData;
        $this->_authorization = $context->getAuthorization();
    }
    public function render(\Magento\Framework\DataObject $row) {
        $id = $this->_getValue($row);
        $filepath = $this->_fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath('your image folder');
        $data = $this->_modelData->load($id);
        foreach($data as $file)
        {   
            echo "<img src='".$filepath."your image folder".$file['image file name which is in db']."' height='30' width='30' />";
        }
    }

}
  • no output is coming – Ashish Ramchandani Mar 4 at 5:35
  • Upload your code which you added in your file. – Emipro Technologies Pvt. Ltd. Mar 4 at 5:35
  • form field $fieldset->addField( 'image', 'text', [ 'name' => 'image', 'label' => __('image'), 'value' => '<img src="' . $imageUrl . '" width="50"/>', 'renderer' => 'Magneto\BookFreeLayout\Block\Adminhtml\Extension\Grid\Renderer\LogoImage', 'required' => true, ] ); – Ashish Ramchandani Mar 4 at 5:36
  • i have edited my answer – Ashish Ramchandani Mar 4 at 5:43
  • Remove 'value' => '<img src="' . $imageUrl . '" width="50"/>' – Emipro Technologies Pvt. Ltd. Mar 4 at 5:44

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.