This is a short tutorial in which we will learn to create a product attribute using Data Patches in Magento 2.3
Check also my article here about Declarative Schema to learn how to manipulate the database in a complete new way starting from Magento 2.3
Context
A data patch is a class that contains data modification instructions. It is defined in a <Vendor>/<Module_Name>/Setup/Patch/Data/<Patch_Name>.php
file and implements \Magento\Framework\Setup\Patch\DataPatchInterface
.
A schema patch contains custom schema modification instructions. These modifications can be complex. It is defined in a <Vendor>/<Module_Name>/Setup/Patch/Schema/<Patch_Name>.php
file and implements \Magento\Framework\Setup\Patch\SchemaPatchInterface
.
Unlike the declarative schema approach, patches will only be applied once. A list of applied patches is stored in the patch_list
database table. An unapplied patch will be applied when running the setup:upgrade
from the Magento CLI.
Data patches have three important methods: getDependencies
, getAliases
, apply
Let’s say we want to create an attribute for the products in which we want to store an embedded video code. So we want to create a new attribute video.
Create the Data Patch class
Lets create the file <Vendor>/<Module_Name>/Setup/Patch/Data/AddVideoToProduct.php
in which we define the class AddVideoToProduct
.
This class needs to implement the DataPatchInterface
interface and implement the methods getDependencies
, getAliases
, apply
To create the product attribute we need to use Magento\Eav\Setup\EavSetupFactory
so we would use DI to inject it into out class.
See the full code bellow:
<?php namespace Vendor\Module\Setup\Patch\Data; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\Patch\DataPatchInterface; class AddVideoToProduct implements DataPatchInterface { /** * @var \Magento\Framework\Setup\ModuleDataSetupInterface */ private $moduleDataSetup; /** @var EavSetupFactory */ private $eavSetupFactory; /** * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup */ public function __construct( \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory ) { $this->eavSetupFactory = $eavSetupFactory; $this->moduleDataSetup = $moduleDataSetup; } /** * {@inheritdoc} */ public function apply() { $this->moduleDataSetup->getConnection()->startSetup(); $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'video', [ 'type' => 'text', 'frontend' => '', 'label' => 'Video', 'input' => 'textarea', 'class' => '', 'source' => '', 'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend', 'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL, 'visible' => true, 'required' => false, 'user_defined' => false, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => true, 'used_in_product_listing' => false, 'is_wysiwyg_enabled' => false, 'unique' => false, 'apply_to' => '' ] ); $this->moduleDataSetup->getConnection()->endSetup(); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } }
After we define the class we need to run:
php bin/magento setup:upgrade
Conclusion
It’s fairly easy to create a product attribute using Data Patches in Magento 2.3, not that different from Upgrade Data Scripts. Let me know what you think and if you already started using Declarative Schema and Data and Schema Patches.
You can find more info on Data Patches on the official docs here.