Magento2 Adminhtml – How to Add Image Role Programmatically adminhtmlmagento2 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…. Best Answer 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. Related SolutionsMagento – Magento 2 add extra image Type/Role You have to just add image role using create image attribute from Stores -> Attribute -> Product Click on Create new attribute. Set Catalog Input type for store owner is Media Image Save Attribute. Now set above attribute in Attribute Set like default. Run command php bin/magento indexer:reindex Magento – Get product image by its “Role” in the gallery You can get products image by Type by following Code : $baseImage = $product->getImage(); $smallImage = $product->getSmallImage(); $thumbnail = $product->getThumbnail(); OR You can get types of gallery images of a product by following code: $existingMediaGalleryEntries = $product->getMediaGalleryEntries(); foreach ($existingMediaGalleryEntries as $entry) { $entry->getTypes(); } OR If you have custom media role then : $customIcon = $product->getResource()->getAttribute('custom_media_attribute')->getFrontend()->getValue($product); Related TopicMagento – How to add Custom Field at Product Edit/Save Page
Best Answer
You can add new image type using this code (from setup data script of your module):
Replace
attribute_image_code
with code of your own attribute and do not forget to rename attribute label.