I'm writing a module that has an admin settings page which assigns permissions to a project per user. There is a separate form for each project (using hook_forms()), and each form shows a list of users using a select list with multiselect enabled. I have the functionality completed that will write the data to a table (one record for each user/project nid combination). What I need to do is to display which users already have access when the form is first displayed by having them be selected.

Here's my form code:

function project_permissions_page() {  //called from hook_menu()
  $output = t('Use this page to assign permissions to each project'); 

  //get list of projects
  $sql = "SELECT nid, uri FROM {project_projects}";
  $result = db_query($sql);
  
  //for each project, create a fieldset with project and user fields
  while ($projects = mysql_fetch_object($result)) {
    $project_name = $projects->uri;
    $project_nid = $projects->nid;

    //call form function
    $output .= drupal_get_form($project_name.'_permissions_form');     
  }
  return $output;  
}

/*
 * Implementation of hook_forms()
 */
function project_permissions_forms() {
  $sql = "SELECT nid, uri FROM project_projects";
  $result = db_query($sql);
  while ($project = mysql_fetch_object($result)) {    
    $forms[$project->uri.'_permissions_form'] = array(
    'callback' => 'project_permissions_form',
    'callback arguments' => array($project->nid, $project->uri)
    );
  }
  return $forms;
}

/*
 * Define the project permissions settings form
 */
function project_permissions_form($project_nid, $project_name) {     
  $form['#base'] = 'project_permissions_form';
  
  $form[$project_nid] = array(
    '#type' => 'fieldset',
    '#title' => $project_name,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );  

  $form[$project_nid]['projects'] = array(
    '#type' => 'textfield',
    '#title' => t('Project name'),
    '#maxlength' => 30,
    '#size' => 30,
    '#default_value' => $project_name,   
  );
  
  $form[$project_nid]['users'] = array(
    '#type' => 'select',
    '#title' => t('Users with access to project'),
    '#description' => t('Select users to be assigned to the project'),    
    '#options' => _get_user_list(),
    '#multiple' => TRUE
  );
  
  $form[$project_nid]['nid'] = array(
    '#type' => 'hidden',
    '#value'=> $project_nid,
  );
  
  $form[$project_nid]['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit'
  );    
  return $form;
}  

/*
 * Gets lists of projects from project_projects table
 */
function _get_projects_list() {
  $sql = "SELECT nid, uri FROM {project_projects}";
  $result = db_query($sql) or die('Error'. mysql_error());
  while($projects = db_fetch_object($result)) {
    $options[$projects->nid] = $projects->uri;
  }
  return $options;
}

/*
 * Gets lists of users from users table
 */
function _get_user_list() {
  $sql = "SELECT uid, name FROM {users} WHERE uid > %d";
  $result = db_query($sql,0) or die('Error'. mysql_error());
  while($users = db_fetch_object($result)) {
    $options[$users->uid] = $users->name;
  }
  return $options;
}  

/*
 * Implementation of hook_submit()
 */
function project_permissions_form_submit($form, $form_values) {
  $nid = $form_values['nid'];
  $user_list = $form_values['users'];
  
  //delete existing records for project
    $sql_delete = "DELETE FROM {project_permissions} WHERE nid = %d";
    $result_delete = db_query($sql_delete, $nid);
  
  //cycle through submitted user IDs and add one record for each one to table
  foreach ($user_list as $key => $value) {
    $uid = $value;
    $sql_add = "INSERT INTO {project_permissions} (nid, uid) VALUES (%d, %d)";
    $result_add = db_query($sql_add, $nid, $uid);         
  }
 
  drupal_set_message('Project permissions settings have been saved');
  
}

From what I've read, I'm pretty sure I need to use #attribute, but I'm not sure how to do it for each individual item in the list and I'm guessing that I would either add the "selected" attribute in the _get_user_list function (since that's where I'm getting my list of users), or add another query in the form builder function. If anybody could point me in the right direction, that would be great.

Thanks.

Comments

wonder95’s picture

I did some playing around, and it looks like #attributes isn't the answer, because it is for the element as a whole, not the individual options in the list. Any ideas on how I can add an attribute to the tags?

Thanks.