2

I have a vocabulary with a user reference field. In a content type I have a field that references one or more terms from this vocabulary with a select widget. I would like to restrict the options to only the terms that are referencing the logged in user.

Drupal 7, Entityreference module is used for the user reference in the term and the select widget that I want to customize in the node is a Term reference.

3

EDIT: If you use Features and the field under that references the taxonomy term under question is in one of your features, there's is much easier way to do this. Add this to your my_module.field_base.inc:


// Exported field_base: 'field_user_blogs_ref'
$field_bases['field_user_blogs_ref'] = array(
    ...
    'settings' => array(
      ...
      'options_list_callback' => 'my_callback',
    ),

Neat. This has the additional benefit that it applies only to this field base as there might be more fields referencing this vocabulary. The same could probably be done with the code below as well but extra effort.

ORIGINAL ANSWER: still leaving this as this method might be useful for some cases.

I finally did this by altering the code that provides the options to the select widget. You can see how this is done in the content_taxonomy module

1) Provide a hook_field_info_alter to set the callback function


function content_taxonomy_field_info_alter(&$info) {
    $info['taxonomy_term_reference']['settings']['options_list_callback'] = 'content_taxonomy_allowed_values';
}

2) Provide the callback that returns the options

function content_taxonomy_allowed_values($field) {
  $options = array();
  foreach ($field['settings']['allowed_values'] as $tree) {
    if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
      if ($tree['vocabulary'] == 'user_blogs') {
        return my_callback($vocabulary);
      }
      $max_depth = (isset($tree['depth']) && !empty($tree['depth'])) ? $tree['depth'] : NULL;
      if ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'], $max_depth)) {
        foreach ($terms as $term) {
          $options[$term->tid] = str_repeat('- ', $term->depth) . $term->name;
        }
      }
    }
  }
  return $options;
}

Notice that the function provides the options for all taxonomy vocabularies, but if the vocabulary is of a certain type then it calls another function that returns the options the way required. If you don't use the content_taxonomy module you can copy and use the taxonomy_allowed_values function from the taxonomy module.

|improve this answer|||||
1

Try Entity Reference View Widget module

This module provides an advanced Entity Reference widget that uses a view for selecting items. The view can be paginated and have exposed filters. It degrades, so it can be used even if Javascript is disabled.

Video Tutorial

|improve this answer|||||
  • Hmm, I didn't think about that, I'll check it, it might do the job fine. Thanks. – krystalcode May 16 '13 at 6:18
  • I had a look, this won't work because the reference in the node is a term reference and not an entity reference, so I am not provided with the view widget as an option. I have updated the question to make this more clear. – krystalcode May 17 '13 at 1:59
  • I just realised I can use entityreference to reference taxonomy terms, so I'll change the field type from term reference to entity reference and it might work. – krystalcode May 17 '13 at 2:03
  • FYI, the entityreference view widget was not adequate for the needs I had so I finally used the solution below. – krystalcode May 17 '13 at 14:54

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.