0

List all users in product edit page admin panel. Any tutorial or guide please share.

I just created a tab in product edit page but need to show all users details in that tab.

Here's my code:

<?
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Learning\ClothingMaterial\Model\Attribute\Source;

class Material extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    /**
     * Get all options
     * @return array
     */
    public function getAllOptions()
    {

        $roleName = "merchant";
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$roleModel = $objectManager->create('Magento\Authorization\Model\Role');
$userModel = $objectManager->create('Magento\User\Model\User');
$roleModel = $roleModel->load($roleName, 'role_name');
$option_val = [];
        if($roleModel->getId()) {
            $userIds = $roleModel->getRoleUsers();
            foreach($userIds as $userId) {
                $user = $userModel->load($userId);
                $usernames = $user->getUsername();
                $userid = $user->getId();
                $option_val['label'] =  __($userid - $usernames);
                $option_val['value'] =  __($userid.$usernames);
            }
        }
        if (!$this->_options) {
            $this->_options = [$option_val];
        }
        return $this->_options;
    }
}
4
  • Which user custmer or admin sub user ? Commented Sep 27, 2018 at 5:49
  • all users that created by admin Commented Sep 27, 2018 at 5:51
  • which magento version are you using ? do you want to show log in that tab, like which admin user have updated this product ? Commented Sep 27, 2018 at 6:00
  • version 2 i just wanted to show all users in dropdown in product edit page so i can assign product to user. Commented Sep 27, 2018 at 6:01

1 Answer 1

0

You need to add this to your class and constructor:

protected $userFactory;
public function __construct(
    ...
    \Magento\User\Model\UserFactory $userFactory,
    ...
)
{
    ...
    $this->userFactory = $userFactory;
    ...
}

Then create a method in the same class to retrieve the role data:

public function getUserData()
{
    $user = $this->userFactory->create();
    echo '<pre>';print_r($user->getData());die;
}

You have to get user collection here and use it for dropdown.

1
  • can you please review my code i updated Commented Sep 27, 2018 at 7:45

Your Answer

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

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