PHP code example of pragmatic-modules / jslayout-parser
1. Go to this page and download the library: Download pragmatic-modules/jslayout-parser library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pragmatic-modules / jslayout-parser example snippets
declare(strict_types=1);
namespace Pragmatic\Example\Block\Checkout;
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
use Pragmatic\JsLayoutParser\Api\ComponentInterface;
use Pragmatic\JsLayoutParser\Model\JsLayoutParser;
class ExampleProcessor implements LayoutProcessorInterface
{
/** @var JsLayoutParser */
private $jsLayoutParser;
public function __construct(JsLayoutParser $jsLayoutParser)
{
$this->jsLayoutParser = $jsLayoutParser;
}
public function process($jsLayout) : array
{
/** @var ComponentInterface $component */
$component = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if ($shippingAddress = $component->getNestedChild('steps.shipping-step.shippingAddress')) {
$shippingAddress->setComponent('Vendor_Module/js/view/shipping');
$shippingAddress->setIsVisible(false);
}
$jsLayout['components']['checkout'] = $component->asArray();
return $jsLayout;
}
}
$component = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if ($shippingAddress = $component->getNestedChild('steps.shipping-step.shippingAddress')) {
$shippingAddress->setComponent('Vendor_Module/js/view/shipping');
$shippingAddress->setIsVisible(false);
}
$jsLayout['components']['checkout'] = $component->asArray();
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
$componentName = $checkout->getComponentName(); // returns 'checkout'
$componentName = 'checkout';
$checkout = $jsLayout['components'][$componentName];
$steps = $jsLayout['components']['checkout']['steps'];
// $steps tells you nothing about parent
$checkout = [
'componentName' => 'checkout',
'data' => $jsLayout['components']['checkout']
];
$componentName = $checkout['componentName'] // 'checkout'
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
$parent = $checkout->getParent(); // returns null
if($steps = $checkout->getChild('steps')) {
$parent = $steps->getParent(); // returns $checkout object
}
$checkout = $jsLayout['components']['checkout'];
$parent = null;
if(isset($checkout['steps'])) {
$steps = $checkout['steps'];
$parent = $checkout;
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($steps = $checkout->getChild('steps')) {
$steps->remove();
}
if(isset($jsLayout['components']['checkout']['steps'])) {
unset($jsLayout['components']['checkout']['steps']);
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($checkout->hasChild('steps')) {
// do something
}
if($checkout->hasChild('non-existing-child')) {
// this won't execute
}
if(isset($jsLayout['component']['checkout']['children']['steps'])) {
// do something
}
if(isset($jsLayout['component']['checkout']['children']['non-existing-child'])) {
// this won't execute
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($child = $checkout->getChild('steps')) {
// do something with child
}
if(isset($jsLayout['components']['checkout']['children']['steps'])) {
$child = $jsLayout['components']['checkout']['children']['steps'];
// do something with child
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
/** @var \Pragmatic\JsLayoutParser\Model\ComponentFactory */
$child = $this->componentFactory->create([
'componentName' => 'example',
'data' => [
'component' => 'Magento_Ui/js/form/element/abstract',
'label' => 'Example component',
'provider' => 'checkoutProvider'
]
]);
if(!$checkout->hasChild('example')) {
$checkout->addChild($child);
}
if(!isset($jsLayout['components']['checkout']['children']['example'])) {
$jsLayout['components']['checkout']['children']['example'] = [
'component' => 'Magento_Ui/js/form/element/abstract',
'label' => 'Example component',
'provider' => 'checkoutProvider'
];
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($checkout->hasChild('steps')) {
$checkout->removeChild('steps');
}
if(isset($jsLayout['components']['checkout']['children']['steps'])) {
unset($jsLayout['components']['checkout']['children']['steps']);
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($checkout->hasNestedChild('steps.shipping-step.shippingAddress')) {
// do something
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress'])
) {
// do something
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($shippingAddress = $checkout->getNestedChild('steps.shipping-step.shippingAddress')) {
// do something with $shippingAddress
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']
)) {
$shippingAddress = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress'];
// do something with $shippingAddress
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($checkout->hasNestedChild('steps.shipping-step.shippingAddress') &&
$checkout->hasChild('steps')
) {
$checkout->moveNestedChild('steps.shipping-step.shippingAddress', 'steps');
}
$checkout->hasNestedChild('steps.shipping-step.shippingAddress'); // false
$checkout->hasNestedChild('steps.shippingAddress'); // true
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']
) && isset($jsLayout['components']['checkout']['children']['steps'])
) {
$steps = &$jsLayout['components']['checkout']['children']
['steps']['children'];
$shippingAddress = $steps['shipping-step']['children']['shippingAddress'];
unset($steps['shipping-step']['children']['shippingAddress']);
$steps['shippingAddress'] = $shippingAddress;
}
isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']
); // false
isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shippingAddress']
); // true
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($checkout->hasNestedChild('steps.shipping-step.shippingAddress')) {
$checkout->removeNestedChild('steps.shipping-step.shippingAddress');
}
$checkout->hasNestedChild('steps.shipping-step.shippingAddress'); // false
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']
)) {
unset($steps['shipping-step']['children']['shippingAddress']);
}
isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']
); // false
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
$checkout->hasChildren() // returns true
(isset($jsLayout['components']['checkout']['children']) &&
count($jsLayout['components']['checkout']['children']) > 0); // returns true
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
$checkout->getChildren(); // returns array with 'steps' component
$jsLayout['components']['checkout']['children'] ?? [] // returns array with 'steps' component
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
$steps = $checkout->getChild('steps');
$steps->isChildOf($checkout); // returns true
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($shippingAddress = $checkout->getNestedChild('steps.shipping-step.shippingAddress')) {
$component = $shippingAddress->getComponent(); // returns 'Magento_Checkout/js/view/shipping'
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']
)) {
$shippingAddress = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress'];
$component = $shippingAddress['component'] // 'Magento_Checkout/js/view/shipping'
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($shippingAddress = $checkout->getNestedChild('steps.shipping-step.shippingAddress')) {
$shippingAddress->setComponent('Vendor_Module/js/view/shipping')
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']
)) {
$shippingAddress = &$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress'];
$shippingAddress['component'] = 'Vendor_Module/js/view/shipping';
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
$config = $regionId->getConfig();
/** $config is an array:
[
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/select',
'customEntry' => 'shippingAddress.region',
]
*/
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id']
)) {
$regionId = &$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id'];
$config = $regionId['config'];
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
$config = $shippingAddress->setConfig([
'template' => 'Vendor_Module/form/field'
]);
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id']
)) {
$regionId = &$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id'];
$regionId['config']['template'] = 'Vendor_Module/form/field';
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
$dataScope = $regionId->getDataScope(); // returns 'shippingAddress.region_id'
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id']
)) {
$dataScope = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id']['dataScope'] ?? null; // 'shippingAddress.region_id'
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
$regionId->setDataScope('shippingAddress.some_region');
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id']
)) {
$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id']['dataScope'] = 'shippingAddress.some_region';
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($shippingFieldset = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset')) {
$displayArea = $shippingFieldset->getDisplayArea(); // returns 'additional-fieldsets'
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']
)) {
$displayArea = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['displayArea'] ?? null; // 'additional-fieldsets'
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($shippingFieldset = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset')) {
$shippingFieldset->setDisplayArea('summary');
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']
)) {
$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['displayArea'] = 'summary';
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
$label = $postcode->getLabel(); // returns 'Zip/Postal Code'
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']
)) {
$label = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']['label'] ?? null; // 'Zip/Postal Code'
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
$label = $postcode->setLabel(__('ZIP'));
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']
)) {
$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']['label'] = __('ZIP');
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
$provider = $postcode->getProvider(); // returns 'checkoutProvider'
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']
)) {
$provider = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']['provider'] ?? null; // 'checkoutProvider'
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
$postcode->setProvider('vendorProvider');
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']
)) {
$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']['provider'] = 'vendorProvider';
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
$sortOrder = $postcode->getSortOrder(); // returns '110'
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']
)) {
$sortOrder = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']['sortOrder'] ?? null; // '110'
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
$postcode->setSortOrder('150');
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']
)) {
$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']['sortOrder'] = '150';
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
$validation = $postcode->getValidation(); // returns ['
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']
)) {
$validation = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']['validation'] ?? null; // ['
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
$postcode->setValidation([
'
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']
)) {
$validation = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']['validation'] = [
'
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
$filterBy = $regionId->getFilterBy();
// returns
// [
// 'target' => '${ $.provider }:${ $.parentScope }.country_id',
// 'field' => 'country_id',
// ]
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id']
)) {
$filterBy = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id']['filterBy'] ?? null;
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
$regionId->setFilterBy(null);
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id']
)) {
$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['region_id']['filterBy'] = null;
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
$isVisible = $postcode->isVisible(); // returns true
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']
)) {
$isVisible = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']['visible'] ?? false; // true
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
$postcode->setIsVisible(false);
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']
)) {
$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['postcode']['visible'] = false;
}
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($street = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.street')) {
$isRequired = $street->isRequired(); // returns true
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['street']
)) {
$isRequired = $jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['street']['
$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
if($street = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.street')) {
$street->setIsRequired(false);
}
if(isset($jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['street']
)) {
$jsLayout['components']['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children']
['street']['