Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Edit.php
Go to the documentation of this file.
1 <?php
7 
9 
18 {
22  protected $_address = null;
23 
27  protected $_customerSession;
28 
33 
38 
42  protected $currentCustomer;
43 
47  protected $dataObjectHelper;
48 
67  public function __construct(
68  \Magento\Framework\View\Element\Template\Context $context,
69  \Magento\Directory\Helper\Data $directoryHelper,
70  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
71  \Magento\Framework\App\Cache\Type\Config $configCacheType,
72  \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
73  \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
74  \Magento\Customer\Model\Session $customerSession,
76  \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory,
77  \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
78  \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
79  array $data = []
80  ) {
81  $this->_customerSession = $customerSession;
82  $this->_addressRepository = $addressRepository;
83  $this->addressDataFactory = $addressDataFactory;
84  $this->currentCustomer = $currentCustomer;
85  $this->dataObjectHelper = $dataObjectHelper;
86  parent::__construct(
87  $context,
89  $jsonEncoder,
90  $configCacheType,
91  $regionCollectionFactory,
92  $countryCollectionFactory,
93  $data
94  );
95  }
96 
102  protected function _prepareLayout()
103  {
104  parent::_prepareLayout();
105 
106  // Init address object
107  if ($addressId = $this->getRequest()->getParam('id')) {
108  try {
109  $this->_address = $this->_addressRepository->getById($addressId);
110  if ($this->_address->getCustomerId() != $this->_customerSession->getCustomerId()) {
111  $this->_address = null;
112  }
113  } catch (NoSuchEntityException $e) {
114  $this->_address = null;
115  }
116  }
117 
118  if ($this->_address === null || !$this->_address->getId()) {
119  $this->_address = $this->addressDataFactory->create();
120  $customer = $this->getCustomer();
121  $this->_address->setPrefix($customer->getPrefix());
122  $this->_address->setFirstname($customer->getFirstname());
123  $this->_address->setMiddlename($customer->getMiddlename());
124  $this->_address->setLastname($customer->getLastname());
125  $this->_address->setSuffix($customer->getSuffix());
126  }
127 
128  $this->pageConfig->getTitle()->set($this->getTitle());
129 
130  if ($postedData = $this->_customerSession->getAddressFormData(true)) {
131  $postedData['region'] = [
132  'region_id' => isset($postedData['region_id']) ? $postedData['region_id'] : null,
133  'region' => $postedData['region'],
134  ];
135  $this->dataObjectHelper->populateWithArray(
136  $this->_address,
137  $postedData,
138  \Magento\Customer\Api\Data\AddressInterface::class
139  );
140  }
141 
142  return $this;
143  }
144 
150  public function getNameBlockHtml()
151  {
152  $nameBlock = $this->getLayout()
153  ->createBlock(\Magento\Customer\Block\Widget\Name::class)
154  ->setObject($this->getAddress());
155 
156  return $nameBlock->toHtml();
157  }
158 
164  public function getTitle()
165  {
166  if ($title = $this->getData('title')) {
167  return $title;
168  }
169  if ($this->getAddress()->getId()) {
170  $title = __('Edit Address');
171  } else {
172  $title = __('Add New Address');
173  }
174  return $title;
175  }
176 
182  public function getBackUrl()
183  {
184  if ($this->getData('back_url')) {
185  return $this->getData('back_url');
186  }
187 
188  if ($this->getCustomerAddressCount()) {
189  return $this->getUrl('customer/address');
190  } else {
191  return $this->getUrl('customer/account/');
192  }
193  }
194 
200  public function getSaveUrl()
201  {
202  return $this->_urlBuilder->getUrl(
203  'customer/address/formPost',
204  ['_secure' => true, 'id' => $this->getAddress()->getId()]
205  );
206  }
207 
213  public function getAddress()
214  {
215  return $this->_address;
216  }
217 
224  public function getStreetLine($lineNumber)
225  {
226  $street = $this->_address->getStreet();
227  return isset($street[$lineNumber - 1]) ? $street[$lineNumber - 1] : '';
228  }
229 
235  public function getCountryId()
236  {
237  if ($countryId = $this->getAddress()->getCountryId()) {
238  return $countryId;
239  }
240  return parent::getCountryId();
241  }
242 
248  public function getRegion()
249  {
250  $region = $this->getAddress()->getRegion();
251  return $region === null ? '' : $region->getRegion();
252  }
253 
259  public function getRegionId()
260  {
261  $region = $this->getAddress()->getRegion();
262  return $region === null ? 0 : $region->getRegionId();
263  }
264 
270  public function getCustomerAddressCount()
271  {
272  return count($this->getCustomer()->getAddresses());
273  }
274 
280  public function canSetAsDefaultBilling()
281  {
282  if (!$this->getAddress()->getId()) {
283  return $this->getCustomerAddressCount();
284  }
285  return !$this->isDefaultBilling();
286  }
287 
293  public function canSetAsDefaultShipping()
294  {
295  if (!$this->getAddress()->getId()) {
296  return $this->getCustomerAddressCount();
297  }
298  return !$this->isDefaultShipping();
299  }
300 
306  public function isDefaultBilling()
307  {
308  return (bool)$this->getAddress()->isDefaultBilling();
309  }
310 
316  public function isDefaultShipping()
317  {
318  return (bool)$this->getAddress()->isDefaultShipping();
319  }
320 
326  public function getCustomer()
327  {
328  return $this->currentCustomer->getCustomer();
329  }
330 
336  public function getBackButtonUrl()
337  {
338  if ($this->getCustomerAddressCount()) {
339  return $this->getUrl('customer/address');
340  } else {
341  return $this->getUrl('customer/account/');
342  }
343  }
344 
351  public function getConfig($path)
352  {
353  return $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
354  }
355 }
$title
Definition: default.phtml:14
$addressRepository
getData($key='', $index=null)
Definition: DataObject.php:119
$customer
Definition: customers.php:11
__()
Definition: __.php:13
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Directory\Helper\Data $directoryHelper, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Framework\App\Cache\Type\Config $configCacheType, \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory, \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository, \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory, \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, array $data=[])
Definition: Edit.php:67