I have a situation where I need to be able to show some fields on the node edit form, but only enable editing of those fields for certain user roles. However, even user roles that cannot edit those fields need to be able to see them while editing. The field_permissions module does not currently support this functionality, but I think it would be a worthwhile enhancement. I realize that adding this functionality could be a bit tricky, but as a possible starting-point, here's some code that I put into a custom "job" module to designate a few fields as read-only:

<?php
/**
 * Implements hook_form_alter().
 */
function job_permission() {
  return array(
    'modify restricted project fields' => array(
      'title' => t('Modify restricted project fields'), 
      'description' => t('Modify the agency name, labor category and assignment number fields on project pages.'),
    ),
  );
}

/**
 * Implements hook_form_alter().
 */
function job_form_job_node_form_alter(&$form, &$form_state, $form_id) {
  if (!user_access('modify restricted project fields')) {
    $form['field_agency_term']['#disabled'] = TRUE;
    $form['field_ogs_labor_category']['#disabled'] = TRUE;
    $form['field_assignment_number']['#disabled'] = TRUE;
  }
}
?>

Comments

sheldon rampton’s picture

I think my use case is the sort of functionality that would be appropriate to delegate to a contrib module such as field_permissions. I've filed a comment on an issue ticket there relating to this. However, adding a "view as read-only widget during editing" option to node access definitions in Drupal core would make it easier to do this and also to develop other contrib modules that might have other ways of deciding when to make fields read-only during editing.

David_Rothstein’s picture

sheldon rampton’s picture

Yes, I guess it is.

sheldon rampton’s picture

Status: Active » Closed (duplicate)