自定义图像未在Magento2中显示

时间:2017-03-24 07:22:48

标签: magento magento2

我在phtml文件中添加了一个自定义图像并运行命令php bin / magento setup:static-content:deploy但它仍未显示。此命令也需要很长时间才能完成。

1 个答案:

答案 0 :(得分:-1)

如果有人想使用ui_component添加图像列,请按照说明进行操作:

在uiComponent xml中添加图像列:

 <column name="imagefile" class="{Namespace}\{ModuleName}\Ui\Component\Listing  \Column\Thumbnail">
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/thumbnail</item>
            </item>
             <item name="config" xsi:type="array">
                <item name="sortable" xsi:type="boolean">true</item>
                <item name="altField" xsi:type="string">imagefile</item>
                <item name="has_preview" xsi:type="string">1</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="sorting" xsi:type="string">asc</item>
                <item name="align" xsi:type="string">left</item>
                <item name="laem>bel" xsi:type="string" translate="true">Thumbnail</item>

            </item>
        </argument>
    </column>

在给定路径创建列文件,即

   <?php
   namespace {NameSpace}\{ModuleName}\Ui\Component\Listing\Column;
   use Magento\Framework\View\Element\UiComponentFactory;
   use Magento\Framework\View\Element\UiComponent\ContextInterface;
   use {NameSpace}\{ModuleName}\Model\News\Image as KtplImage;
  class Thumbnail extends \Magento\Ui\Component\Listing\Columns\Column
  {
 const NAME = 'imagefile';
 const ALT_FIELD = 'imagefile';
    public function __construct(
    ContextInterface $context,
    UiComponentFactory $uiComponentFactory,
    KtplImage $imageFactory,
    \Magento\Framework\UrlInterface $urlBuilder,
    array $components = [],
    array $data = []
) {
    parent::__construct($context, $uiComponentFactory, $components, $data);
    $this->imageFactory = $imageFactory;
    $this->urlBuilder = $urlBuilder;
}

/**
 * Prepare Data Source
 *
 * @param array $dataSource
 * @return void
 */
public function prepareDataSource(array & $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        $fieldName = $this->getData('name');
        foreach ($dataSource['data']['items'] as & $item) {
            $itemIndex = new \Magento\Framework\Object($item);
            $item[$fieldName . '_src'] =   $this->imageFactory->getBaseUrl().$itemIndex->getImagefile();
            $item[$fieldName . '_orig_src'] =  $this->imageFactory->getBaseUrl().$itemIndex->getImagefile();                
        }
     }
  }

}
 use Magento\Framework\App\Filesystem\DirectoryList;
 use Magento\Backend\App\Action;
 protected $_fileUploaderFactory;
 public function __construct(
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
Action\Context $context     
 ) {
  $this->_fileUploaderFactory = $fileUploaderFactory;
  parent::__construct($context);
}
 public function execute(){
 $uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);
 $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
 $uploader->setAllowRenameFiles(false);
 $uploader->setFilesDispersion(false);
 $path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)
 ->getAbsolutePath('images/');
 $uploader->save($path);
 }