An attribute may require additional functionality. In this article, you will learn which customizations can be implemented in Magento 2 and how it’s done. Please check out Part 1, Part 2 and Part 3 of the EAV series to catch up on the topic.
Additional options for saving EAV entities
Here are the options we have on the EAV Entity editing page:
- The default value of the attribute. Allows you to specify the value that will be substituted in the attribute field, if it is empty. This simplifies the procedure of adding and modifying entities. Here are the possible default attribute value types for an item:
- Text Field — any single-line text
- Text Area — any multiline text
- Date — any date
- Yes/No — “yes” or “no”
- Multiple Select — any number of options
- Dropdown — one option
- Price — no default value
- Media image — no default value
- Visual Swatch — one option
- Text Swatch — one option
- Fixed Product Tax — no default value
- Selecting a scope. The scope allows you to specify different attribute values for different websites / stores / views. Here are the possible values of the scope attribute for an item:
- Text Field — store view / website / global
- Text Area — store view / website / global
- Date — store view / website / global
- Yes/No — store view / website / global
- Multiple Select — store view / website / global
- Dropdown — store view / website / global
- Price — website / global (настраивается в Stores / Configuration / Catalog / Catalog / Price / Catalog Price Scope)
- Media image — store view / website / global
- Visual Swatch — store view / website / global
- Text Swatch — store view / website / global
- Fixed Product Tax — global
- Selecting an attribute set (products only). Allows you to regroup or hide attributes that are not suitable for this type of product.
Magento Custom Development
Take your online store to the next level with BelVG Magento Custom Development
Visit the pageBefore saving the EAV entity on the client side, we have the following options:
- The prohibition of storing entities if there are blank fields of attributes that have the is_required = 1 property. It lets you specify which entity attributes must be filled in.
- Validation of the attribute field values using the algorithm specified in the frontend_class.
The following frontend classes are supported by Magento out-of-the-box:
-
- validate-number: Decimal Number
- validate-digits: Integer Number
- validate-email: Email
- validate-url: URL
- validate-alpha: Letters
- validate-alphanum: Letters (a-z, A-Z) or Numbers (0-9)
When saving the EAV entity on the server side, we have the following options:
- Checking if the attribute fields with is_required = 1 are filled. Magento checks the required fields on both the client and the server sides.
- Checking if the attribute fields with is_required = 1 are unique.
- Performing operations in the backend_model:
- Executing validation (validate method). Allows you to implement additional checks on the server before saving.
- Performing operations before saving (beforeSave method)
- Executing the operation after saving (afterSave method)
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.The module catalog has an additional catalog_eav_attribute table for the attributes in which the following parameters are stored:
- Frontend Input Renderer
- Is Global
- Is Visible
- Is Searchable
- Is Filterable
- Is Comparable
- Is Visible On Front
- Is HTML Allowed On Front
- Is Used For Price Rules
- Is Filterable In Search
- Is Used In Product Listing
- Is Used For Sorting
- Is Visible In Advanced Search
- Is WYSIWYG Enabled
- Is Used For Promo Rules
- Is Required In Admin Store
- Is Used in Grid
- Is Visible in Grid
- Is Filterable in Grid
These settings allow you to fine-tune the attribute.
Creating customizations based on changes to attribute values
To customize an attribute, you can change the following attribute properties:
- backend_model
- source_model
- attribute_model
- frontend_model
Attribute Model
Allows the fine-tuning of an attribute. The default model (Magento\Eav\Model\Entity\Attribute) is predominantly used.
Backend Model
The model is used to process and validate attribute values. Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend is used by default.
It allows overriding:
- validate
- afterLoad
- beforeSave
- afterSave
- beforeDelete
- afterDelete
- etc.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
class TestBackend extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend { public function validate($object) { $attribute_code = $this->getAttribute()->getAttributeCode(); $value = $object->getData($attribute_code); if ($value == 'test') { throw new \Magento\Framework\Exception\LocalizedException(__("Value can't be test")); } return true; } } |
Source Model
The model is used to provide a list of attribute values.
Magento\Eav\Model\Entity\Attribute\Source\Config is used by default.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class TestSource extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource { public function getAllOptions() { if (!$this->_options) { $this->_options = [ ['label' => __('Label 1'), 'value' => 'value 1'], ['label' => __('Label 2'), 'value' => 'value 2'], ['label' => __('Label 3'), 'value' => 'value 3'], ['label' => __('Label 4'), 'value' => 'value 4'] ]; } return $this->_options; } } |
Frontend Model
The model is used to display the values in the frontend part of the site.
Magento\Eav\Model\Entity\Attribute\Frontend\DefaultFrontend is used by default.
Example:
1 2 3 4 5 6 7 8 9 |
class TestFrontend extends \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend { public function getValue(\Magento\Framework\DataObject $object) { $attribute_code = $this->getAttribute()->getAttributeCode(); $value = $object->getData($attribute_code); return nl2br(htmlspecialchars($value)); } } |
That the final part of point 5.1: Demonstrate the ability to use EAV model concepts Describe the EAV hierarchy structure. We will continue the EAV topic with point 5.2: Demonstrate the ability to use EAV entity load and save Describe the EAV load and save process and differences from the flat table load and save process.
Looking for a professional Magento development team? BelVG is here to help!
Magento Development Services
Take your online store to the next level with BelVG Magento development
Visit the page