0

I have created custom image uploader to upload category icon. I also created custom model and controller for the same.

After click on save button image will save in pub/static folder successfully.

Now, I want to get that image in frontend. How to done it?

  • Where did you save the icon file name? Did you use any custom Database Table? – Pritam Info 24 Feb 6 '19 at 12:49
  • No, I am not using any custom database table. I just added one Attribute using InstallData.php in Setup Folder. – Mayur Jotaniya Feb 7 '19 at 4:14
  • I also save file in pub/media folder. I just want to display that file in frontend. – Mayur Jotaniya Feb 7 '19 at 4:15
0

You can create a helper class and use your help to get category icon Url as described below.

I assume your custom extension name is "Vendor_Module". I also assume that your custom category attribute name is "caticon"

step 1) Create a helper class Data.php under /app/code/Vendor/Module/Helper/

File : Data.php

<?php

namespace Vendor\Module\Helper;

class Data
    extends \Magento\Framework\App\Helper\AbstractHelper
{
    // category attribute name
    const ATTRIBUTE_NAME = "caticon";

    /**
     * Store manager
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_storeManager = $storeManager;
    }

    /**
     * Retrieve Icon image URL by category
     *
     * @return string
     */
    public function getImageUrl(\Magento\Catalog\Model\Category $category)
    {
        $image = $category->getData(self::ATTRIBUTE_NAME);

        return $this->getUrl($image);
    }

    /**
     * Retrieve URL
     *
     * @return string
     */
    public function getUrl($value)
    {
        $url = false;
        if ($value) {
            if (is_string($value)) {
                $url = $this->_storeManager->getStore()->getBaseUrl(
                        \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
                    ) . 'pub/media/' . $value;
            } else {
                throw new \Magento\Framework\Exception\LocalizedException(
                    __('Something went wrong while getting the image url.')
                );
            }
        }

        return $url;
    }


}

step 2) Run Di compile commands from CLI

php bin/magento setup:di:compile

step 3) How to use your helper to get category icon url in a phtml file.

Sample example phmtl file:

    <?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?>
    $_helper       = $this->helper('Vendor\Module\Helper\Data');
    $categoryId    = 10;
    $category      = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
    $categoryIconUrl =  $_helper->getUrl($category->getCaticon()); // i.e get icon Attribute
    // or you can use below line of code to get icon image by category object
   // $categoryIconUrl =  $_helper->getImageUrl($category)
    ?>                            
    <img src="<?php echo $categoryIconUrl?>" />
| 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.