Untitled
plain_text
a month ago
1.8 kB
1
Indexable
Never
<?php namespace Drupal\custom_csv_user_checker\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\user\Entity\User; class CustomCsvUserCheckerForm extends FormBase { public function getFormId() { return 'custom_csv_user_checker_form'; } public function buildForm(array $form, FormStateInterface $form_state) { $form['csv_upload'] = [ '#type' => 'file', '#title' => $this->t('Upload CSV File'), '#description' => $this->t('Upload a CSV file containing email addresses.'), '#upload_validators' => [ 'file_validate_extensions' => ['csv'], ], ]; $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Process CSV'), ]; return $form; } public function submitForm(array &$form, FormStateInterface $form_state) { $csv_file = $form_state->getValue('csv_upload')[0]; $file_path = \Drupal::service('file_system')->realpath($csv_file->getFileUri()); $csv_data = array_map('str_getcsv', file($file_path)); $header = [$this->t('Email'), $this->t('Username'), $this->t('Account Status')]; $rows = []; foreach ($csv_data as $csv_row) { $email = $csv_row[0]; $account = User::loadByProperties(['mail' => $email]); if (!empty($account)) { $account = reset($account); $username = $account->getAccountName(); $status = $this->t('Account Present'); } else { $username = ''; $status = $this->t('Account Not Present'); } $rows[] = [$email, $username, $status]; } $form_state->set('results', [ '#theme' => 'table', '#header' => $header, '#rows' => $rows, ]); } }