Skip to content

Instantly share code, notes, and snippets.

View vasilii-b's full-sized avatar
🙂

Vasilii Burlacu vasilii-b

🙂
View GitHub Profile
@vasilii-b
vasilii-b / magento2-view-url-in-html.html
Last active July 27, 2018 11:22
Loading theme images into KO templates (HTML files)
<img data-bind="attr: { 'src': require.toUrl('images/loader-2.gif')" />
@vasilii-b
vasilii-b / magento2-layout-empty-container.xml
Last active January 14, 2021 23:58
Adding an empty container in Magento 2 layout file
<container name="some.container" htmlTag="div" htmlClass="container">
<!-- Force container to render -->
<block class="Magento\Framework\View\Element\Text">
<arguments>
<argument name="text" xsi:type="string"><![CDATA[&nbsp;]]></argument>
</arguments>
</block>
</container>
@vasilii-b
vasilii-b / magento2-body-class.xml
Created July 27, 2018 11:21
Set body class using XML layout
<?xml version="1.0" encoding="UTF-8" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<attribute name="class" value="custom-class"/>
</body>
</page>
<?php
namespace Vendor\ModuleName\ViewModel;
use Magento\Framework\View\Element\Block\ArgumentInterface;
/**
* ClassViewModel
*/
class ClassViewModel implements ArgumentInterface
@vasilii-b
vasilii-b / m2-nested-array-manipulation.md
Created August 21, 2018 06:54
Magento 2 way to manipulate with nested arrays. Out of the box

Magento\Framework\Stdlib\ArrayManager - Provides methods for nested array manipulations

@vasilii-b
vasilii-b / m2-remove-css-js-from-head.xml
Created August 24, 2018 09:05
Magento 2 XML - remove CSS or JS file from head
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<remove src="name.css"/>
</head>
</page>
@vasilii-b
vasilii-b / js-file-upload-file-size-validation.js
Created February 27, 2019 07:11
Checks whenever the uploaded file matches allowed file maximum size
var uploadField = document.getElementById("file");
uploadField.onchange = function() {
if(this.files[0].size > 2097152){ // 2MB
alert("File is too big!");
this.value = "";
};
};
@vasilii-b
vasilii-b / html-file-upload-allowed-extensions.html
Created February 27, 2019 07:16
HTML attribute that prevents users from uploading files from not allowed list of extensions
<!-- (IE 10+, Edge, Chrome, Firefox 42+) -->
<input type="file" accept=".xls,.xlsx" />
@vasilii-b
vasilii-b / m2-php-emulate-area-code.php
Last active September 16, 2024 13:16
Shows example of usage of Magento 2 emulate area code
<?php
declare(strict_types=1);
namespace Vendor\Module\Service;
use Magento\Framework\App\Area as AppArea;
use Magento\Framework\App\State as AppState;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\NoSuchEntityException;
@vasilii-b
vasilii-b / m2-set-page-title-from-xml.xml
Created March 20, 2019 12:15
Set/Update page title via layout xml file
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="page.main.title">
<action method="setPageTitle">
<argument translate="true" name="title" xsi:type="string">New page title</argument>
</action>
</referenceBlock>
</body>
</page>