Image.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Category form input image element
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Data\Form\Element;
  12. use Magento\Framework\UrlInterface;
  13. class Image extends \Magento\Framework\Data\Form\Element\AbstractElement
  14. {
  15. /**
  16. * @var UrlInterface
  17. */
  18. protected $_urlBuilder;
  19. /**
  20. * @param \Magento\Framework\Data\Form\Element\Factory $factoryElement
  21. * @param \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection
  22. * @param \Magento\Framework\Escaper $escaper
  23. * @param UrlInterface $urlBuilder
  24. * @param array $data
  25. */
  26. public function __construct(
  27. \Magento\Framework\Data\Form\Element\Factory $factoryElement,
  28. \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
  29. \Magento\Framework\Escaper $escaper,
  30. UrlInterface $urlBuilder,
  31. $data = []
  32. ) {
  33. $this->_urlBuilder = $urlBuilder;
  34. parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
  35. $this->setType('file');
  36. }
  37. /**
  38. * Return element html code
  39. *
  40. * @return string
  41. */
  42. public function getElementHtml()
  43. {
  44. $html = '';
  45. if ((string)$this->getValue()) {
  46. $url = $this->_getUrl();
  47. if (!preg_match("/^http\:\/\/|https\:\/\//", $url)) {
  48. $url = $this->_urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]) . $url;
  49. }
  50. $html = '<a href="' .
  51. $url .
  52. '"' .
  53. ' onclick="imagePreview(\'' .
  54. $this->getHtmlId() .
  55. '_image\'); return false;" ' .
  56. $this->_getUiId(
  57. 'link'
  58. ) .
  59. '>' .
  60. '<img src="' .
  61. $url .
  62. '" id="' .
  63. $this->getHtmlId() .
  64. '_image" title="' .
  65. $this->getValue() .
  66. '"' .
  67. ' alt="' .
  68. $this->getValue() .
  69. '" height="22" width="22" class="small-image-preview v-middle" ' .
  70. $this->_getUiId() .
  71. ' />' .
  72. '</a> ';
  73. }
  74. $this->setClass('input-file');
  75. $html .= parent::getElementHtml();
  76. $html .= $this->_getDeleteCheckbox();
  77. return $html;
  78. }
  79. /**
  80. * Return html code of delete checkbox element
  81. *
  82. * @return string
  83. */
  84. protected function _getDeleteCheckbox()
  85. {
  86. $html = '';
  87. if ($this->getValue()) {
  88. $label = (string)new \Magento\Framework\Phrase('Delete Image');
  89. $html .= '<span class="delete-image">';
  90. $html .= '<input type="checkbox"' .
  91. ' name="' .
  92. parent::getName() .
  93. '[delete]" value="1" class="checkbox"' .
  94. ' id="' .
  95. $this->getHtmlId() .
  96. '_delete"' .
  97. ($this->getDisabled() ? ' disabled="disabled"' : '') .
  98. '/>';
  99. $html .= '<label for="' .
  100. $this->getHtmlId() .
  101. '_delete"' .
  102. ($this->getDisabled() ? ' class="disabled"' : '') .
  103. '> ' .
  104. $label .
  105. '</label>';
  106. $html .= $this->_getHiddenInput();
  107. $html .= '</span>';
  108. }
  109. return $html;
  110. }
  111. /**
  112. * Return html code of hidden element
  113. *
  114. * @return string
  115. */
  116. protected function _getHiddenInput()
  117. {
  118. return '<input type="hidden" name="' . parent::getName() . '[value]" value="' . $this->getValue() . '" />';
  119. }
  120. /**
  121. * Get image preview url
  122. *
  123. * @return string
  124. */
  125. protected function _getUrl()
  126. {
  127. return $this->getValue();
  128. }
  129. /**
  130. * Return name
  131. *
  132. * @return string
  133. */
  134. public function getName()
  135. {
  136. return $this->getData('name');
  137. }
  138. }