1

I added a custom category attribute "Listing Image" and want to display the checkbox "Set Default Value", like "Thumbnail" and "Category Image". I already compared the ui_component, but couldn't find the entry for that.

enter image description here

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="content">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Content</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">10</item>
            </item>
        </argument>
        <field name="listingimage">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="source" xsi:type="string">category</item>
                    <item name="label" xsi:type="string" translate="true">Listing Image</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="formElement" xsi:type="string">fileUploader</item>
                    <item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
                    <item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item>
                    <item name="sortOrder" xsi:type="number">45</item>
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="uploaderConfig" xsi:type="array">
                        <item name="url" xsi:type="url" path="categorylandingpage/category/upload/attribute_code/listingimage"/>
                    </item>
                    <item name="scopeLabel" xsi:type="string">[WEBSITE]</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>
0

I already did that as listing grid product, see the image above.

enter image description here

Using:

  \Magento\Framework\View\Asset\Repository $assetRepo,

  $this->assetRepo->getUrl('Magento_Catalog::images/product/placeholder/thumbnail.jpg');

1- file Thumbnail.php

  <?php

  namespace PHPAISS\Jobs\Ui\Component\Listing\Column\Jobs;

  use Magento\Framework\DataObject;
  use Magento\Framework\UrlInterface;
  use Magento\Framework\View\Asset\Repository;
  use Magento\Framework\View\Element\UiComponent\ContextInterface;
  use Magento\Framework\View\Element\UiComponentFactory;
  use Magento\Ui\Component\Listing\Columns\Column;
  use PHPAISS\Jobs\Helper\Data;

class Thumbnail extends Column
{
     /**
 * Column name in job listing grid
 */
const NAME = 'thumbnail';

/**
 * Alt image
 */
const ALT_FIELD = 'title';

/**
 * @var UrlInterface
 */
private $urlBuilder;
/**
 * @var Data
 */
private $helper;
/**
 * @var Repository
 */
private $assetRepo;

/**
 * Thumbnail constructor.
 *
 * @param ContextInterface                         $context
 * @param UiComponentFactory                       $uiComponentFactory
 * @param UrlInterface                             $urlBuilder
 * @param Repository $assetRepo
 * @param Data                                     $helper
 * @param array                                    $components
 * @param array                                    $data
 */
public function __construct(
    ContextInterface $context,
    UiComponentFactory $uiComponentFactory,
    UrlInterface $urlBuilder,
    \Magento\Framework\View\Asset\Repository $assetRepo,
    Data $helper,
    array $components = [],
    array $data = []
) {
    parent::__construct($context, $uiComponentFactory, $components, $data);
    $this->urlBuilder = $urlBuilder;
    $this->helper = $helper;
    $this->assetRepo = $assetRepo;
}

/**
 * Prepare Data Source
 *
 * @param array $dataSource
 * @return array
 */
public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        $fieldName = self::NAME;

        foreach ($dataSource['data']['items'] as & $item) {
            /** @var \PHPAISS\Jobs\Model\Job $job */
            $job = new DataObject($item);

            // Set default thumbnail as thumbnail in product listing grid
            $urlImage = $job->getImage() ?
                        $this->helper->getUrlJobImage($job->getImage()) :
                        $this->assetRepo->getUrl('Magento_Catalog::images/product/placeholder/thumbnail.jpg');

            $item[$fieldName . '_src'] = $urlImage;
            $item[$fieldName . '_alt'] = $this->getAlt($item) ?: '';
            $item[$fieldName . '_link'] = $this->urlBuilder->getUrl(
                'jobs/job/edit',
                ['id' => $job->getEntityId(), 'store' => $this->context->getRequestParam('store')]
            );
            $item[$fieldName . '_orig_src'] = $urlImage;
        }
    }

    return parent::prepareDataSource($dataSource);
}

/**
 * @param array $row
 *
 * @return null|string
 */
protected function getAlt($row)
{
    $altField = $this->getData('config/altField') ?: self::ALT_FIELD;
    return isset($row[$altField]) ? $row[$altField] : null;
}
}

2 - file jobs_job_listing.xml

     <!--
     ** Thumbnail Column
     ** The name 'thumbnail' defined in const NAME = 'thumbnail' in file:
     ** \PHPAISS\Jobs\Ui\Component\Listing\Column\Jobs\Thumbnail
     -->
    <column name="thumbnail" class="PHPAISS\Jobs\Ui\Component\Listing\Column\Jobs\Thumbnail" component="Magento_Ui/js/grid/columns/thumbnail">
        <settings>
            <altField>title</altField>
            <hasPreview>1</hasPreview>
            <label translate="true">Thumbnail</label>
            <sortable>false</sortable>
        </settings>
    </column>

You can check the module in my github: Jobs Module

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.