ai_agents_test-1.0.0-alpha1/src/Form/ImportTestForm.php
src/Form/ImportTestForm.php
<?php
declare(strict_types=1);
namespace Drupal\ai_agents_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Form controller for importing tests.
*/
final class ImportTestForm extends FormBase {
/**
* Constructs a new ImportTestForm object.
*/
public function __construct(
protected PrivateTempStoreFactory $tempStore,
) {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('tempstore.private'),
);
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'ai_agents_test_import';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
// File upload form with restrictions for YAML files.
$form['file'] = [
'#type' => 'file',
'#title' => $this->t('Upload a test file'),
'#description' => $this->t('Upload a YAML file containing test data. Only .yml or .yaml files are allowed.'),
'#attributes' => [
'accept' => '.yml,.yaml',
],
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Import'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
// Get the temporary file URI.
/** @var \Symfony\Component\HttpFoundation\FileBag $files */
$files = $this->getRequest()->files;
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = NULL;
foreach ($files as $file_objects) {
$file = $file_objects['file'];
}
// If no file.
if (empty($file)) {
$form_state->setErrorByName('file', $this->t('Please upload a file.'));
return;
}
// Get the yaml.
try {
$yaml = file_get_contents($file->getRealPath());
}
catch (\Exception $e) {
$form_state->setErrorByName('file', $this->t('The file could not be read. Please check the file and try again.'));
return;
}
try {
// Parse the YAML file.
$parsed_yaml = Yaml::parse($yaml);
}
catch (\Exception $e) {
$form_state->setErrorByName('file', $this->t('The file is not a valid YAML file. Please check the file and try again.'));
return;
}
// Check so all keys are present.
$required_keys = [
'label',
'description',
'ai_agent',
'rules',
'messages',
'config_reset',
'triggering_instructions',
];
foreach ($required_keys as $key) {
if (!array_key_exists($key, $parsed_yaml)) {
$form_state->setErrorByName('file', $this->t('The file is missing the required key: @key. Please check the file and try again.', ['@key' => $key]));
return;
}
}
// Also make sure no extra keys are present.
$allowed_keys = [
'label',
'description',
'ai_agent',
'rules',
'messages',
'config_reset',
'triggering_instructions',
'tokens',
];
foreach ($parsed_yaml as $key => $value) {
if (!in_array($key, $allowed_keys)) {
$form_state->setErrorByName('file', $this->t('The file contains an unknown key: @key. Please check the file and try again.', ['@key' => $key]));
return;
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
// Get the temporary file URI.
/** @var \Symfony\Component\HttpFoundation\FileBag $files */
$files = $this->getRequest()->files;
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = NULL;
foreach ($files as $file_objects) {
$file = $file_objects['file'];
}
// Get the yaml.
$yaml = file_get_contents($file->getRealPath());
// Set in a user temporary session variable.
$this->tempStore->get('ai_agents_test')->set('import', $yaml);
// Forward to the interactive import form.
$form_state->setRedirect('ai_agents_test.interactive_add_form');
}
}
