I'm trying to understand how the install works for EAV entities in Magento2.
I see that in the installEntities
method from the Magento\Customer\Setup\CustomerSetup
class, the attributes have 2 fields with the same value position
and sort_order
.
but in the catalog module in the Magento\Catalog\Setup\CategorySetup
there is only the sort_order
field.
Please explain what's the difference between them (if any) and for what is each one used.
2 Answers
I'm pretty sure it's used like an alias but I don't get why both position
and sort_order
are part of this script.
The only place I've noticed the use of the position
in the Customer module is under Magento/Customer/Model/ResourceModel/Setup/PropertyMapper.php
:
public function map(array $input, $entityTypeId)
{
return [
'is_visible' => $this->_getValue($input, 'visible', 1),
'is_system' => $this->_getValue($input, 'system', 1),
'input_filter' => $this->_getValue($input, 'input_filter', null),
'multiline_count' => $this->_getValue($input, 'multiline_count', 0),
'validate_rules' => $this->_getValue($input, 'validate_rules', null),
'data_model' => $this->_getValue($input, 'data', null),
'sort_order' => $this->_getValue($input, 'position', 0),
'is_used_in_grid' => $this->_getValue($input, 'is_used_in_grid', 0),
'is_visible_in_grid' => $this->_getValue($input, 'is_visible_in_grid', 0),
'is_filterable_in_grid' => $this->_getValue($input, 'is_filterable_in_grid', 0),
'is_searchable_in_grid' => $this->_getValue($input, 'is_searchable_in_grid', 0),
];
}
This method is used to map input attribute properties to storage representation.
It is used in the addAttribute()
method in Magento/Eav/Setup/EavSetup.php
:
public function addAttribute($entityTypeId, $code, array $attr)
{
$entityTypeId = $this->getEntityTypeId($entityTypeId);
$data = array_replace(
['entity_type_id' => $entityTypeId, 'attribute_code' => $code],
$this->attributeMapper->map($attr, $entityTypeId)
);
...
}
However, I don't see this addAttribute()
method being called in the Customer install script as it inserts multiple rows at the same time:
if ($data) {
$this->getSetup()->getConnection()
->insertMultiple($this->getSetup()->getTable('customer_form_attribute'), $data);
}
Maybe that's the reason why both position
and sort_order
are specified as the addAttribute()
is not called.
-
Ah. Good finding. I guess the
sort_order
is just a redundancy.– Marius ♦Commented Jul 6, 2016 at 8:34
It seems the sort_order
is the correct one as it is used at \Magento\Eav\Setup\EavSetup::addAttribute
.
$sortOrder = isset($attr['sort_order']) ? $attr['sort_order'] : null;
And its also the column name in customer_eav_attribute
table.
When looking into catalog_eav_attribute
, the position
field is always 0
. It seems its not used. Even if you change the position value the admin remains unchanged.