Score:0

Adding a select box to a tableselect

ph flag

I'm trying to use the technique outlined in this blog post:

https://www.hexblot.com/blog/drupal-tableselect-fields

to add a select box to a tableselect, but there are a couple of issues:

  • The default value is not being respected (I've tried using #value and #default_value)
  • The roles element is empty on submit

Any idea what I'm missing? Here's my code:

foreach ($query as $row) {
  $member = user_load($row->etid);
  $roles = og_get_user_roles('node', $org->nid, $member->uid, FALSE);
  $role = reset($roles);
  $role = $role ? $role : 'campaigner';
  $rows[$member->uid] = [ 
    'mail' => $member->mail,
    'current_role' => $role,
    'role' => [ 
      'data' => [ 
        '#type' => 'select',
        '#name' => 'role[' . $member->uid . ']',
        '#options' => [ 
          'campaigner' => 'Campaigner',
          'administrator' => 'Administrator',
        ],
        '#default_value' => $role,
      ],
    ],
  ];
} 

$form['team_heading']['team'] = [
  '#type' => 'tableselect',
  '#header' => [
    'mail' => t('Email'),
    'current_role' => t('Current Role'),
    'role' => t('V2 Role')
  ],
  '#options' => $rows,
  '#js_select' => FALSE,
  '#default_value' => drupal_map_assoc(array_keys($rows)),
];

$form['role'] = ['#type' => 'value'];
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.