Search code examples
drupaldrupal-7

How can i hide list entries based on user roles Drupal 7


I'm quite new to Drupal, i setup node pages and assigned edit permission to various roles. Within the edit options i have a select list field with dropdown options red green blue yellow

What i'm looking at is to restrict each role from viewing items within a dropdown list.

Example: Student role will see red blue

Teacher role will see red green

Admin role will see red green blue yellow

I searched and seems like Entity Reference is the way togo but i can not set it up correctly. A. detail step by step help would be really appreciated

update: code need help

DONE!

the following worked for me...

My form name is cal_form, field name is field_color

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

switch ($form_id)  {
    case 'cal_form': 
    global $user;
//check role
    if (in_array('student', $user->roles)) {

//student role will not see the following entries       
unset($form['field_color'][LANGUAGE_NONE]['#options']['green']);
unset($form['field_color'][LANGUAGE_NONE]['#options']['yellow']);
   }
    break; 
  } 
}

Added the above as php code as mymodule.module and activated it.


Solution

  • if it is a simple options list field then your simplest option is probably altering the edit form yourself with hook_form_alter or hook_form_FORM_ID_alter (hook_from_FORM_ID_alter is better as it will only run for that particular form).
    If you use a taxonomy reference field instead, you could use taxonomy access or taxonomy access lite.