Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LayoutProcessor.php
Go to the documentation of this file.
1 <?php
7 
9 {
13  protected $merger;
14 
18  protected $countryCollection;
19 
23  protected $regionCollection;
24 
28  protected $defaultShippingAddress = null;
29 
33  private $topDestinationCountries;
34 
42  public function __construct(
43  \Magento\Checkout\Block\Checkout\AttributeMerger $merger,
44  \Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection,
45  \Magento\Directory\Model\ResourceModel\Region\Collection $regionCollection,
46  \Magento\Directory\Model\TopDestinationCountries $topDestinationCountries = null
47  ) {
48  $this->merger = $merger;
49  $this->countryCollection = $countryCollection;
50  $this->regionCollection = $regionCollection;
51  $this->topDestinationCountries = $topDestinationCountries ?:
53  ->get(\Magento\Directory\Model\TopDestinationCountries::class);
54  }
55 
62  protected function isCityActive()
63  {
64  return false;
65  }
66 
73  protected function isStateActive()
74  {
75  return false;
76  }
77 
85  public function process($jsLayout)
86  {
87  $elements = [
88  'city' => [
89  'visible' => $this->isCityActive(),
90  'formElement' => 'input',
91  'label' => __('City'),
92  'value' => null
93  ],
94  'country_id' => [
95  'visible' => true,
96  'formElement' => 'select',
97  'label' => __('Country'),
98  'options' => [],
99  'value' => null
100  ],
101  'region_id' => [
102  'visible' => true,
103  'formElement' => 'select',
104  'label' => __('State/Province'),
105  'options' => [],
106  'value' => null
107  ],
108  'postcode' => [
109  'visible' => true,
110  'formElement' => 'input',
111  'label' => __('Zip/Postal Code'),
112  'value' => null
113  ]
114  ];
115 
116  if (!isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
117  $jsLayout['components']['checkoutProvider']['dictionaries'] = [
118  'country_id' => $this->countryCollection->loadByStore()->setForegroundCountries(
119  $this->topDestinationCountries->getTopDestinations()
120  )->toOptionArray(),
121  'region_id' => $this->regionCollection->addAllowedCountriesFilter()->toOptionArray(),
122  ];
123  }
124  if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']
125  ['address-fieldsets']['children'])
126  ) {
127  $fieldSetPointer = &$jsLayout['components']['block-summary']['children']['block-shipping']
128  ['children']['address-fieldsets']['children'];
129  $fieldSetPointer = $this->merger->merge($elements, 'checkoutProvider', 'shippingAddress', $fieldSetPointer);
130  $fieldSetPointer['region_id']['config']['skipValidation'] = true;
131  }
132  return $jsLayout;
133  }
134 }
__()
Definition: __.php:13
__construct(\Magento\Checkout\Block\Checkout\AttributeMerger $merger, \Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection, \Magento\Directory\Model\ResourceModel\Region\Collection $regionCollection, \Magento\Directory\Model\TopDestinationCountries $topDestinationCountries=null)