Untitled
unknown
plain_text
a year ago
2.0 kB
4
Indexable
Never
use Drupal\group\Entity\GroupMembership; // ... public function submitForm(array &$form, FormStateInterface $form_state) { $csv_file_fid = $form_state->getValue('csv_upload'); if (!empty($csv_file_fid)) { $file = File::load($csv_file_fid[0]); if ($file instanceof File) { $file_path = $file->getFileUri(); $csv_data = array_map('str_getcsv', file($file_path)); $header = [ $this->t('Email'), $this->t('Username'), $this->t('Account Status'), $this->t('Group Membership'), ]; $rows = []; foreach ($csv_data as $csv_row) { $email = $csv_row[0]; $query = Database::getConnection()->select('users_field_data', 'u'); $query->fields('u', ['uid', 'name']); $query->condition('u.mail', $email); $user = $query->execute()->fetchAssoc(); if (!empty($user)) { // User found $username = $user['name']; $status = $this->t('Account Present'); // Load the user's group memberships $group_memberships = GroupMembership::loadByUser($user['uid']); $group_membership_labels = []; foreach ($group_memberships as $group_membership) { $group = $group_membership->getGroup(); $group_membership_labels[] = $group->label(); } $group_membership_text = implode(', ', $group_membership_labels); } else { // User not found $username = ''; $status = $this->t('Account Not Present'); $group_membership_text = ''; } $rows[] = [$email, $username, $status, $group_membership_text]; } // Set the results in the form state $form_state->set('results', [ 'header' => $header, 'rows' => $rows, ]); $form_state->setRebuild(); // Rebuild the form to display results } } }