Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Image.php
Go to the documentation of this file.
1 <?php
13 
15 
16 class Image extends \Magento\Framework\Data\Form\Element\AbstractElement
17 {
21  protected $_urlBuilder;
22 
30  public function __construct(
31  \Magento\Framework\Data\Form\Element\Factory $factoryElement,
32  \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
33  \Magento\Framework\Escaper $escaper,
34  UrlInterface $urlBuilder,
35  $data = []
36  ) {
37  $this->_urlBuilder = $urlBuilder;
38  parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
39  $this->setType('file');
40  }
41 
47  public function getElementHtml()
48  {
49  $html = '';
50 
51  if ((string)$this->getValue()) {
52  $url = $this->_getUrl();
53 
54  if (!preg_match("/^http\:\/\/|https\:\/\//", $url)) {
55  $url = $this->_urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]) . $url;
56  }
57 
58  $html = '<a href="' .
59  $url .
60  '"' .
61  ' onclick="imagePreview(\'' .
62  $this->getHtmlId() .
63  '_image\'); return false;" ' .
64  $this->_getUiId(
65  'link'
66  ) .
67  '>' .
68  '<img src="' .
69  $url .
70  '" id="' .
71  $this->getHtmlId() .
72  '_image" title="' .
73  $this->getValue() .
74  '"' .
75  ' alt="' .
76  $this->getValue() .
77  '" height="22" width="22" class="small-image-preview v-middle" ' .
78  $this->_getUiId() .
79  ' />' .
80  '</a> ';
81  }
82  $this->setClass('input-file');
83  $html .= parent::getElementHtml();
84  $html .= $this->_getDeleteCheckbox();
85 
86  return $html;
87  }
88 
94  protected function _getDeleteCheckbox()
95  {
96  $html = '';
97  if ($this->getValue()) {
98  $label = (string)new \Magento\Framework\Phrase('Delete Image');
99  $html .= '<span class="delete-image">';
100  $html .= '<input type="checkbox"' .
101  ' name="' .
102  parent::getName() .
103  '[delete]" value="1" class="checkbox"' .
104  ' id="' .
105  $this->getHtmlId() .
106  '_delete"' .
107  ($this->getDisabled() ? ' disabled="disabled"' : '') .
108  '/>';
109  $html .= '<label for="' .
110  $this->getHtmlId() .
111  '_delete"' .
112  ($this->getDisabled() ? ' class="disabled"' : '') .
113  '> ' .
114  $label .
115  '</label>';
116  $html .= $this->_getHiddenInput();
117  $html .= '</span>';
118  }
119 
120  return $html;
121  }
122 
128  protected function _getHiddenInput()
129  {
130  return '<input type="hidden" name="' . parent::getName() . '[value]" value="' . $this->getValue() . '" />';
131  }
132 
138  protected function _getUrl()
139  {
140  return $this->getValue();
141  }
142 
148  public function getName()
149  {
150  return $this->getData('name');
151  }
152 }
getData($key='', $index=null)
Definition: DataObject.php:119
$label
Definition: details.phtml:21
__construct(\Magento\Framework\Data\Form\Element\Factory $factoryElement, \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection, \Magento\Framework\Escaper $escaper, UrlInterface $urlBuilder, $data=[])
Definition: Image.php:30