Magento 2 Certified Professional Developer Guide
Section 3: Customizing the Magento UI
3.1 Demonstrate ability to utilize themes and the template structure
Demonstrate the ability to customize the Magento UI using themes. When would you create a new theme?
In case there is a need to modify the web store layout, you can create a new theme to achieve this. To learn how to create a Magento theme, follow the link
How do you define theme hierarchy for your project?
To define theme hierarchy for your project, specify the parent theme in theme.xml file inside the theme:
... Magento/blank ...
Demonstrate the ability to customize/debug templates using the template fallback process
In order to customize a certain view file, override it in the current theme. Template fallback process is when Magento 2 searches for a file in the child theme, then in the parent themes and finally in modules. To learn more about this, follow the link:
There are the following methods of template debugging:
- XDebug (The main peculiarity of this method are breakpoints as well as the ability to view and modify variables at any time)
- Developer mode (the mode for development, displays errors directly on screen)
- Logs in folder var/logs and reports in folder var/reports
- Display Magento reports in browser
- Template path hints (displays templates’ paths and names)
To learn more about debugging methods, follow the link (https://belvg.com/blog/developer-mode-and-debugging-in-magento-2.html)
How do you identify which exact theme file is used in different situations?
To easier identify what template file is used in a certain place at the website, enable path hints for it.
First, log in to the admin panel and navigate to “Stores -> Configuration”.

Then, go to “Advanced -> Developer”. Expand the “Debug” tab and set “Enabled Template Path Hints for Storefront” attributes at Yes.

In case you have multiple stores, set in the upper right menu the one for which you enable path hints.
When there is a need to enable hints for a certain ip-address, expand the “Developer Client Restrictions” tab and enter the IP-address into the “Allowed IPs (comma separated)” field. If you need to enter several IP-addresses, separate them with commas.

Another way to enable or disable path hints is via CLI:
bin/magento dev:template-hints:enablebin/magento dev:template-hints:disable
As you have completed the course of actions described above, path hints will appear at the front end of the store.

Following the highlighted paths, we can easily find the needed templates files.
If you know the needed file’s unique line (for example, class name, attributes, etc.), then you can search for it among the project files. In PhpStorm, find the project files folder, click the right mouse button, select “Find in Path…” and insert the unique line in the appeared window.
Another way to search for files is using grep command: In order to override the parent theme files, put the file in the current theme folder at the same path it was located in the parent theme. Example: If you need to override a file in the parent theme In order to override module’s view files, create them at the same path as they were located in the module’s view folder, but deleting from the path and adding the module name. Example: If we want to override the module file vendor/magento/module-catalog/view/frontend/templates/product/view/gallery.phtml, create a file in the theme This method overrides templates files and all the files in web folder. However, if you try to override the layout file the following way, instead of overriding the parent theme file will be added up to. To learn more about the layout files override in Magento 2, follow the link Blocks are PHP classes that provide data for template files (.phtml) and implement rendering. Also, blocks can cache the results of template rendering. All blocks extend \Magento\Framework\View\Element\Template, so by default blocks are injected with a \Magento\Framework\View\ Element\Template\Context object and support getters and setters for returning and storage of any objects or data. This object contains a number of other objects that are loaded into the instance: Separation of blocks and templates allows you to divide design related logic and design. Blocks are usually, but not always, connected to PHTML template files. Blocks can be thought of as data containers for a template, which represents a piece of the HTML on the page. In layouts XML, you can manage blocks on page and add new ones, set templates to move and delete. The block life cycle consists of two phases: generating blocks and rendering blocks. Blocks are instantiated at the moment the layout is created. They are not executed at that time, just instantiated. Also during this phase, the structure is built, the children of blocks are set, and for each block, the prepareLayout() method is called. However, rendering occurs in the later rendering phase. Generating phase: GeneratePool goes through all generators and generates all scheduled elements. It has generators with the following elements: Magento\Framework\View\Page\Config\Generator\Head Magento\Framework\View\Page\Config\Generator\Body Magento\Framework\View\Layout\Generator\Block Magento\Framework\View\Layout\Generator\Container Magento\Framework\View\Layout\Generator\UiComponent Rendering phase: At this moment, we already have all the layout xml for the generated page, and all block classes are created. In this method, we check the type of rendered elements. It creates them and returns html using toHtml() method. This method is not recommended to override. If you want to change block rendering, override _toHtml() method. _prepareLayout() – most often used for: _beforeToHtml() Magento\Shipping\Block\Adminhtml\Create\Items::_beforeToHtml() _toHtml() Using this method, you can manipulate block rendering, complement the condition, make wrappers for html, change the template for the block, etc. Magento\GroupedProduct\Block\Order\Email\Items\Order\Grouped::_toHtml() , Magento\Sales\Block\Reorder\Sidebar::_toHtml() view_block_abstract_to_html_before – use for editing params: view_block_abstract_to_html_after – use for html manipulation – editing, adding conditions, wrappers The most important method for rendering a block is Magento\Framework\View\Element\AbstractBlock::toHtml(). First, it runs _loadCache(), and if the cache is missing, then it run _beforeToHtml() after the block is rendered by the method_toHtml(). Afterward, the cache is saved _saveCache($html) and run _afterToHtml($html). Method _loadCache() uses cache_key return by getCacheKey() and _saveCache($html) – cache_tags obtained by the method getCacheTags(). Cache_key each block is added up like BLOCK_CLASS::CACHE_KEY_PREFIX. $cache_key , if this property not defined – BLOCK_CLASS::CACHE_KEY_PREFIX . sha1(implode(‘|’, $this->getCacheKeyInfo())). Cache_tags is an array and consists of a property $cache_tags, if it defined in block and if block instance of Magento\Framework\DataObject\IdentityInterface values returned by the method getIdentities() are added to the array. We can manage $cache_lifetime variable. Value will be in seconds, if you want to disable cache, you can set value to 0 or not set the value at all, because all blocks are non-cacheable by default. Applying non-template block types is wise when we use simple renderers. Another reason for using such block types is when block content is dynamic generated or stored in database or in containers. For example, if you want form_key in template, you can insert block Magento\Framework\View\Element\FormKey. Using a template block or other block types is recommended when complicated renderers are used, or when you want to customize themes or add new blocks. А prime example is a breadcrumbs block Magento\Catalog\Block\Breadcrumbs. If you want to customize breadcrumbs, you can set template in layout. Magento layouts contain instructions that allow to: Let us examine the cases of each instruction application: To add static resources to the page, first create default_head_blocks.xml file at the following path: Add a CSS file with the following instruction: To add a locally located JavaScript file, use one of the following instructions: In order to connect an external resource,add src_type attribute with “url” value.For example: To add a font,apply link tag and add attributes rel=”stylesheet” and type=”text/css”: To add a new container,use the following instructions: To modify a container(to add a block,for example),apply referenceContainer instruction: To create a block,apply block instruction.For example: To modify a block,apply referenceBlock instruction.Let us use the following block as an example: To modify its argument and add a new one,apply the following instruction: In case you need to set a template for a block,there are two ways to do this. Method 1:usingtemplateattribute: Method 2:using an argument with a name template: In our examples,new_template.phtml is the path to the template file,relatively to template folder( It should be noted that the templates,set with template attribute,have a higher priority.Therefore,the value in the template attribute will rewrite the one,specified with the help of argument. Before and after attributes allow to change the element order inside the parent.Set the name of the element,before or after which we want to place the needed element,as the attributes’ values.Using move instruction,we can not only change the element’s order,but also modify the parent’s order.For example,the following construction will relocate the block named product.info.review into the container with the name product.info.main,and will place it before the container named product.info.price: To delete a block or container,use the remove attribute of the referenceBlock or referenceContainer instructions correspondingly.For instance,to delete a catalog.compare.sidebar block,apply the following instruction: To create a layout file,you first need to create an XML file with the name layout handle at one of the following paths: To pass the variables’ values from layout to block,apply Magento 2 has a number of instructions,allowing to modify layout in nearly any way.But first,we need to find out how to modify a layout for a particular page.This can be done in configuration XML file of the page,using layout attribute of the page root node.For instance,to modify page Advanced Search layout from the default 1-column to 2-column with left bar,place at the following path Javascript module is a separate*.js file that can import other require.js modules.It has the similar contents: Plain module is a common module that is not inherited from others.You will find an example of a plain module above(Vendor_Module/js/script).In the examples below we will use modules that are inherited with jQuery.widget,uiElement.extend functions.This type of modules should be used when instances of the module are not connected to any element. jQuery UI widget allows to create a custom jQuery UI widget with a custom handler.This type of module should be used when instances of the module are connected to certain elements that are not UI components. Let us examine this case using the following file: vendor/magento/module-multishipping/view/frontend/web/js/payment.js: UiComponents allow to create a custom widget with a custom handler,inherited from uiElement.This type of module is recommended to use when instances of the module are connected to UI components. Let us examine this case using the following file: vendor/magento/module-catalog/view/frontend/web/js/storage-manager.js: UI components are realized using XML configuration+PHP modifiers and data providers+HTML knockout templates+JS module,based on uiElement+CSS/LESS/SASS styles.They allow to simplify development and significantly decrease code duplication. Out-of-the-box UI components are heavily used in adminhtml,while at the frontend – only in checkout(the UI components are added into checkout via layout).If JS module is a component inside checkout or in form or grid in adminhtml,then apply UI components.Otherwise,use regular JS module. Requirejs-config.js files(located at the To insert a JS component into a PHTML template,use one of the following variants: In case you need to initialize JS module with connection to HTML Element, there are two ways to do this: 1) Use data-mage-init attribute of the element, to which the component should be connected. For example: 2) Use Initialization in PHTML template is commonly used to pass parameters from php to javascript module. But we can also initialize javascript module in another javascript file. Example: vendor/magento/module-swatches/view/adminhtml/web/js/visual.jsgrep -R "How can you override native files?
3.2 Determine how to use blocks
Demonstrate an understanding of block architecture and its use in development
Which objects are accessible from the block?
What is the typical block’s role?
Identify the stages in lifecycle of a block
In what cases would you put your code in the _prepareLayout(), _beforeToHtml(), and _toHtml() methods?
How would you use events fired in the abstract block?
Describe how blocks are rendered and cached
Identify the uses of different types of blocks
When would you use non-template block types?
In what situation should you use a template block or other block types?
3.3 Demonstrate ability to use layout and XML schema
Describe the elements of the Magento layout XML schema, including the major XML directives. How do you use layout XML directives in your customizations?
Adding and deleting static resources
Adding or modifying containers
Adding or modifying blocks.Modifying block template and parameters
Changing of the order or the location of blocks and containers.Deleting blocks and containers
Describe how to create a new layout XML file
Describe how to pass variables from layout to block
How to modify existing layout files
3.4 Utilize JavaScript in Magento
Describe different types and uses of JavaScript modules.Which JavaScript modules are suited for which tasks?
define(['jquery'],function($)
return function(...){...};});define(['jquery','Vendor_Module/js/script'],function($,myModule)
return function(...){...myModule(...);...};});Plain modules
jQuery UI widgets
define(['jquery','mage/template','Magento_Ui/js/modal/alert','jquery/ui','mage/translate'],function($,mageTemplate,alert){'use strict';$.widget('mage.payment',{options:{...},_create:function(){...},});return $.mage.payment;});UiComponents
define(['underscore','uiElement','mageUtils','Magento_Catalog/js/product/storage/storage-service','Magento_Customer/js/section-config','jquery'],function(_,Element,utils,storage,sectionConfig,$){'use strict';...return Element.extend({defaults:{...},initialize:function(){...return this;},});});Describe UI components.In which situation would you use UiComponent versus a regular JavaScript module?
Describe the use of requirejs-config.js,x-magento-init,and data-mage-init
…
$('[data-role=swatch-visual-options-container]').sortable({
distance: 8,
tolerance: 'pointer',
cancel: 'input, button',
axis: 'y',
....
}
});
...

Tell us about your project
Get in touch with our team. Send us an email at [email protected] or call us 1 650 353 2301