How EAV Data Storage Works in Magento 2: Part 4

How EAV Data Storage Works in Magento 2: Part 4

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.

Before 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)

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:

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:

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:

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.



Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *