Skip to content

Commit

Permalink
Merge pull request #129 from TripalCultivate/g2.117-fixEmptyRowBug
Browse files Browse the repository at this point in the history
Fixes empty row bug
  • Loading branch information
laceysanderson authored Dec 20, 2024
2 parents 6437caa + 03782b5 commit c230de9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\trpcultivate_phenotypes\Plugin\TripalImporter;

use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Renderer;
use Drupal\Core\StringTranslation\StringTranslationTrait;
Expand Down Expand Up @@ -143,6 +144,13 @@ class TripalCultivatePhenotypesTraitsImporter extends ChadoImporterBase implemen
*/
protected Renderer $service_Renderer;

/**
* The Drupal Messenger Service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $service_Messenger;

/**
* Used to reference the validation result summary in the form.
*
Expand Down Expand Up @@ -173,6 +181,8 @@ class TripalCultivatePhenotypesTraitsImporter extends ChadoImporterBase implemen
* The service used to generate the termplate file.
* @param Drupal\Core\Render\Renderer $renderer
* The Drupal renderer service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The Drupal messenger service.
*/
public function __construct(
array $configuration,
Expand All @@ -185,6 +195,7 @@ public function __construct(
EntityTypeManager $service_entityTypeManager,
TripalCultivatePhenotypesFileTemplateService $service_FileTemplate,
Renderer $renderer,
MessengerInterface $messenger,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $chado_connection);

Expand All @@ -196,6 +207,7 @@ public function __construct(
$this->service_entityTypeManager = $service_entityTypeManager;
$this->service_FileTemplate = $service_FileTemplate;
$this->service_Renderer = $renderer;
$this->service_Messenger = $messenger;
}

/**
Expand All @@ -213,6 +225,7 @@ public static function create(ContainerInterface $container, array $configuratio
$container->get('entity_type.manager'),
$container->get('trpcultivate_phenotypes.template_generator'),
$container->get('renderer'),
$container->get('messenger'),
);
}

Expand Down Expand Up @@ -353,7 +366,7 @@ public function form($form, &$form_state) {
// This is a reminder to user about expected trait data.
$phenotypes_minder = $this->t('This importer allows for the upload of phenotypic trait dictionaries in preparation
for uploading phenotypic data. <br /><strong>This importer Does NOT upload phenotypic measurements.</strong>');
\Drupal::messenger()->addWarning($phenotypes_minder);
$this->service_Messenger->addWarning($phenotypes_minder);

// Field Genus:
// Prepare select options with only active genus.
Expand All @@ -362,7 +375,7 @@ public function form($form, &$form_state) {

if (!$active_genus) {
$phenotypes_minder = $this->t('This module is <strong>NOT configured</strong> to import Traits for analyzed phenotypes.');
\Drupal::messenger()->addWarning($phenotypes_minder);
$this->service_Messenger->addWarning($phenotypes_minder);
}

// If there is only one genus, it should be the default.
Expand Down Expand Up @@ -398,6 +411,9 @@ public function form($form, &$form_state) {
* {@inheritdoc}
*/
public function formSubmit($form, &$form_state) {
// Display successful message to user if file import was without any error.
$this->service_Messenger
->addStatus($this->t('<b>Your file import was successful and a Job Process Request has been created to securely save your data.</b>'));
}

/**
Expand Down Expand Up @@ -590,7 +606,26 @@ public function formValidate($form, &$form_state) {
$storage[$this->validation_result] = $validation_feedback;
$form_state->setStorage($storage);

if ($failed_validator === TRUE) {
// Check if the $validation_feedback contains 'fail' or 'todo' status.
// If either is found, prevent form submission.
$submit_form = TRUE;

foreach ($validation_feedback as $feedback_item) {
if ($feedback_item['status'] == 'todo' || $feedback_item['status'] == 'fail') {
$submit_form = FALSE;

// No need to inspect other validators, a single instance of fail/todo
// is sufficient to prevent form submission.
break;
}
}

if ($submit_form === FALSE) {
// Provide a general error message indicating that input values and/or the
// data file may contain one or more errors.
$this->service_Messenger
->addError($this->t('Your file import was not successful. Please check the Validation Result Window for errors and try again.'));

// Prevent this form from submitting and reload form with all the
// validation failures in the storage system.
$form_state->setRebuild(TRUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,27 @@ public function testTraitFormValidation(string $submitted_genus, string $filenam
);
}
}

// Assert that the default value of genus field is the genus
// entered/selected, indicating that on form validate error, the form was
// not submitted and reloaded with the genus value as default.
$this->assertEquals(
$form_state->getValue('genus'),
$submitted_genus,
'The import form should set the default value of genus to the genus entered if the form was not submitted due to validation error.'
);

// If the form was not submitted due to validation error, check to ensure
// that no Tripal Job was created in the process.
$tripal_jobs = $this->chado_connection->query(
'SELECT job_id FROM {tripal_jobs} ORDER BY job_id DESC LIMIT 1'
)
->fetchField();

$this->assertFalse(
$tripal_jobs,
'A failed import due to validation error that did not submit should not create a job request.'
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ protected function setUp(): void {
$this->container->get('entity_type.manager'),
$this->container->get('trpcultivate_phenotypes.template_generator'),
$this->container->get('renderer'),
$this->container->get('messenger'),
);

}
Expand Down

0 comments on commit c230de9

Please sign in to comment.