2

I have created a view with taxonomy term as filter criteria.

I want to display values of taxonomy terms based on the user logged in(i.e. I want to display only the terms which are created by that user alone) in that select list.

Any help will be really appreciated.

  • what you want to show in that view? – subhojit777 Oct 15 '13 at 6:01
  • That taxonomy term is a exposed Filter and i want the results of that field to be showed according to the user created terms(current user logged-in) alone in that list,instead of the all the taxonomy terms of that vocabulary. – Ram Oct 15 '13 at 6:14
2

There is no way you can get the author uid of taxonomy term. But here is a solution you can try:

  • Add a user reference field to the vocabulary, the field will contain author uid.
  • Add term exposed filter to the view.
  • Using hook_form_alter() alter the exposed filter so that it shows only those taxonomy terms which are created by the currently logged in user.
  • "no way you can get the author uid of taxonomy term" - explanation: taxonomy terms does not store UIDs in the database. So it's not a problem "you won't get it", but way simpler "no such data". – Mołot Oct 15 '13 at 7:33
0

As mentioned by subhojit777, its not possible to get the author uid of the taxonomy term. But you can customize that options by getting all the nodes of that particular user and all the terms used in that particular node.

There are more than one proper way to do it. But the following way is simple and will not have more db call.

So you can try the following in your hook_form_alter,

$options = array();
// Get term defined by current user
$nids_result = db_select('node', 'n')
    ->fields('n', array('nid'))
    ->condition('n.uid', $uid, '=')
    ->execute()
    ->fetchAll();

foreach ($nids_result as $nid_obj) {

    $terms_result = db_select('taxonomy_index', 'ti')
        ->fields('ti')
        ->condition('ti.nid', $nid_obj->nid, '=')
        ->execute()
        ->fetchAll();

    foreach ($terms_result as $term_obj) {
            // Check if the term value is not from any other vocabulary that node is using
        if (isset($form['taxonomy_category_tid']['#options'][$term_obj->tid])) {

            $options[$term_obj->tid] = $form['taxonomy_category_tid']['#options'][$term_obj->tid];
        }
    }
}           
$options = array_unique($options);          

$form['taxonomy_category_tid']['#options'] = $options;

*taxonomy_category_tid* is the taxonomy field id as in form element.

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.