Magento2 – Create EAV Attribute for Catalog

attributeseavmagento2

I'm trying to understand how magento2 is creating attributes in initial installation and using this I'm trying to create my own attribute.

For example I'd like to create duplicate of thumbnail attribute.

I followed the instruction in this article: https://www.atwix.com/magento/adding-attribute-programatically-magento2/

But I'm getting an error (I got error with report which refer to class below)

    <type name="Magento\Framework\Module\Updater\SetupFactory">

As I have no Magento\Framework\Module\ Updater folder. What am I doing wrong?


Then I tried to look how magento does it resolve.

I have opened Catalog/Setup folder and found file, which is creating attributes just like I need (Setup/CategorySetup). For example:

                'thumbnail' => [
                    'type' => 'varchar',
                    'label' => 'Thumbnail',
                    'input' => 'media_image',
                    'frontend' => 'Magento\Catalog\Model\Product\Attribute\Frontend\Image',
                    'required' => false,
                    'sort_order' => 3,
                    'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_STORE,
                    'used_in_product_listing' => true,
                    'group' => 'Images',
                ],

this is a part of

    public function getDefaultEntities()

But I can't find where does this method calls(1 question), as it runs only once when magento installs and it's impossible to put breakpoint

2. Which file executes by default? Is it InstallData/Schema? Should I write anything in config to say magento execute installation script (as it done in magento 1 in core_setup directive)

p.s. Any article on this topic will be appreciated.

Best Answer

Place InstallData.php file in your module's Setup directory:

<?php

namespace Training\Product\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * Category setup factory
     *
     * @var CategorySetupFactory
     */
    private $categorySetupFactory;

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

    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

        $categorySetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'some_new_attr',
            [
                'label'            => 'Some Cool Attribute',
                'group'            => 'Product Details',
                'required'         => false,
                'visible_on_front' => true,
            ]
        );

        $setup->endSetup();
    }
}

Then add your module to app/etc/config.php, clear cache and install:

bin/magento setup:upgrade

This should be enough, no need to add anything in XML files. This should answer your second question.

Regarding first one, data from Magento\Catalog\Setup\CategorySetup::getDefaultEntities() is used by Magento\Eav\Setup\EavSetup::installEntities() method.

Related Topic