Skip to content

Instantly share code, notes, and snippets.

@dfelton
Last active September 19, 2024 14:51
Show Gist options
  • Save dfelton/491b7dd59847c26b71e3d5613784c513 to your computer and use it in GitHub Desktop.
Save dfelton/491b7dd59847c26b71e3d5613784c513 to your computer and use it in GitHub Desktop.
Magento 2 EAV properties defaults and mappings

Magento 2 EAV Property Defaults and Mappings

Reference sheet for when creating EAV attributes with \Magento\Eav\Setup\EavSetup::addAttribute().

EAV

EAV properties which apply to all entity types.

Derived from: \Magento\Eav\Model\Entity\Setup\PropertyMapper

use \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface

EAV property                    | Map Value                  | Default Value
----------------------------------------------------------------------------------
attribute_model                 | attribute_model            | null
backend_model                   | backend                    | null
backend_table                   | table                      | null
backend_type                    | type                       | varchar
default_value                   | default                    | null
frontend_class                  | frontend_class             | null
frontend_input                  | input                      | text
frontend_label                  | label                      | null
frontend_model                  | frontend                   | null
is_global                       | global                     | ScopedAttributeInterface::SCOPE_GLOBAL
is_required                     | required                   | 1
is_unique                       | unique                     | 0
is_user_defined                 | user_defined               | 0
note                            | note                       | null
source_model                    | source                     | null

Catalog Product

EAV properties specific to catalog_product and catalog_category entities (note: while catalog_category entities may have all of these properties, not all values are applicable to this entity type).

Derived from:

use \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface

EAV property                    | Map Value                  | Default Value
----------------------------------------------------------------------------------
apply_to                        | apply_to                   | null
frontend_input_renderer         | input_renderer             | null
is_global                       | global                     | ScopedAttributeInterface::SCOPE_GLOBAL
is_comparable                   | comparable                 | 0
is_configurable                 | is_configurable            | 1
is_filterable                   | filterable                 | 0
is_filterable_in_grid           | is_filterable_in_grid      | 0
is_filterable_in_search         | filterable_in_search       | 0
is_html_allowed_on_front        | is_html_allowed_on_front   | 0
is_searchable                   | searchable                 | 0
is_used_for_promo_rules         | used_for_promo_rules       | 0
is_used_in_grid                 | is_used_in_grid            | 0
is_visible                      | visible                    | 1
is_visible_in_advanced_search   | visible_in_advanced_search | 0
is_visible_in_grid              | is_visible_in_grid         | 0
is_visible_on_front             | visible_on_front           | 0
is_wysiwyg_enabled              | wysiwyg_enabled            | 0
position                        | position                   | 0
search_weight                   | search_weight              | 1
used_in_product_listing         | used_in_product_listing    | 0
used_for_sort_by                | used_for_sort_by           | 0

Prefilled command with all defaults:

declare(strict_types=1);

namespace VendorName\ModuleName\Setup\Patch\Data;

use Magento\Catalog\Model\{Product, Category};
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Setup\EavSetup;

class CreateAttributeName implements DataPatchInterface
{
    public function __construct(
        EavSetupFactory $eavSetupFactory,
        ModuleDataSetupInterface $moduleDataSetup,
    ) {
    }

    public function apply(): void
    {
        /** @var \Magento\Eav\Setup\EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(
            'ENTITY_TYPE', // Product::ENTITY | Catalog::ENTITY
            'ATTRIBUTE_CODE',
            [
                'apply_to' => null, // (comma separated list of valid `catalog_product_entity.type_id` values; NULL means apply to all types)
                'attribute_model' => null,
                'backend' => null,
                'comparable' => 0,
                'default' => null,
                'filterable' => 0,
                'filterable_in_search' => 0,
                'frontend' => null,
                'frontend_class' => null,
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'input' => 'text',
                'input_renderer' => null,
                'is_configurable' => 1,
                'is_filterable_in_grid' => 0,
                'is_html_allowed_on_front' => 0,
                'is_used_in_grid' => 0,
                'is_visible_in_grid' => 0,
                'label' => null,
                'note' => null,
                'position' => 0,
                'required' => 1,
                'search_weight' => 1,
                'searchable' => 0,
                'source' => null,
                'table' => null,
                'type' => 'varchar',
                'unique' => 0,
                'used_for_promo_rules' => 0,
                'used_for_sort_by' => 0,
                'used_in_product_listing' => 0,
                'user_defined' => 0,
                'visible' => 1,
                'visible_in_advanced_search' => 0,
                'visible_on_front' => 0,
                'wysiwyg_enabled' => 0,
            ]
        );
    }

    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }
}
<?php
declare(strict_types=1);
namespace VendorName\ModuleName\Setup\Patch\Data;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UninstallInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UninstallInterface;
class PatchName implements DataPatchInterface, UninstallInterface
{
public function __construct(
private ModuleDataSetupInterface $moduleDataSetup,
private EavSetupFactory $eavSetupFactory,
) {
}
public function apply(): self
{
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute(
'ENTITY_TYPE_ID', // int|string
'CODE', // int|string
[
// configuration data
]
);
return $this;
}
public function getAliases(): array
{
return [];
}
public static function getDependencies(): array
{
return [];
}
public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context): void
{
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->removeAttribute(
'ENTITY_TYPE_ID', // int|string`
'CODE' // int|string
);
}
}

Types Cheatsheet

Specific values for achieving certain types of attributes. More property values may be set, but these are what are required for each outcome.

Yes/No Attribute:

use Magento\Eav\Model\Entity\Attribute\Source\Boolean;

$data = [
    'input' => 'boolean',
    'source' => Boolean::class,
    'type' => 'int',
];

Multiselect

use Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend;

$data = [
    'type' => 'text', // can leave default of varchar if we don't need more than 255 characters to store value
    'backend' => ArrayBackend::class,
    'input' => 'multiselect',
];

Select Attribute w/ Class Driven Options

(as opposed to eav_attribute_option driven)

$data = [
    'type' => 'int|varchar', // (whatever is appropriate for source class)
    'input' => 'select',
    'source' => \VendorName\ModuleName\Model\Source\ClassName::class', // (see below)
];
declare(strict_types=1);

namespace VendorName\ModuleName\Model\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class ClassName extends AbstractSource
{
    public function getAllOptions()
    {
        return [
            ['label' => __('-- Select --'), 'value' => null],
            ['label' => __('Foo'), 'value' => 'foo'],
            ['label' => __('Bar'), 'value' => 'bar'],
        ];
    }
}

Datetime

Creating the EAV attribute:

$data = [
    'type' => 'datetime',
    'input' => 'date',
];

UI Component Configurations:

<field name="REPLACE_WITH_ATTRIBUTE_CODE">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="component" xsi:type="string">Magento_Ui/js/form/element/date</item>
            <item name="visible" xsi:type="boolean">true</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="formElement" xsi:type="string">input</item>
            <item name="source" xsi:type="string">REPLACE_WITH_ATTRIBUTE_CODE</item>
            <item name="label" xsi:type="string" translate="true">Foo Bar Label</item>
            <item name="tooltip" xsi:type="array">
                <item name="description" xsi:type="string"><![CDATA[Foo Bar.]]></item>
            </item>
            <item name="options" xsi:type="array">
                <item name="dateFormat" xsi:type="string">y-MM-dd</item> <!-- change as necessary -->
                <item name="showsTime" xsi:type="boolean">false</item> <!--true or false -->
            </item>
        </item>
    </argument>
</field>

WYSWIYG

$data = [
    'input' => 'textarea',
    'is_wysiwyg_enabled' => true,
    'type' => 'text',
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment