0

I have a content type Topic. The topic has the taxonomy terms -health -fitness and -news. I only want certain roles like administrators to be able to see and use the taxonomy term - news when creating topics. I need all other users to only see and use the other two.

How do I go about this? Would hook form alter be the right direction?

2
  • DO you want to access taxonomy term field only for the admin or specific list of terms in field?
    – Ajay Reddy
    Aug 22 '18 at 18:01
  • Hi, I want only the people of a certain role (admin or another) to be able to create content and select the News check box/radio button taxonomy term field. Aug 22 '18 at 19:29
0

You can do hook_form_FORM_ID_alter() and alter the options values based on role.

use Drupal\Core\Form\FormStateInterface;
/**
 * implements hook_form_alter().
 */
function chad_form_node_topic_form_alter(&$form, FormStateInterface $form_state, $form_id) {

    $current_user = \Drupal::currentUser();
    $roles = $current_user->getRoles();
    if (!in_array('administrator', $roles)) {
      // You can uset values like below.          
      unset($form['topic_types']['widget']['#options'][3]);
    }
}
11
  • Thanks for your help but I am having problems figuring out the code. The code I am using is below where Chad is the custom module and node_topic is the form. News is the taxonomy term of type topic and I am not sure how to refer to the correct TAXONOMY_FIELD Aug 23 '18 at 12:24
  • function chad_form_node_topic_form_alter(&$form, FormStateInterface $form_state, $form_id) { $current_user = \Drupal::currentUser(); $roles = $current_user->getRoles(); if (in_array('administrator', $roles)) { // You can uset values like below. unset($form['news']['widget'][0]['state']['#options']['TID']); } } Aug 23 '18 at 12:24
  • What is the field machine name of taxonomy term you are using in node_topic?
    – Ajay Reddy
    Aug 23 '18 at 12:45
  • topic_types is the main vocabulary with term name : News (term tid 3) really appreciating your help. This is driving me mad Aug 23 '18 at 13:01
  • Updated answer. Hope this heps!
    – Ajay Reddy
    Aug 23 '18 at 13:22
0

If you like to hide the whole field, you probably are looking for the Field Permissions module:

The Field Permissions module allows site administrators to set field-level permissions to edit, view and create fields on any entity.

If you like to hide certain terms only, you could use Views as reference method and filter your terms accordingly:

  1. Create a new view for your taxonomy terms.
  2. Create a Filter criteria "Term ID" to filter out all terms that shouldn't be available to non-privileged users, add a "Entity reference" display to that view and configure its Term name in the format settings.
  3. Implement hook_views_query_alter() in a custom module and remove the filter criteria, if the users have the elevated roles:

mymodule.module:

use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;

/**
 * Implements hook_views_query_alter()
 */
function mymodule_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view->id() != 'my_taxonomy_reference_view') {
    return;
  }

  $current_user = \Drupal::currentUser();
  $roles = $current_user->getRoles();

  if (in_array('USER_ROLE', $roles)) {
    unset($query->where[1]['conditions']['key_of_condition']);
  }
}
  1. Head to your taxonomy term field settings and choose your view as reference type.

You might have to fiddle a little with the conditions, as the above example is just an outline for how you could do it. I actually didn't try it myself. But it should get you going.

3
  • Field permissions deals with the permission and visibility of the whole field. I am looking to show the field but hide one of the taxonomy terms that can be selected. Aug 23 '18 at 7:51
  • I see. Obviously I got your question wrong. Aug 23 '18 at 7:55
  • @GregHalford See my updated answer. It hopefully is of help. Aug 23 '18 at 8:35

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.