1
'example_condition_user' => array(
      'label' => t('User Condition'),
      'arguments' => array(
      ),
      'parameter' => array(
        'username' => array(
          'type'  => 'text',
          'options list' => 'list_all_users', 
          'label' => t('Username'),
          'description' => t('user that the conditions apply'),
         ),
        'user_entry' => array(
          'type'  => 'user',
          'label' => t('user'),
        )
      ),
      'group' => t('exemplo'),
      'module' => 'exemplo',
)


function list_all_users(){

  $query = db_select('users', 'u');
  $query->fields('u', array('name'));

  $result = $query->execute()->fetchAll();
  $ret = array();


  foreach($result as $r){
    array_push($ret, $r->name);
  }


  return array_values($ret);

}

The list_all_users function returns the users (that are in my database) in a array, but the options list does not get the values ​​of the array, but the array index, and this is a problem, because I need the values ​​of the index, and then do the validations rule_condition_info (), does anyone have any idea how to fix this?

0

You just need a reliable array key so you can load the data up again somewhere else; since you're going into the users table, uid seems appropriate:

function list_all_users() {
  return db_query('SELECT uid, name FROM {users}')->fetchAllKeyed();
}

That will turn the options into an array with user IDs as keys and names as values. As far as the front end is concerned, nothing changes.

Now when you receive the selected option value in the validation function, you can just load the user up and get the name from the entity:

$account = user_load($the_value);
$name = $account->name;

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.