1

I want to add image role in my custom page in admin side. There are four image role in core part . I want to add same as in my custom page.

How to add it in my custom page as mentioned in image

please help me.... enter image description here

1 Answer 1

3

You can add new image type using this code (from setup data script of your module):

namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'attribute_image_code',
            [
                'type' => 'varchar',
                'label' => 'Image Attribute Name (Label)',
                'input' => 'media_image',
                'frontend' => 'Magento\Catalog\Model\Product\Attribute\Frontend\Image',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'filterable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'sort_order' => 10,
                'required' => false,
            ]
        );
    }
}

Replace attribute_image_code with code of your own attribute and do not forget to rename attribute label.

5
  • I want to add in phtml file...how to add it? Commented Jun 23, 2017 at 12:27
  • 5
    I don't want to add new image type... i want to use image role which is already available in core Commented Jun 23, 2017 at 12:28
  • Please, add code to clarify you question Commented Jun 23, 2017 at 12:29
  • @RohanHapani did you find a solution? This answer seems to just add a new EAV property and not update the Role list Commented Nov 27, 2017 at 21:19
  • @SiarheyUchukhlebau I want to assign this role to multiple images any idea??
    – Ghulam.M
    Commented Oct 9, 2020 at 10:43

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.