This article is produced by scandiweb

scandiweb is the most certified Adobe Commerce (Magento) team globally, being a long-term official Adobe partner specializing in Commerce in EMEA and the Americas. eCommerce has been our core expertise for 20+ years.

Magento Series: Creating a Module

What is a module?

A module is a part of the code responsible for specific functionality. Modules are a great way to give any coding project a consistent, clear, and debuggable structure. They help teams develop code with parts that can be connected to or reused in other parts of the code. 

Questions to ask

Here are important questions to ask when developing a module for your coding project:

  • What will my module do?

    What problems does your module solve? What role does your module play in the context of Magento?
  • How to name my module?

    You need to decide what the vendor of the module and its name will be following the functionality that needs to be implemented.
  • Which modules must be loaded before my module can start working?

    You can specify the list of dependencies under the <sequence> tag in the module.xml file.
  • Which Magento 2 entities can interact with this module?

    You need to determine which Magento (Adobe Commerce) entities the module will interact with (customer, product, shipment, invoice, etc.), then specify the settings in di.xml file according to these interactions.
  • Do I want to share this module?

How should a module be named?

The name of the module must consist of two parts: vendor name and module name:

  • vendor name – If what you’re developing is a basic module that is not going to be shared across other projects, then the vendor name can be the same as the project name. If the module will be shared across projects or teams, you should specify your company (e.g., scandiweb) as the vendor of that module. It is a good practice to add the composer.json file to a shared module.
  • module name – This is what the module is called. The name of the module should be informative and should reflect the task that it solves.

Where should a module be located?

When developing a module, it is best to place its source code following the conventional folder structure:

/app/code/<VENDOR_NAME>/<MODULE_NAME>/

For instance, if the vendor name is scandiweb and the module name is AwesomeModule, then the module source code should be placed as follows:

/app/code/scandiweb/AwesomeModule/

There are other locations for modules in Magento 2 (Adobe Commerce) code:

  • /vendor/<VENDOR_NAME>/ – system modules, modules from third-party developers, and modules installed with the composer are placed here

    ➡️ It is not recommended to edit the code of modules from the vendor folder since they belong to the category of third-party code or system code. All the changes made will most likely disappear with the next system update.
  • /app/design/<VENDOR_NAME>/ – this is where you can place a module if you need to redefine the functionality of an existing module for the current theme

How to define a module

After developing a module, you need to tell Magento (Adobe Commerce) what they are and how to use them before they can work. You can do this by following these steps:

1. Create a module.xml file

Located in /VendorName/ModuleName/etс/module.xml

That file contains the corresponding module information (name, dependencies). For example:

<?xml version="1.0"?>
<config
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="VendorName_ModuleName">
	<!-- Dependencies can be placed here -->
    </module>
</config>

➡️ The module version is not controlled manually. It’s good practice to have a standard approach to managing modules within your organization. Declarative Schema is a good reference for monitoring the updates on the module code.

2. Create a registration.php file

Located in /VendorName/ModuleName/registration.php

That file is used to notify Magento about the existence of a module in a file system. For example:

<?php \Magento\Framework\Component\ComponentRegistrar::register(
	\Magento\Framework\Component\ComponentRegistrar::MODULE,
	'VendorName_ModuleName',
	__DIR__
);

How to set up a module

In case your module has the status disabled, you may enable it by running the CLI command:

magento module:enable <module_name>

Once your module is enabled, it’s time to set it up. And to this, you need to run a simple command from the CLI:

magento setup:upgrade

What if you’re using a CMA setup?

npm run cli                           # entering the CLI

$ magento module:enable <module_name> # enabling a module
$ magento setup:upgrade               # setting up a module

$ exit                                # leaving the CLI

➡️ The PHPStorm MaGinto plugin is a free extension that lets you quickly create extensions.

How to add a dependency

A dependency is generally a piece of code, functionality, or library that is a prerequisite for another part of the code to work. For the purposes of this article, the dependencies are the code of other modules, which Magento (Adobe Commerce) must load before executing the code of the current module.

To add a dependency, you should add the <sequence> tag inside the main module tag. For instance, let the DependencyName be a dependency from some vendor DependencyVendor:

<?xml version="1.0"?>
<config
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="VendorName_ModuleName">
	<sequence>
		<!-- Add dependencies here -->
		<module name="DependencyVendor_DependencyName" />
	</sequence>
    </module>
</config>

How to share a module

If you want to share your module, make sure the composer file and repository for the module are created so it can be published.

How to check the module state

You can check the status of your modules by typing the following command from the CLI:

magento module:status

Need help with Magento (Adobe Commerce) development? scandiweb is the most certified Magento team in the world—get your dedicated eCommerce team today or get in touch with one of our experts.

Need help with your eCommerce?

Get in touch for a free consultation to discuss your business and explore how we can help.

Your request will be processed by

If you enjoyed this post, you may also like