John81
John81

Reputation: 4074

Restricting Field List Entries Based on User Role in Drupal 7

In Drupal 7 is there a way to restrict the content of a field list based on the user role?

For example:

for user role 1, I want the custom field list to show:

Apple Banana Grape Orange

for user role 2, I want the custom field list to show:

Apple Grape

The Field Permissions module lets you restrict access to display and edit the list based on role but I'm looking for something more granular to restrict access to items in the list based on role.

Upvotes: 2

Views: 1970

Answers (2)

kunal
kunal

Reputation: 71

We can do it without any custom module simply, by use of reference view as mentioned by alex. What is required is "to select current user as filter".

Prepare a content type user_category where you save the user name against the category, eg

user: 1 - category: - apple, grape, banana
user 2 - category: apple, grape etc

Form a view of type reference and filter as current user and you are done.

Use field type as reference in the content, select the view in field options.

Upvotes: 0

theunraveler
theunraveler

Reputation: 3284

You can implement hook_field_widget_form_alter() in your own module and remove certain options based on whatever criteria you'd like. For example:

function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['field_name'] == 'field_MY_FIELD') {
    // Users without the "administer nodes" permission should not see the
    // "Banana" and "Orange" options.
    if (!user_access('administer nodes')) {
      unset($element['#options']['banana'], $element['#options']['orange']);
    }
  }
}

See http://api.drupal.org/api/drupal/modules!field!field.api.php/function/hook_field_widget_form_alter/7 for more information on hook_field_widget_form_alter().

Upvotes: 2

Related Questions