0

enter image description hereI have an issue with display image from admin UI component in magento 2.3.1.Who is solution help me ! Here is my source:

<?php
    namespace Booking\Hotel\Ui\Component\Listing\Column;
    use Magento\Framework\View\Element\UiComponentFactory;
    use Magento\Framework\View\Element\UiComponent\ContextInterface;
    use Magento\Store\Model\StoreManagerInterface;
    use Magento\Ui\Component\Listing\Columns\Column;
    class Logo extends Column
    {
        const ALT_FIELD = 'title';
        /**
         * @var \Magento\Store\Model\StoreManagerInterface
         */
        protected $storeManager;
        /**
         * @var \Magento\Catalog\Helper\Image
         */
        protected $imageHelper;
        /**
         * @param ContextInterface              $context
         * @param UiComponentFactory            $uiComponentFactory
         * @param StoreManagerInterface         $storeManager
         * @param \Magento\Catalog\Helper\Image $imageHelper
         * @param array                         $components
         * @param array                         $data
         */
        public function __construct(
            ContextInterface $context,
            UiComponentFactory $uiComponentFactory,
            StoreManagerInterface $storeManager,
            \Magento\Catalog\Helper\Image $imageHelper,
            array $components = [],
            array $data = []
        ) {
            $this->storeManager = $storeManager;
            $this->imageHelper = $imageHelper;
            parent::__construct($context, $uiComponentFactory, $components, $data);
        }
        /**
         * Prepare Data Source
         *
         * @param array $dataSource
         * @return array
         */
        public function prepareDataSource(array $dataSource)
        {
            if (isset($dataSource['data']['items'])) {
                $fieldName = $this->getData('name');
                foreach ($dataSource['data']['items'] as &$item) {
                    $url = '';
                    if (isset($item[$fieldName])) {
                        if ($item[$fieldName] != '') {
                            $url = $this->storeManager->getStore()->getBaseUrl(
                                    \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
                                ) . "catalog/tmp/category" . $item[$fieldName];
                        } else {
                            $url = $this->imageHelper->getDefaultPlaceholderUrl('small_image');
                        }
                    } else {
                        $url = $this->imageHelper->getDefaultPlaceholderUrl('small_image');
                    }
                    $item[$fieldName . '_src'] = $url;
                    $item[$fieldName . '_alt'] = $this->getAlt($item) ?: '';
                    $item[$fieldName . '_orig_src'] = $url;
                }
            }
            return $dataSource;
        }
        protected function getAlt($row)
        {
            $altField = $this->getData('config/altField') ?: self::ALT_FIELD;
            return isset($row[$altField]) ? $row[$altField] : null;
        }
    }

This is the image column I added in the AAA file

<column name="hotel_image" class="Booking\Hotel\Ui\Component\Listing\Column\Logo">
<argument name="data" xsi:type="array">
    <item name="config" xsi:type="array">
        <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/thumbnail</item>
        <item name="label" xsi:type="string" translate="true">Image</item>
        <item name="altField" xsi:type="string">title</item>
        <item name="has_preview" xsi:type="string">1</item>
        <item name="sortOrder" xsi:type="number">30</item>
    </item>
</argument>
</column>

But it not show enter image description here

6

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.