5

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.

2
  • 1
    @Wojtek Naruniec i tried with code what you mentioned above , but it is now working also i don't find the Magento\Catalog\Setup\CategorySetupFactory.php file in the 1.0.0 beta version by *user3404885 *
    – Amit Bera
    Commented Oct 30, 2015 at 12:06
  • @zhartaunik second question remain as it is. Can you please give it answer for that or you found anything Commented Dec 25, 2015 at 11:36

1 Answer 1

8

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.

2
  • Thank you for your post. I have already resolved my issue. Vote for your answer as it can be helpfull for others.
    – zhartaunik
    Commented Sep 27, 2015 at 18:37
  • Would you please explain if there is a difference of using eavSetupFactory instead of categorySetupFactory?
    – Mehdi
    Commented Jul 25, 2017 at 18:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.