0

I'm trying to populate a select list dynamically by pulling its values from a taxonomy term field linked to the current-user account. Just to clarify, below function is a hook defined by a module that came with a custom installation profile. It is basically used to create fields on a specific form. So, my custom module has-

function some_function_attributes() {


      global $user;
      $user_fields = user_load($user->uid);
      $termid = $user_fields->field_user_organization['und']['0']['tid'];
      $taxonomy_term = taxonomy_term_load($termid);
      //dsm($taxonomy_term);
      $options = field_get_items('taxonomy_term', $taxonomy_term, 'field_org_environment','select');
      dsm($options);
      return array(
        'environment' => array(
        'type' => 'select',
        'options' => $options,
        'name' => t('Environment'),
        )
      );
    }

Allowed values in the field (for this user)-

E1|Env1

E2|Env2


Here's the devel screenshot for dsm($options); enter image description here

So, its populating the right field but not in the way it should.


Actual form element looks like this- enter image description here

Currently, its just returning the array and I can't figure out what I missed. Been banging my head to the wall for this- How would I populate Env1 and Env2 in this select-list ?

1 Answer 1

0

You'll have to reformat the options array to be in the form 'return value' => 'display value'. See https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#options

4
  • Thanks.I had a look at your link. It says 'return value' => 'display value' could be used when the return values are specific. But mine aren't, they change depending on the logged-in user. Could you elaborate if I misunderstood ?
    – dharma
    Aug 25, 2017 at 12:37
  • By 'specific value' it means that the array key will be actually what goes to the DB. Otherwise, it is still the array index, i.e. 0, 1, 2.... Given the question, I think you need exactly specific values, otherwise the same array keys will have different values for different users and you will put wrong information in the DB.
    – kufeiko
    Aug 25, 2017 at 13:18
  • Makes sense, so I need to change the format in $options = field_get_items('taxonomy_term', $taxonomy_term, 'field_org_environment','select'); Here in the params- I have $taxonomy_term which is the term name linked to the user and 'field_org_environment' is the vocab field (widget: select-list) I'm trying to populate as a select-list. Excuse my noobish behavior, How do you recommend I do it to be in the form 'return value' => 'display value' ?
    – dharma
    Aug 25, 2017 at 13:36
  • I see you have the values E1, E2... but where do you get the descriptive label from, i.e. Env1, Env2 ? Also, try dpm() in place of dsm(), you may like the representation better :)
    – kufeiko
    Aug 29, 2017 at 7:52

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.