Magento 2 – How to Add Custom Image Attribute to Category

admincategorycustom-fieldmagento2overrides

Can you help me to add custom field for the image on my category?

I want to add a thumbnail for my category via admin panel, and then get it on front-end.

I have now eav attribute in eav_attribute table, but it still doesn't appear in admin panel.

Thanks!

Best Answer

    protected $categorySetupFactory;

    public function __construct(
        ...
        \Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory    
        ....
    ) {
        ....
        $this->categorySetupFactory = $categorySetupFactory;    
        ....
    }

Now In your setup file you can use this like,

         $setup = $this->categorySetupFactory->create(['setup' => $setup]);         
         $setup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY, 'custom_image', [
                'type' => 'varchar',
                'label' => 'Custom Image',
                'input' => 'image',
                'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image',
                'required' => false,
                'sort_order' => 9,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group' => 'General Information',
            ]
        );
Related Topic