This is my 2nd question about my problem, the last was "Users have access to add Commerce Products into the Store and publish it. How to display to user only allowed firms in the product addition form?" I am writing my 6th module and I do not know nothing about form fields arrays. I need to display in node addition form only user referenced terms in field Firms. Taxonomy "Firms" has field_seller of type Entity Reference. I add to all Firms terms users that must have access to them. How can I display to user only those terms that have reference to this user when he add node Product ? I've made this code that has only correct name (I've checked this with other funcions that was in it and worked):

/* Displays only terms that have Entity Reference field with current user reference */
function user_referenced_taxonomy_terms_form_node_form_alter(&$form, &$form_state, $form_id) {

how to get array $form['field_firm'] of terms that have reference to current user if the user role is not admin?

1 Answer 1


I solved this task with my module: This code limit radiobuttons of field_firm on the node addition page with only those terms that have entity reference to current user. Admin have access to all. If user has no one term access field Firms hides.

/* BASE_FORM_ID : node_form */
/* Displays only terms that have Entity Reference field with current user reference */
//$form['field_firm']['und'][1551]= array('#access' => FALSE); //hides taxonomy term 1551

function user_referenced_taxonomy_terms_form_node_form_alter(&$form, &$form_state, $form_id) {

  global $user;
  //if current user has roles administrator or editor return
  foreach ($user->roles as $role) {
      if (($role == 'administrator') || ($role == 'editor')) return;
  if (isset($form['field_firm'])) {
    $query = new EntityFieldQuery();
    $term_entities = $query
      ->entityCondition('bundle', 'firms')
      ->entityCondition('entity_type', 'taxonomy_term', '=')
      ->fieldCondition('field_seller','target_id',$user->uid) // if user has access
    if ($term_entities) {
        //forming array of firms using $term_entities array
         $items = array_keys($term_entities['taxonomy_term']);
         foreach ($form['field_firm']['und']['#options'] as $key=>$value) {
             if ($key == '_none') continue;
             for ($i=0;$i<count($items);$i++) {
                 if ($key == $items[$i])  $delete = FALSE;
             if ($delete) $form['field_firm']['und'][$key]= array('#access' => FALSE);
    }else {
        //hide field firm if user has no access to any term
        $form['field_firm']['#access'] = FALSE;

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.