Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
File.php
Go to the documentation of this file.
1 <?php
7 
13 class File extends \Magento\Framework\Data\Form\Element\AbstractElement
14 {
18  protected $_assetRepo;
19 
25  protected $_adminhtmlData = null;
26 
30  protected $urlEncoder;
31 
41  public function __construct(
42  \Magento\Framework\Data\Form\Element\Factory $factoryElement,
43  \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
44  \Magento\Framework\Escaper $escaper,
45  \Magento\Backend\Helper\Data $adminhtmlData,
46  \Magento\Framework\View\Asset\Repository $assetRepo,
47  \Magento\Framework\Url\EncoderInterface $urlEncoder,
48  $data = []
49  ) {
50  $this->_adminhtmlData = $adminhtmlData;
51  $this->_assetRepo = $assetRepo;
52  $this->urlEncoder = $urlEncoder;
53  parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
54  $this->setType('file');
55  }
56 
62  public function getElementHtml()
63  {
64  $this->addClass('input-file');
65  if ($this->getRequired()) {
66  $this->removeClass('required-entry');
67  $this->addClass('required-file');
68  }
69 
70  $element = sprintf(
71  '<input id="%s" name="%s" %s />%s%s',
72  $this->getHtmlId(),
73  $this->getName(),
74  $this->serialize($this->getHtmlAttributes()),
75  $this->getAfterElementHtml(),
76  $this->_getHiddenInput()
77  );
78 
79  return $this->_getPreviewHtml() . $element . $this->_getDeleteCheckboxHtml();
80  }
81 
87  protected function _getDeleteCheckboxHtml()
88  {
89  $html = '';
90  if ($this->getValue() && !$this->getRequired() && !is_array($this->getValue())) {
91  $checkboxId = sprintf('%s_delete', $this->getHtmlId());
92  $checkbox = [
93  'type' => 'checkbox',
94  'name' => sprintf('%s[delete]', $this->getName()),
95  'value' => '1',
96  'class' => 'checkbox',
97  'id' => $checkboxId
98  ];
99  $label = ['for' => $checkboxId];
100  if ($this->getDisabled()) {
101  $checkbox['disabled'] = 'disabled';
102  $label['class'] = 'disabled';
103  }
104 
105  $html .= '<span class="' . $this->_getDeleteCheckboxSpanClass() . '">';
106  $html .= $this->_drawElementHtml('input', $checkbox) . ' ';
107  $html .= $this->_drawElementHtml('label', $label, false) . $this->_getDeleteCheckboxLabel() . '</label>';
108  $html .= '</span>';
109  }
110  return $html;
111  }
112 
118  protected function _getDeleteCheckboxSpanClass()
119  {
120  return 'delete-file';
121  }
122 
128  protected function _getDeleteCheckboxLabel()
129  {
130  return __('Delete File');
131  }
132 
138  protected function _getPreviewHtml()
139  {
140  $html = '';
141  if ($this->getValue() && !is_array($this->getValue())) {
142  $image = [
143  'alt' => __('Download'),
144  'title' => __('Download'),
145  'src' => $this->_assetRepo->getUrl('images/fam_bullet_disk.gif'),
146  'class' => 'v-middle'
147  ];
148  $url = $this->_getPreviewUrl();
149  $html .= '<span>';
150  $html .= '<a href="' . $url . '">' . $this->_drawElementHtml('img', $image) . '</a> ';
151  $html .= '<a href="' . $url . '">' . __('Download') . '</a>';
152  $html .= '</span>';
153  }
154  return $html;
155  }
156 
162  protected function _getHiddenInput()
163  {
164  return $this->_drawElementHtml(
165  'input',
166  [
167  'type' => 'hidden',
168  'name' => sprintf('%s[value]', $this->getName()),
169  'id' => sprintf('%s_value', $this->getHtmlId()),
170  'value' => $this->getEscapedValue()
171  ]
172  );
173  }
174 
180  protected function _getPreviewUrl()
181  {
182  return $this->_adminhtmlData->getUrl(
183  'customer/index/viewfile',
184  ['file' => $this->urlEncoder->encode($this->getValue())]
185  );
186  }
187 
196  protected function _drawElementHtml($element, array $attributes, $closed = true)
197  {
198  $parts = [];
199  foreach ($attributes as $k => $v) {
200  $parts[] = sprintf('%s="%s"', $k, $v);
201  }
202 
203  return sprintf('<%s %s%s>', $element, implode(' ', $parts), $closed ? ' /' : '');
204  }
205 
212  public function getEscapedValue($index = null)
213  {
214  if (is_array($this->getValue())) {
215  return false;
216  }
217  $value = $this->getValue();
218  if (is_array($value) && $index === null) {
219  $index = 'value';
220  }
221 
222  return parent::getEscapedValue($index);
223  }
224 }
__()
Definition: __.php:13
__construct(\Magento\Framework\Data\Form\Element\Factory $factoryElement, \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection, \Magento\Framework\Escaper $escaper, \Magento\Backend\Helper\Data $adminhtmlData, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\Url\EncoderInterface $urlEncoder, $data=[])
Definition: File.php:41
_drawElementHtml($element, array $attributes, $closed=true)
Definition: File.php:196
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
serialize($attributes=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
$attributes
Definition: matrix.phtml:13
$index
Definition: list.phtml:44
$element
Definition: element.phtml:12