Domanda

list all user name and id in dropdown in magento 2 admin panel on product edit page.

Here's my code. Please find where i mistake.

<?
/**
* 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');
        if($roleModel->getId()) {
        $userIds = $roleModel->getRoleUsers();
        var_dump($userIds);
        foreach($userIds as $userId) {
        $user = $userModel->load($userId);    




        $this->_options = [


        ['label' => __($user->getUsername()), 'value' => $user->getUsername()],

        ];
    return $this->_options;
    } 



}
È stato utile?

Soluzione

please replace below code with in your block of code :

foreach($userIds as $userId) {
    $user = $userModel->load($userId);      
    $this->_options[]= array("label"=>$user->getUsername(),"value"=>$user->getUsername());

}
return  $this->_options;

Altri suggerimenti

Piggy backing off Rutvee's correct answer. In response to what they said about creating a new question, that is not necassary.

please replace below code with in your block of code :

foreach($userIds as $userId) { $user = $userModel->load($userId);
$this->_options[]= array("label"=>$user->getUsername(),"value"=>$user->getUsername());

} return $this->_options;

change: "label"=>$user->getUsername()

to

"label"=>$user->getUsername() . ' ID: ' . $user->getID()

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top