As I understand it States API supports all form elements, not only those that are fields.

I frequently find myself needing to have different fields visible/hidden depending on a users role.

It would be great if roles could be added as a "control field".

Comments

Tobias Xy’s picture

Status: Active » Closed (won't fix)

Yes, the states API supports all form elements, but a user role isn't a form element. So in my opinion this isn't the kind of thing we want FCS to do.

I actually think you're looking for a module like Field permissions. Please correct me if I misunderstood your intention.

mustanggb’s picture

Field Permissions has no ajax (aka States API) support, so no it's not what I was looking for.

What I am looking is GUI support to add #states driven selectors, with a bit of clever handling that falls back nicely when javascript is disabled.

Perhaps I can post a bit of code that explains it better.

mustanggb’s picture

Status: Closed (won't fix) » Active

Okay, something along these lines:

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#entity_type']) && $form['#entity_type'] == 'user') {
    $role_fields = array(
      'role_tester' => array(
        'field_example',
      ),
    );

    foreach ($role_fields as $role => $fields) {
      $has_role = (isset($form['#user']->roles) && in_array($role, $form['#user']->roles));

      foreach ($fields as $field) {
        // Roles selection widget is visible
        if (user_access('administer permissions')) {
          // Allow javascript show/hide if user has the roles form item
          $roles = user_roles();
          $role_id = array_search($role, $roles);
          $form[$field]['#states']['visible'][':input[name="roles[' . $role_id . ']"]'] = array('checked' => TRUE);

          // In case javascript is disabled hide by default
          if (!$has_role) {
            $form[$field]['#attributes']['style'][] = 'display: none;';
          }
        }
        else if (!$has_role) {
          // Remove unless the user has the role
          $form[$field]['#access'] = FALSE;
        }
      }
    }
  }
}
hass’s picture

That looks like a job for field permission. Role membership does not change at form selection, too.

One thing that may be useful is that a field becomes required for one role, but not for others... But hiding fields is different situation I would only toggle with permissions and not with states.

Tobias Xy’s picture

Actually on the user edit page the role can change. It is a form element there, but not a "normal" field.
How should we handle these kind of elements in general?
In my opinion it would be nice to include them, but thats probably too much pain, because there isn't a unified way of accessing them (at least I don't know it) and hook_field_extra_fields/field_info_extra_fields isn't providing the information we would need to work with extra fields (additionally the user roles aren't exposed through this hook).
We could add special handling for each kind of entity or at least for nodes (e.g. node title) and users (e.g. roles), maybe as a submodule. How do you think about that?

mustanggb’s picture

Perhaps the ultimate solution would be to turn them into normal fields in core.

But until then I think you're right that they'd require special handling and that it'd be really good if we could include them.

Also the submodule idea sounds like the way to go because it'd keep the main normal real fields code clean, whilst still having the functionality that users have been asking for.

hass’s picture

I'm not sure how much work this is, but first of all we should really get a final 2.0 out and than look for possible future features like these here. Development does not stop after 2.0 release :-).