0

I have Create New Grid View For Upload Banner From Back-end Side .

The Upload Image Was Save Success Fully In Data Base .

But When I Edit That The All Other Information Was Edit But The Image File Path Are Not Come So That Can Not Edit .

If We Choose The New Image Than The New Image Was Save But Existing Image Was Not Save When We Edit That Grid In Magento2.

That'd My Save.php File

use Magento\Backend\App\Action; use Magento\Framework\App\Filesystem\DirectoryList;

class Save extends \Magento\Backend\App\Action { public function __construct(Action\Context $context) { parent::__construct($context); }

public function execute()
{
    $data = $this->getRequest()->getPostValue();


    /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultRedirectFactory->create();
    if ($data) {
        $model = $this->_objectManager->create('Vrinsoft\Banner\Model\Banner');

        $id = $this->getRequest()->getParam('id');
        if ($id) {
            $model->load($id);
            $model->setCreatedAt(date('Y-m-d H:i:s'));
        }

        try{
            $uploader = $this->_objectManager->create(
                'Magento\MediaStorage\Model\File\Uploader',
                ['fileId' => 'banner']
            );
            $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
            /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
            $imageAdapter = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')->create();
            $uploader->setAllowRenameFiles(true);
            $uploader->setFilesDispersion(true);
            /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
            $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
                ->getDirectoryRead(DirectoryList::MEDIA);
            $result = $uploader->save($mediaDirectory->getAbsolutePath('banner_banner'));
                if($result['error']==0)
                {
                    $data['banner'] = 'banner_banner' . $result['file'];
                }
        } catch (\Exception $e) {
            //unset($data['image']);
        }
          // if(isset($data['banner']) || $data['banner']){
         //                $data['banner'] = implode(',',$data['banner']);
         //            }

        if(isset($data['banner']['delete']) && $data['banner']['delete'] == '1')
            $data['banner'] = '';




        if(isset($data['store_id']) && $data['store_id']){
            $data['store_id'] = implode(',',$data['store_id']);
        }
        //           
        $model->setData($data);

        try {
            $model->save();
            $this->messageManager->addSuccess(__('The Banner has been saved.'));
            $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false);
            if ($this->getRequest()->getParam('back')) {
                return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId(), '_current' => true]);
            }
            return $resultRedirect->setPath('*/*/');
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\RuntimeException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            echo $e->getMessage();die;
            $this->messageManager->addException($e, __('Something went wrong while saving the Banner.'));
        }

        $this->_getSession()->setFormData($data);
        return $resultRedirect->setPath('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
    }
    return $resultRedirect->setPath('*/*/');
}

}

And That's My Edit.php File

namespace Vrinsoft\Banner\Controller\Adminhtml\banner;

use Magento\Backend\App\Action;

class Edit extends \Magento\Backend\App\Action { /** * Core registry * * @var \Magento\Framework\Registry */ protected $_coreRegistry = null;

/**
 * @var \Magento\Framework\View\Result\PageFactory
 */
protected $resultPageFactory;

/**
 * @param Action\Context $context
 * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
 * @param \Magento\Framework\Registry $registry
 */
public function __construct(
    Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Magento\Framework\Registry $registry
) {
    $this->resultPageFactory = $resultPageFactory;
    $this->_coreRegistry = $registry;
    parent::__construct($context);
}

/**
 * {@inheritdoc}
 */
protected function _isAllowed()
{
    return true;
}

/**
 * Init actions
 *
 * @return \Magento\Backend\Model\View\Result\Page
 */
protected function _initAction()
{
    // load layout, set active menu and breadcrumbs
    /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
    $resultPage = $this->resultPageFactory->create();
    $resultPage->setActiveMenu('Vrinsoft_Banner::Banner')
        ->addBreadcrumb(__('Vrinsoft Banner'), __('Vrinsoft Banner'))
        ->addBreadcrumb(__('Manage Item'), __('Manage Item'));
    return $resultPage;
}

/**
 * Edit Item
 *
 * @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
public function execute()
{

    // 1. Get ID and create model
    $id = $this->getRequest()->getParam('id');
    // echo $id; exit;
    $model = $this->_objectManager->create('Vrinsoft\Banner\Model\Banner');
    // echo json_encode($model->getData());exit;
    // 2. Initial checking
    if ($id) {


        $model->load($id);
         // if(isset($data['banner']) || $data['banner']){
         //                $data['banner'] = implode(',',$data['banner']);
         //            }
        if (!$model->getId()) {

            $this->messageManager->addError(__('This item no longer exists.'));
            /** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();

            return $resultRedirect->setPath('*/*/');
        }
    }

    // 3. Set entered data if was error when we do save
    $data = $this->_objectManager->get('Magento\Backend\Model\Session')->getFormData(true);

    if (!empty($data)) {  

        $model->setData($data);
    }

    // 4. Register model to use later in blocks
    $this->_coreRegistry->register('banner', $model);

    // 5. Build edit form
    /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
    $resultPage = $this->_initAction();
    $resultPage->setActiveMenu('Vrinsoft_Banner::banner');
    $resultPage->addBreadcrumb(__('Vrinsoft'), __('Vrinsoft'));
    $resultPage->addBreadcrumb(
        $id ? __('Edit Banner') : __('New Banner'),
        $id ? __('Edit Banner') : __('New Banner')
    );
    $resultPage->getConfig()->getTitle()->prepend($id ? __('Edit Banner') : __('New Banner'));
    // $resultPage->getConfig()->getTitle()->prepend($model->getId() ? $model->getTitle() : __('New Item'));
    return $resultPage;
}

}

0

The Solution Is Add The Below Code On Following File Path

/app/code/Your_Extention/Your_Module/Controller/Adminhtml/banner/Save.php

In That File Add Below Code

if(is_array($data['image']))
 {
    echo $str_val = implode("", $data['image']);
    $data['image'] = $str_val;
 }

Here The Multiple Image The Have Considere The Image Array So We Have Convert That Array To String Using Simple impload Function.

| improve this answer | |

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.