Skip to content
Vitaliy edited this page Dec 11, 2022 · 8 revisions

Code generation

Code generation is one of the most wanted features in the Magento community. There is a bunch of existing open-source solutions, however, generating code using the IDE seems to be the most convenient way to go. Therefore several actions were created to help with common Magento 2 customizations. This list will be extended in the future. All templates used for generation can be found and edited in Preferences/Settings | Editor | File and Code Templates. To use this functionality you need to right-click on the module directory (some items are available for other dirs) and select a needed item in the context menu.

New module

New Magento 2 modules can be created from the context menu or by pressing Ctrl(⌘)+N.

New module as a separate project

This should be useful for extension developers. When the project is created, the IDE suggests selecting the PHP version and suggests including the Magento source code to PHP including paths.

Plugin for a class public method

The action creates a plugin class method along with a plugin declaration record in di.xml in the scope of the specified module and area; a plugin class and di.xml are created if needed.

Reference

The action creates a class along with a preference declaration record in di.xml in the scope of the specified module and area.

Observer

New observers can be created from the context menu. Just right-click on the event name and specify the module, area, and observer name.

Block

View Model (class)

GraphQL Resolver

CLI command

CRON group

Controller

CRON job

View Model with layout declaration

The action for adding a view model to a block and a reference block from the context menu.

Override in Theme action

The action for copying a view file to the appropriate place in a custom place.

UI Component Grid

The action for creating a UI Component Grid (Listing).

Action creates the following files:

  • Action Controller PHP Class
  • menu.xml
  • routes.xml
  • acl.xml
  • Data Provider PHP Class
  • Layout file
  • UI component file

Example of filling out the form:

Result:

UI Component Form

The action for creating a UI Component Form.

Action creates the following files:

  • Buttons PHP Classes
  • Save Action Controller PHP Class
  • View Action Controller PHP Class
  • routes.xml
  • acl.xml
  • Data Provider PHP Class (or di.xml declaration)
  • Layout file
  • UI component file

Example of filling out the form:

Result:

Magento Entity (CRUD)

The action for adding the whole infrastructure for CRUD.

Usage

  • Create a new module e.g. Vendor_MyModule.

  • Open the context menu (right-click to the module directory). Click to New -> Module File -> Magento 2 Entity.

  • Specify the Entity Name, DB table name, and ID field name. Select the Create Admin UI Components checkbox.

  • Go to the Admin UI Components tab. Check whether all inputs are filled correctly.

  • Go to the ACL tab. Check inputs here as well.

  • Go to the Menu tab. Chose the correct parent menu and specify the sort order.

  • Go to the Properties tab. Add needed properties (Entity ID added by default).

  • Click the Ok button to get all files generated.

  • Check files (e.g data interface)

  • Run php bin/magento setup:upgrade. A new menu item should be accessible in the admin panel.

  • Click on the menu item to get to the newly added listing.

  • Click on the Add New button to get to the create/edit form.

  • Specify data and save the entity

Your entity is ready for extension. Enjoy!

Email templates

The action for adding Email Template files from the context menu.

Web API declaration

Web API interface for service (PHP class) generation

Layout XML file

Added context action for the layout.xml file generation

Page Types XML file

Crontab XML file

Email Templates XML file

Sections XML file

Fieldset XML file

View XML file

Indexer XML file

Added context action for the indexer.xml file generation

MVIEW XML file

Widget XML file

Extension Attributes XML file

System XML file

Config XML file

WEB API XML file

DI XML file

ACL XML file

Routes XML file

Data Patch with Customer EAV attribute

Data Patch with Category EAV attribute

Data Patch with Product EAV attribute

Code generation of Readme file

Code generation of GraphQl schema file

In editor generation of Plugin Methods

New plugin methods can be created from the context menu (generate...) or by pressing Ctrl(⌘)+N.

Inspections

Plugin inspection in the scope of PHP class

Inspection highlights the following cases:

  • Plugin declared for a final class
  • Plugin declared for a final method
  • Plugin declared for a constructor
  • Plugin declared for a not public method
  • Plugin declared for a static method
  • Incompatible parameter
  • Redundant parameter

Plugin duplication inspection in the scope of di.xml

This inspection highlights possible accidental plugin duplication. A Plugin name must be unique. In case if overriding is wanted, the best practice is to disable the original plugin and give a unique name to the current plugin.

Observer duplication inspection in the scope of events.xml

Inspection for observer declaration records, similar to plugin inspection.

GraphQL resolver inspection in the scope of PHP class (Requires GraphQLJS plugin)

If a class is configured in schema.GraphQLs as a resolver, it should implement one of the resolver interfaces.

Cacheable false in the scope of the default layout

This inspection highlights the cacheable attribute which is set to be false in default.xml as that can make the whole site uncacheable.

Module declaration in the scope of the module.xml and registration.php

Inspection checks whether the name of the module declared in app/code reflect the folder structure.

GraphQL resolver in the scope of a schema file

Inspection checks whether resolvers declared in schema implement resolver interfaces.

ACL resource inspection

Inspection check ACL resource with a unique identifier to have a title attribute.

Web API XML service tag attributes inspections

DI XML preference tag attributes inspections

DI XML type tag attributes inspections that related to the PHP/Magento types

The Type tag inspections (in the di.xml files) detect empty values or invalid values where a Type values (class, interface, virtual type) are expected.

It also supports recursive inspection for xsi:type="array" arguments.

DI XML plugin type attribute inspections

Virtual type check

Added inspection to check if the type attribute value in the virtual type tag of the di.xml is valid

For an empty value:

For an invalid value:

Reference navigation / Code completion

Line markers from a Plugin class to a Target class

MFTF support

Support of the most commonly used MFTF XML directives.

  • Page URL completion and reference

  • Action group completion and reference

  • Test extends attribute completion and reference

  • Selector completion and reference

RequireJS mapping support

The RequireJS configuration file requirejs-config.js is commonly used in the different areas of Magento 2.You can use it to create aliases for a component.

  • Reference on injected argument

  • Completion and reference in XML

GraphQL support (Optional, Requires GraphQLJS plugin)

Sometimes, you may need to quickly navigate from GraphQL resolver to schema and vice versa. We added line markers to make that easier. To use this functionality, install the GraphQLJS plugin.

PHP class and method reference in the crontab.xml

Parent menu item reference in the menu.xml

Disabled observers in events.xml.

Line markers from a Test class to a Fixture file (integration and API functional tests)

Code completion for configs

Code completion for system.xml

and config.xml

Copy Magento Path action

Ability to copy Magento Path for JS, PHTML, CSS, and other files.

Run the Upgrade Compatibility Tool

More info about UCT

Clone this wiki locally