Skip to content

Commit

Permalink
Merge pull request #1640 from tripal/tv4g4-1580-importerSubmitButton
Browse files Browse the repository at this point in the history
TripalImporter support for use_button is false
  • Loading branch information
dsenalik committed Sep 19, 2023
2 parents 7de4481 + a5152db commit bf538b9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
15 changes: 9 additions & 6 deletions tripal/src/Form/TripalImporterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,15 @@ public function buildForm(array $form, FormStateInterface $form_state, $plugin_i
$form = array_merge($form, $importer_form);
}


$form['button'] = [
'#type' => 'submit',
'#value' => $importer_def['button_text'],
'#weight' => 10,
];
// We should only add a submit button if this importer uses a button.
// Examples of importers who don't use this button are mutl-page forms.
if (array_key_exists('use_button', $importer_def) AND $importer_def['use_button'] !== FALSE) {
$form['button'] = [
'#type' => 'submit',
'#value' => $importer_def['button_text'],
'#weight' => 10,
];
}

return $form;
}
Expand Down
11 changes: 10 additions & 1 deletion tripal/src/TripalImporter/Annotation/TripalImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ class TripalImporter extends Plugin {
*/
public $require_analysis;

/**
* Indicates whether the base importer should add a submit button or not.
* This should only be used in situations were you need multiple buttons
* or control over the submit process (e.g. multi-page forms).
*
* @var bool
*/
public $use_button = TRUE;

/**
* Text that should appear on the button at the bottom of the importer form.
*
Expand Down Expand Up @@ -201,4 +210,4 @@ class TripalImporter extends Plugin {
*/
public $callback_path;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TripalImporterFormBuildTest extends KernelTestBase {
'upload_title' => 'Gemstone Descriptions',
'use_analysis' => FALSE,
'require_analysis' => FALSE,
'use_button' => TRUE,
'button_text' => 'Import file',
'file_upload' => FALSE,
'file_load' => FALSE,
Expand Down Expand Up @@ -147,8 +148,6 @@ protected function setMockManager($annotation) {

/**
* Tests focusing on the Tripal importer form.
*
* @group tripal_importer
*/
public function testTripalImporterForm() {

Expand Down Expand Up @@ -203,7 +202,7 @@ public function testTripalImporterForm() {
"The importer_plugin_id[#value] should be set to our fake plugin_id.");
// a submit button.
$this->assertArrayHasKey('button', $form,
"The form should not have a submit button since we indicated a specific importer.");
"The form should have a submit button since we indicated a specific importer.");

// We should also have our importer specific form elements added to the form!
$this->assertArrayHasKey('gemstone_composition', $form,
Expand All @@ -224,8 +223,6 @@ public function testTripalImporterForm() {
/**
* Confirm that the file-related form elements are added to the form
* as expected based on plugin annotation.
*
* @group tripal_importer
*/
public function testTripalImporterFormFiles() {

Expand Down Expand Up @@ -399,8 +396,6 @@ public function testTripalImporterFormFiles() {
/**
* Confirm that the file-related form elements are added to the form
* as expected based on plugin annotation.
*
* @group tripal_importer
*/
public function testTripalImporterFormAnalysis() {

Expand Down Expand Up @@ -431,4 +426,35 @@ public function testTripalImporterFormAnalysis() {
$this->assertCount(4, $form['analysis_method']['#options'],
"There were not the expected number of options including the empty option that we expected for our analysis.");
}

/**
* Confirm that importers whose annotation indicates they do not want a submit
* button, do not get a submit button forced on them.
*/
public function testTripalImporterFormNoButton() {

$container = \Drupal::getContainer();
$plugin_id = 'fakeImporterName';
$expected = $this->definitions[$plugin_id];

// -- Indicate to use an analysis elements.
$expected['use_button'] = FALSE;
$manager = $this->setMockManager([$plugin_id => $expected]);
$container->set('tripal.importer', $manager);

// Build the form using the Drupal form builder.
$form = \Drupal::formBuilder()->getForm(
'Drupal\tripal\Form\TripalImporterForm',
$plugin_id,
);
$this->assertIsArray($form,
'We still expect the form builder to return a form array even without a plguin_id but it did not.');
$this->assertEquals('tripal_admin_form_tripalimporter', $form['#form_id'],
'We did not get the form id we expected.');

// check that our analysis element is in the form.
$this->assertArrayNotHasKey('button', $form,
"We should not have a submit button if our annotation sets use_button to FALSE but we do.");

}
}

0 comments on commit bf538b9

Please sign in to comment.