custom_csv_user_checker.info.yml
------------------------------------------------
name: 'Custom CSV User Checker'
type: module
description: 'Processes a CSV file to check user account presence.'
core_version_requirement: ^8 || ^9
package: Custom
dependencies:
- drupal:user
custom_csv_user_checker.module
-----------------------------------------------
<?php
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form().
*/
function custom_csv_user_checker_form($form, FormStateInterface $form_state) {
$form['csv_upload'] = [
'#type' => 'file',
'#title' => t('Upload CSV File'),
'#description' => t('Upload a CSV file containing email addresses.'),
'#upload_validators' => [
'file_validate_extensions' => ['csv'],
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Process CSV'),
];
return $form;
}
/**
* Form submission handler.
*/
function custom_csv_user_checker_form_submit($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 = ['Email', 'Username', 'Account Status'];
$rows = [];
foreach ($csv_data as $csv_row) {
$email = $csv_row[0];
$account = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties(['mail' => $email]);
if (!empty($account)) {
$account = reset($account);
$username = $account->getAccountName();
$status = 'Account Present';
} else {
$username = '';
$status = 'Account Not Present';
}
$rows[] = [$email, $username, $status];
}
$form_state->set('results', [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
]);
}
/**
* Implements hook_theme().
*/
function custom_csv_user_checker_theme($existing, $type, $theme, $path) {
return [
'csv_user_checker_results' => [
'variables' => ['results' => []],
],
];
}
custom_csv_user_checker.routing.yml
----------------------------------------------------
custom_csv_user_checker.form:
path: '/custom-csv-user-checker'
defaults:
_form: '\Drupal\custom_csv_user_checker\Form\CustomCsvUserCheckerForm'
_title: 'CSV User Checker'
requirements:
_permission: 'access content'