3

I have a Customer Attribute with Image Type.

Its working Good all in all areas. I would like to display that attribute in Customer Listing same as Product Image.

How to do this? Any ideas will be appreciated.

Update

public function install(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context
) {
    $setup->startSetup();

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $setup->startSetup();
    $attributesInfo = [
        'photo_id' => [
            'label' => 'Photo ID',
            'type' => 'text',
            'input' => 'select',
            'position' => 1000,
            'visible' => true,
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
            'required' => false,
            'system' => 0,
            'user_defined' => true,
            'position' => 1000,
        ]
    ];
    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();

    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    foreach ($attributesInfo as $attributeCode => $attributeParams) {
        $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
    }
    $magentoUsernameAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'photo_id');
    $magentoUsernameAttribute->addData(
        [
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer','customer_account_edit','adminhtml_checkout','adminhtml_customer_address','customer_address_edit','customer_register_address'],
        ]
    );
    $magentoUsernameAttribute->save();
  • 1
    could you update your question with your setup/InstallData.php file code. – Naveed Asim Aug 6 '18 at 5:48
  • Make sure your attribute is in default attribute set of customer entity, and assigned it to a group (see table eav_attribute_group). And you need to add you attribute in vendor/magento/module-customer/view/adminhtml/ui_component/customer_listing.xml. – obscure Aug 6 '18 at 8:07
  • And your install data should also have: 'is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => true, – obscure Aug 6 '18 at 8:07
  • I noticed that if user_defined = true than the attribute might not be in the attribute set. – obscure Aug 6 '18 at 8:08
  • Can you update your whole code to your question? – Sukumar Gorai Aug 6 '18 at 15:57
5
+25

1.You need to add 'is_used_in_grid' => true, in your InstallData.php for attribute options.

<?php 

namespace Vendor\Module\Setup; 

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{

    /**
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * @var \Magento\Eav\Model\Entity\Attribute\SetFactor
     */
    private $attributeSetFactory;    

    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }    

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
         /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(Customer::ENTITY, 'photo_id', [
            'photo_id' => [
            'label' => 'Photo ID',
            'type' => 'text',
            'input' => 'select',
            'position' => 1000,
            'visible' => true,
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
            'required' => false,
            'system' => 0,
            'user_defined' => true,
            'position' => 1000,
            'is_used_in_grid' => true,
        ]
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'photo_id')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);

        $attribute->save();
    }
}

2.Create file customer_listing.xml in app/code/Vendor/Module/view/adminhtml/ui_component/

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="customer_columns">
        <column name="photo_id" class="Vendor\Module\Ui\Component\Listing\Column\PhotoId">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
                    <item name="label" translate="true" xsi:type="string">Photo Id</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

3.Create file PhotoId.php inVendor\Module\Ui\Component\Listing\Column\

<?php
namespace Vendor\Module\Ui\Component\Listing\Column;
use Magento\Customer\Model\CustomerFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;


class PhotoId extends Column
{
    protected $customerFactory;

    private $_urlBuilder;

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        UrlInterface $urlBuilder,
        array $components = [], array $data = [])
    {
        $this->customerFactory = $customerFactory;
        $this->_urlBuilder = $urlBuilder;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {   
        if (isset($dataSource['data']['items'])) {

            foreach ($dataSource['data']['items'] as & $item) {
                $customer = $this->customerFactory->create()->load($item["entity_id"]);
                $item[$this->getData('name')] = '<img src='.$this->_urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]).'customer'.$customer->getData('photo_id').'/>';

            }
        }
        return $dataSource;
    }
}

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.