6

Is anyone have an idea about convert XML to LayoutProcessor format? I have created a layout processor as given below. I need to only those sentence which is defined in XML.

\Magento\Checkout\Block\Checkout\LayoutProcessor

<item name="shipping-address-fieldset" xsi:type="array">
    <item name="children" xsi:type="array">
        <item name="telephone" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Mobile phone number</item>
                <item name="elementTmpl" xsi:type="string">Vendor_Customer/form/element/input</item>
            </item>
            <item name="validation" xsi:type="array">
                <item name="custom-validator-phone" xsi:type="boolean">true</item>
            </item>
        </item>
    </item>
</item>

/app/code/Vendor/Customer/Plugin/Checkout/LayoutProcessorPlugin.php

public function afterProcess(
    \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
    array $jsLayout
) {
    $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
    ['shippingAddress']['children']['shipping-address-fieldset']['children']['telephone']['children'] = [

        [
            'config' => ['elementTmpl' => 'ui/form/element/input'],
            'dataScope' => '1',
            'provider' => 'checkoutProvider',
            'validation' => ['custom-validator-phone' => true],
        ]
    ];
    return $jsLayout;
}

2 Answers 2

4

Lose the last ['children'].

$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['telephone']

and make 'config' => ['elementTmpl' => 'ui/form/element/input'] look like this

'config' => ['elementTmpl' => 'ui/form/element/input', 'label' => __('Mobile phone number')]
2
  • Thanks for quick reply! Let me check! Commented Dec 10, 2019 at 12:33
  • This is custom validation rule: 'validation' => ['custom-validator-phone' => true], Commented Dec 10, 2019 at 12:41
4

follow the below code.

create di.xml at app/code/Vendor/Extension/frotend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
        <plugin name="checkout_shipping" type="Vendor\Extension\Plugin\Checkout\Model\LayoutProcessor" sortOrder="150"/>
    </type>
</config>

then create plugin file with layoutprocessor.php at Vendor\Extension\Plugin\Checkout\Model\LayoutProcessor.php

<?php
namespace Vendor\Extension\Plugin\Checkout\Model;

use Magento\Checkout\Block\Checkout\LayoutProcessor;

class LayoutProcessor
{
    public function __construct(
        \Magento\Payment\Model\Config $paymentModelConfig
    )
    {
        $this->paymentModelConfig = $paymentModelConfig;
    }

    /* disable company from checkout page */
    public function afterProcess(
        LayoutProcessor $subject,
        array $jsLayout
    ) {
        /* For Disable Company field from checkout page shipping form */
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children']['telephone'] = [
            'config' => ['elementTmpl' => 'ui/form/element/input'],
        'dataScope' => '1',
        'provider' => 'checkoutProvider',
        'validation' => ['custom-validator-phone' => true],
        ];

        $activePayments = $this->paymentModelConfig->getActiveMethods();
        /* For Disable company field from checkout billing form */
        if (count($activePayments)) {
            foreach ($activePayments as $paymentCode => $payment) {
                $jsLayout['components']['checkout']['children']['steps']['children']
                ['billing-step']['children']['payment']['children']
                ['payments-list']['children'][$paymentCode.'-form']['children']
                ['form-fields']['children']['company'] = [
                    'config' => ['elementTmpl' => 'ui/form/element/input'],
                    'dataScope' => '1',
                     'provider' => 'checkoutProvider',
                     'validation' => ['custom-validator-phone' => true],
                ];
            }
        }

        return $jsLayout;
    }
}
1
  • Thanks for answerer! Your answer is also correct but you missed label. 'config' => ['elementTmpl' => 'ui/form/element/input', 'label' => __('Mobile phone number')] Commented Dec 11, 2019 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.