0

Say I have a text list field attached to a node with the following values:

  • Apples
  • Oranges
  • Grapes
  • Kiwi
  • Strawberries
  • Pears
  • Bananas

But I want to give some options only to some roles, for example:

  • Only Administrators can select Strawberries.
  • Only Anonymous can select Kiwi.
  • Only Role C can select Pears.

Is there a module for this? Or, if I was going to code it myself, is there a hook to start from?

This is for Drupal 8 but if there was a solution only for 7, that would be interesting to look at too.

  • 1
    I can't be absolutely sure, but I think that the best way is restrict values per role using hook_form_alter – Eugene Jan 31 '16 at 18:19
  • Would it be OK that all those roles can "see" (but not "select") all those values? – Pierre.Vriens Jan 31 '16 at 19:02
  • @Pierre.Vriens In this case, yes, I don't mind if roles see the values but can't select them, as long as there is some way to differentiate the values they cannot select so that I don't annoy them by letting them select things they can't. – Patrick Kenny Feb 1 '16 at 16:25
1

This is Drupal 7 solution

Using hook_form_alter, you can unset some of the options based on the user role. For example look at this list of article content type (field_list)

enter image description here

It is the result of kpr() on its form

enter image description here

simply use this code

function THEME_form_alter(&$form, &$form_state, $form_id){
    global $user;    
    if($form_id == "article_node_form"){
        //kpr($form);
        if(in_array('SOME_ROLE', $user->roles)) 
            unset($form['field_list']['und']['#options'][2]);
   }
}

This is the result

enter image description here

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.