Browse Source

Merge pull request #1177 from tripal/1170-tv3-bulk_loader_fixes

Bulk Loader Bug in Required Fields
Stephen Ficklin 4 years ago
parent
commit
dae8b84fdd

+ 25 - 3
tripal_bulk_loader/includes/tripal_bulk_loader.admin.templates.inc

@@ -426,6 +426,10 @@ function tripal_bulk_loader_modify_template_base_form($form, &$form_state = NULL
             '#type' => 'hidden',
             '#value' => $priority,
           ],
+          'required' => [
+            '#type' => 'item',
+            '#markup' => $field['required'] ? '✔' : '',
+          ],
           'field_name' => [
             '#type' => 'item',
             '#markup' => $field['title'],
@@ -1848,7 +1852,16 @@ function tripal_bulk_loader_template_field_form_default_values($mode, &$form_sta
   $v['foreign_record'] = (isset($form_state['values']['foreign_record'])) ? $form_state['values']['foreign_record'] : 'NULL';
   $v['foreign_field_options'] = $ref_chado_fields;
   $v['foreign_field'] = (isset($form_state['values']['foreign_field'])) ? $form_state['values']['foreign_field'] : current($ref_chado_fields);
-  $v['required'] = (isset($form_state['values']['required'])) ? $form_state['values']['required'] : FALSE;
+
+  // The required checkbox should be forced as checked if this database
+  // column is set to 'not null'. Otherwise, use the form state.
+  if (array_key_exists('not null', $table_description['fields'][$field]) and
+      $table_description['fields'][$field]['not null'] === TRUE) {
+    $v['required'] = 'FORCE_TRUE';
+  }
+  else {
+    $v['required'] = (isset($form_state['values']['required'])) ? $form_state['values']['required'] : $template_field['required'];
+  }
 
   if (isset($original_field['regex']) && empty($form_state['storage']['regex']['pattern'])) {
     $form_state['storage']['regex']['pattern'] = $original_field['regex']['pattern'];
@@ -2169,6 +2182,16 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
     '#default_value' => $values['required'],
   ];
 
+  // If the required value is set as "FORCE_TRUE" then that means the
+  // underlying database column is set to NOT NULL so the end-user should
+  // not be able to change if this field is required or not.
+  if ($values['required'] === 'FORCE_TRUE') {
+    $form['fields']['field']['additional']['required']['#attributes']['disabled'] = TRUE;
+    $form['fields']['field']['additional']['required']['#value'] = 1;
+    $form['fields']['field']['additional']['required']['#default_value'] = 1;
+    $form['fields']['field']['additional']['required']['#title'] = 'The database constraints force this field to be required.';
+  }
+
   $form['fields']['field']['additional']['regex_transform'] = [
     '#type' => 'fieldset',
     '#title' => 'Transform Data File Value Rules',
@@ -2188,7 +2211,7 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
   $pattern_default = '';
   $replace_default = '';
 
-  // Check to see if there is more than one regex and if so warn them that 
+  // Check to see if there is more than one regex and if so warn them that
   // this feature is no longer supported. We don't want people to lose existing
   // transformation rules just by tweaking the field so we will only set defaults
   // if there is a single rule and summarize multiple rules statically.
@@ -2420,7 +2443,6 @@ function tripal_bulk_loader_template_field_form_submit($form, &$form_state) {
     $form_state['rebuild'] = FALSE;
     $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/' . $form_state['storage']['template_id'] . '/edit';
   }
-
 }
 
 /**

+ 4 - 2
tripal_bulk_loader/includes/tripal_bulk_loader.loader.inc

@@ -528,14 +528,16 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
   }
 
   // Check that template required fields are present. if a required field is
-  // missing and this
-  // is an optional record then just return. otherwise raise an error
+  // missing and this is an optional record then just return. otherwise raise
+  // an error
   $skip_optional = 0;
   foreach ($table_data['required'] as $field => $required) {
+
     if ($required) {
       // check if the field has no value (or array is empty)
       if (!isset($values[$field]) or
         (is_array($values[$field]) and count($values[$field]) == 0)) {
+
         // check if the record is optional.  For backwards compatiblity we need to
         // check if the 'mode' is set to 'optional'
         if ($table_data['optional'] or preg_match('/optional/', $table_data['mode']) or

+ 9 - 3
tripal_bulk_loader/theme/templates/tripal_bulk_loader_modify_template_base_form_fields.tpl.php

@@ -18,11 +18,12 @@ $header = [
   'Chado Field',
   'Field Type',
   'Field Settings',
+  'Required',
 ]; //'Data Column', 'Constant Value', 'Referred Record');
 $rows = [];
 
 // This is an array to keep track of the record => field index as well as the number of fields.
-// It is used to make the record column span all the field rows providing more room for 
+// It is used to make the record column span all the field rows providing more room for
 // information and a visual separation between records.
 // Expect: record_id => array(index1, index2, index3)
 $field_record = [];
@@ -81,10 +82,15 @@ foreach (element_children($element) as $key) {
     'class' => ['field-type'],
   ];
 
-  // Finally specify the field value.
-
+  // Specify the field value.
   $row[] = $value;
 
+  // Specify if the field is required.
+  $row[] = [
+    'data' => drupal_render($row_element['required']),
+    'class' => ['field-required'],
+  ];
+
   // Add this field to the table.
   $rows[] = [
     'class' => [$type_class],

+ 3 - 0
tripal_bulk_loader/theme/tripal_bulk_loader.css

@@ -48,6 +48,9 @@
   text-align: center;
   width: 50px;
 }
+#tripal-bulk-loader-modify-template-base-form td.field-required {
+  text-align: center;
+}
 #tripal-bulk-loader-modify-template-base-form td.record {
   width: 100px;
   background-color: #F2F3ED;