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.

  • 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 Oct 30 '15 at 12:06
  • @zhartaunik second question remain as it is. Can you please give it answer for that or you found anything – Bhupendra Jadeja Dec 25 '15 at 11:36
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.

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

protected by Community Feb 12 '16 at 3:10

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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