Skip to content

Instantly share code, notes, and snippets.

@ericrisler
Last active March 18, 2021 13:15
Show Gist options
  • Save ericrisler/df15337e287c1295338c7ed53cb9054d to your computer and use it in GitHub Desktop.
Save ericrisler/df15337e287c1295338c7ed53cb9054d to your computer and use it in GitHub Desktop.
References related to EAV Attribtues in Magento 2

The array of data you can pass to the \Magento\Eav\Setup\EavSetup::addAttribute() method is as follows:

Some of the attribute keys are remapped using the Magento\Eav\Model\Entity\Setup\PropertyMapper class. We indicate what it's mapped to in [] brackets

$data = [
     'type' => 'varchar',  
          // (string)[eav_attribute][backend_type]: database storage type (varchar|int|text|decimal)
     'backend' => NULL,      
          // (string)[eav_attribute][backend_model]: class name used to retrieve/save the attribute data to the db
     'frontend' => NULL,     
          // (string)[eav_attribute][frontend_model]: ????
     'label' => '',     
          // (string)[eav_attribute][frontend_label]: the label text
     'input' => 'text',    
          // (string)[text|textarea|date|boolean|multiselect|select|price|weee|media_image|swatch_visual|swatch_text][eav_attribute][frontend_input]: html element type 
     'class' => '',        
          // (string)[validate-number|validate-digits|validate-email|validate-url|validate-alpha|validate-alphanum][eav_attribute][fontend_class]: Input validatiaon class
     'source' => NULL,       
          // (string)[eav_attribute][source_model]: class that inhertis from the \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource class and is used to retrieve the valid options for a multiselect|select|boolean. If you omit this for one of these types, you should use the 'options' entry to define the valid options. This is critical as during import, your data will fail validation if you leave both blank.
     'required' => false,  
          // (boolean)[eav_attribute][is_required]: user must enter a value
     'user_defined' => false,   
          // (boolean)[eav_attribute][is_user_defined]: vs "system" defined. Appears to make indicate that the options and some of the settings are not editable by the user (i.e. dropdown options are not editable when this is false). Also, if you set to true, the backen_type will be set to a type based on the frontend input (i.e. select input will yeild int backend type and allow admin to add options).
     'default' => NULL,      
          // (mixed)[eav_attribute][default_value]: default value to set
     'unique' => false,    
          // (boolean)[eav_attribute][is_unique]: value must be unique in the defined scope (store|website|global). just like SKU
     'note' => '',   
          // (string)[eav_attribute]: A comment that appears below the frontend input.
     'option' => '',       
            // (array)[eav_attribute_option*]: Array of attribute options (get stored in table eav_attribute_option && 
            // eav_attribute_option_value.
            // if you look at the Magento\Eav\Setup\EavSetup::addAttributeOption() you see there are two
            //  structures that can be set here:
            //
            //      Structure 1:used to alter existing options
            //      [
            //      'value'=>[
            //          $optionId => [
            //              $storeId=>$value1, // ($storeId===0 is used as default value)
            //              $storeId=>$value2,
            //              $storeId=>$value3,
            //             ...
            //              ], 
            //          $optionId => [...],
            //         ],
            //      'order'=>[
            //          $optionId,$optionId,$optionId
            //          ],
            //      'delete'=>[
            //          $optionId,$optionId,$optionId
            //          ]
            //      ]
            //      
            //      Structure 2: used to insert ne options and values.
            //      ['values' => [
            //          $sortOrder => $label,
            //          $sortOrder => $label
            //          ...
            //          ]
            //      ]
            //      
     'sort_order' => 10, 
          // (int)[eav_entity_attribute]: Display order in backend / frontend
     'group' => 'General', 
          // (string)[eav_entity_attribute]: Name of group in product admin page to place attribtue
     'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
          // (int)[catalog_eav_attribute][is_global]: the highest level scope that this attribute can be used. 
          // See \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface,
     'visible' => true,    
          // (boolean)[catalog_eav_attribute][visible]: dispaly this in admin??
     'searchable' => false, 
          // (boolean)[catalog_eav_attribute][is_searchable]: If yes, search will include checking this field for values. Must be "Yes" to allow used in advanced search.
     'filterable' => false,        
          // (boolean)[catalog_eav_attribute][is_filterable]: Use in Layered Navigation (Dropdown, Multiple Select and Price only)
     'comparable' => false,        
          // (boolean)[catalog_eav_attribute][is_comparable]: Adds attribtue to compare list when user is comparing multiple products
     'visible_on_front' => false,   
          // (boolean)[catalog_eav_attribute][is_visible_on_front]: (Simple and virtual products only) To include the attribute on the Additional Information tab of the product page
     'is_html_allowed_on_front' => false, 
          // (boolean)[catalog_eav_attribute][is_html_allowed_on_front]: Allow html tags in attribute value
     'is_used_for_price_rules' => false,
          // (boolean)[catalog_eav_attribute][is_used_for_price_rules]: Use for conditions in catalog proce rule system
     'is_filterable_in_search' => false,
          // (boolean)[catalog_eav_attribute][is_filterable_in_search]: Use in search result layered navigation
     'used_in_product_listing' => false, 
          // (boolean)[catalog_eav_attribute][used_in_product_listing]: (Depends on theme) To include the attribute in product summaries that appear in catalog listings
     'used_for_sort_by' => false,
          // (boolean)[catalog_eav_attribute][used_for_sort_by]: Depending on theme, includes the attribute as a “Sort  By” option for catalog listings
     //'apply_to' => 'simple',
          // (string)[simple|bundle|...][catalog_eav_attribute][apply_to]: limit product type this applies to: 
     'is_visible_in_advanced_search' => false,	
          // (boolean)[catalog_eav_attribute][is_visible_in_advanced_search]: Will display this field in advanced search form. 'searchable' must be Yes.
     'position' => 0,
          // (int)[catalog_eav_attribute][position]: Position in layered navigation block
     'is_wysiwyg_enabled' => false,
          // (boolean)[catalog_eav_attribute][is_wysiwyg_enabled]: Enables the wysiwyg editor in admin
     'is_used_for_promo_rules' => false,
          // (boolean)[catalog_eav_attribute][is_used_for_promo_rules]: Use for Promo Rule Conditions
     'is_required_in_admin_store' => false,
          // (boolean)[catalog_eav_attribute][is_required_in_admin_store]: ???
     'is_used_in_grid' => true, 
          // (boolean)[catalog_eav_attribute][is_used_in_grid]: Add as a "Column Option" entry in admin UI. This is the list that can be chosen from to display as a column in the product grid.
     'is_visible_in_grid' => false, 
          // (boolean)[catalog_eav_attribute][is_visible_in_grid]: Adds the column to the product grid in admin.
     'is_filterable_in_grid' => true, 
          // (boolean)[catalog_eav_attribute][is_filterable_in_grid]: Adds a filter box to the top of the column in the admin product grid
     'search_weight' => 1,
          // (int)[catalog_eav_attribute][search_weight]: Weight used in search to rank....10 is ranked higher.
     'additional_data' => NULL,
          // (string)[catalog_eav_attribute][additional_data]:
     /* ElasticSearch module related */
     'is_used_in_autocomplete' => false,
          // (boolean)[catalog_eav_attribute][is_used_in_autocomplete]: used to find products during autocomplete activity
     'is_displayed_in_autocomplete' => false,
          // (boolean)[catalog_eav_attribute][is_displayed_in_autocomplete]: if value should be displayed in autocomplete dropdown on frontend search box
     'is_used_in_spellcheck' => false,
          // (boolean)[catalog_eav_attribute][is_used_in_spellcheck]: use field to account for misspelling
     'facet_min_coverage_rate' => 90,
          // (int)[catalog_eav_attribute][facet_min_coverage_rate]: percentage: Ex: Brand facet will be displayed only if 90% of the product have a brand.
     'facet_max_size' => 10,
          // (int)[catalog_eav_attribute][facet_max_size]: Max number of values returned by a facet query.
     'facet_sort_order' => '_count',
          // (string)[_count|_manual|_term|_score][catalog_eav_attribute][facet_sort_order]: _count:Result count, _manual:Admin sort, _term:Name, _score:Relevance
     'display_pattern' => NULL,
          // (string)[catalog_eav_attribute][display_pattern]: ???
     'display_precision' => 0,
          // (int)[catalog_eav_attribute][display_precision]: ???
     'is_reference_field' => false,
          // (boolean)[catalog_eav_attribute][is_reference_field]: Should be used on fields like SKU, EAN, references containing technical identifier (eg : containing values like MH08, 3459379414678 ...)
     /* ElasticSearch module related */
 ];      

$eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'come_attribute_code', $data);

TODO: Add the following (some may match up to the above...the system seems to map between the above and the table columns):

TABLE:: customer_eav_attribute stores extra properties about customer attributes:
     'is_visible' => 1,
     'input_filter' => '',
     'multiline_count' => 1,
     'is_system' => 1, (above)
     'sort_order' => 1, (above)
     'data_model' => '',
     'is_used_in_grid' => 1,
     'is_visible_in_grid' => 1,
     'is_filterable_in_grid' => 1,
     'is_searchable_in_grid' => 1,
     'validate_rules'=> [
      'max_text_length' => 5,
      'max_text_length' => 1,
      'input_validation' => 'email|date|??'
     ]



***eav attribute properties are defined as const in various API's:

So far I have found part of possible options in Magento\Catalog\Model\ResourceModel\Eav\Attribute and Magento\Catalog\Api\Data\EavAttributeInterface

const IS_WYSIWYG_ENABLED = 'is_wysiwyg_enabled';

const IS_HTML_ALLOWED_ON_FRONT = 'is_html_allowed_on_front';

const USED_FOR_SORT_BY = 'used_for_sort_by';

const IS_FILTERABLE = 'is_filterable';

const IS_FILTERABLE_IN_SEARCH = 'is_filterable_in_search';

const IS_USED_IN_GRID = 'is_used_in_grid';

const IS_VISIBLE_IN_GRID = 'is_visible_in_grid';

const IS_FILTERABLE_IN_GRID = 'is_filterable_in_grid';

const POSITION = 'position';

const APPLY_TO = 'apply_to';

const IS_SEARCHABLE = 'is_searchable';

const IS_VISIBLE_IN_ADVANCED_SEARCH = 'is_visible_in_advanced_search';

const IS_COMPARABLE = 'is_comparable';

const IS_USED_FOR_PROMO_RULES = 'is_used_for_promo_rules';

const IS_VISIBLE_ON_FRONT = 'is_visible_on_front';

const USED_IN_PRODUCT_LISTING = 'used_in_product_listing';

const IS_VISIBLE = 'is_visible';

const SCOPE_STORE_TEXT = 'store';

const SCOPE_GLOBAL_TEXT = 'global';

const SCOPE_WEBSITE_TEXT = 'website';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment