Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DefaultRenderer.php
Go to the documentation of this file.
1 <?php
7 
12 
17 {
23  protected $_type;
24 
28  protected $_elementFactory;
29 
33  protected $_countryFactory;
34 
39 
43  protected $addressMapper;
44 
55  public function __construct(
56  \Magento\Framework\View\Element\Context $context,
57  ElementFactory $elementFactory,
58  \Magento\Directory\Model\CountryFactory $countryFactory,
59  \Magento\Customer\Api\AddressMetadataInterface $metadataService,
61  array $data = []
62  ) {
63  $this->_elementFactory = $elementFactory;
64  $this->_countryFactory = $countryFactory;
65  $this->_addressMetadataService = $metadataService;
66  $this->addressMapper = $addressMapper;
67  parent::__construct($context, $data);
68  $this->_isScopePrivate = true;
69  }
70 
76  public function getType()
77  {
78  return $this->_type;
79  }
80 
87  public function setType(\Magento\Framework\DataObject $type)
88  {
89  $this->_type = $type;
90  return $this;
91  }
92 
98  public function getFormat(AddressModelInterface $address = null)
99  {
100  $countryFormat = $address === null
101  ? false : $address->getCountryModel()->getFormat(
102  $this->getType()->getCode()
103  );
104  $format = $countryFormat ? $countryFormat->getFormat() : $this->getType()->getDefaultFormat();
105  return $format;
106  }
107 
114  public function render(AddressModelInterface $address, $format = null)
115  {
116  $address = $address->getDataModel(0, 0);
117  return $this->renderArray($this->addressMapper->toFlatArray($address), $format);
118  }
119 
123  public function getFormatArray($addressAttributes = null)
124  {
125  $countryFormat = false;
126  if ($addressAttributes && isset($addressAttributes['country_id'])) {
128  $country = $this->_countryFactory->create()->load($addressAttributes['country_id']);
129  $countryFormat = $country->getFormat($this->getType()->getCode());
130  }
131  $format = $countryFormat ? $countryFormat->getFormat() : $this->getType()->getDefaultFormat();
132  return $format;
133  }
134 
141  public function renderArray($addressAttributes, $format = null)
142  {
143  switch ($this->getType()->getCode()) {
144  case 'html':
146  break;
147  case 'pdf':
148  $dataFormat = ElementFactory::OUTPUT_FORMAT_PDF;
149  break;
150  case 'oneline':
152  break;
153  default:
155  break;
156  }
157 
158  $attributesMetadata = $this->_addressMetadataService->getAllAttributesMetadata();
159  $data = [];
160  foreach ($attributesMetadata as $attributeMetadata) {
161  if (!$attributeMetadata->isVisible()) {
162  continue;
163  }
164  $attributeCode = $attributeMetadata->getAttributeCode();
165  if ($attributeCode == 'country_id' && isset($addressAttributes['country_id'])) {
166  $data['country'] = $this->_countryFactory->create()->loadByCode(
167  $addressAttributes['country_id']
168  )->getName();
169  } elseif ($attributeCode == 'region' && isset($addressAttributes['region'])) {
170  $data['region'] = __($addressAttributes['region']);
173  $dataModel = $this->_elementFactory->create($attributeMetadata, $value, 'customer_address');
174  $value = $dataModel->outputValue($dataFormat);
175  if ($attributeMetadata->getFrontendInput() == 'multiline') {
176  $values = $dataModel->outputValue(ElementFactory::OUTPUT_FORMAT_ARRAY);
177  // explode lines
178  foreach ($values as $k => $v) {
179  $key = sprintf('%s%d', $attributeCode, $k + 1);
180  $data[$key] = $v;
181  }
182  }
184  }
185  }
186  if ($this->getType()->getEscapeHtml()) {
187  foreach ($data as $key => $value) {
188  $data[$key] = $this->escapeHtml($value);
189  }
190  }
192  return $this->filterManager->template($format, ['variables' => $data]);
193  }
194 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
return false
Definition: gallery.phtml:36
$addressAttributes
Definition: register.phtml:117
$values
Definition: options.phtml:88
__()
Definition: __.php:13
$address
Definition: customer.php:38
$type
Definition: item.phtml:13
getFormat(AddressModelInterface $address=null)
render(AddressModelInterface $address, $format=null)
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
$attributeCode
Definition: extend.phtml:12
__construct(\Magento\Framework\View\Element\Context $context, ElementFactory $elementFactory, \Magento\Directory\Model\CountryFactory $countryFactory, \Magento\Customer\Api\AddressMetadataInterface $metadataService, Mapper $addressMapper, array $data=[])