Browse Source

finished so__transcript field settings form

Stephen Ficklin 4 years ago
parent
commit
aaa4b156b0

+ 22 - 1
tripal/includes/TripalFields/TripalField.inc

@@ -633,9 +633,16 @@ class TripalField {
   }
 
   /**
+   * Validates the instance settings form.
+   *
+   * Remember if you used the 'themeable' option for form elements you
+   * must copy the values correctly.  See the description in hte
+   * `instanceSettingsForm()` function for details.
    *
    * @param $form
+   *   The form object.
    * @param $form_state
+   *   The form_state object.
    */
   public function settingsFormValidate($form, &$form_state) {
 
@@ -656,6 +663,17 @@ class TripalField {
    *   $element = parent::instanceSettingsForm();
    * @endcode
    *
+   * Additinally, theming of the instance setting form is not supported by
+   * Drupal, but Tripal will allow you to theme form elements if you
+   * place them in the $element['themeable'] variable.  However, if you place
+   * form elements there you must set corresponding form elements of type
+   * 'value' at the base level of the element array and copy appropriate
+   * settings values  from the
+   * $form_state['values']['instance']['settings']['themeable'] variable into
+   * the
+   * $form_state['values']['instance']['settings'] variable. Do this in the
+   * `instanceSettingsFormValidate()` function.
+   *
    * Please note, the form generated with this function does not easily
    * support AJAX calls in the same way that other Drupal forms do.  If you
    * need to use AJAX you must manually alter the $form in your ajax call.
@@ -668,7 +686,10 @@ class TripalField {
 
     $element['#field'] = $this->field;
     $element['#instance'] = $this->instance;
-    $element['#theme'] = ['tripal_field_settings_default'];
+    $element['themeable'] = [];
+    $element['themeable']['#field'] = $this->field;
+    $element['themeable']['#instance'] = $this->instance;
+    $element['themeable']['#theme'] = 'tripal_field_settings_default';
     $element['#element_validate'][] = 'tripal_field_instance_settings_form_validate';
     return $element;
   }

+ 2 - 2
tripal/includes/tripal.fields.inc

@@ -445,8 +445,8 @@ function tripal_field_instance_settings_form($field, $instance) {
   $field_class = $field['type'];
   if (tripal_load_include_field_class($field_class)) {
     $field = new $field_class($field, $instance);
-    $form =  $field->instanceSettingsForm();
-    return [$form];
+    $form = $field->instanceSettingsForm();
+    return $form;
   }
 }
 

+ 58 - 8
tripal_chado/includes/TripalFields/so__transcript/so__transcript.inc

@@ -206,27 +206,35 @@ class so__transcript extends ChadoField {
     $fields = field_info_instances('TripalEntity', $bundle->name);
 
     foreach ($fields as $field_name => $details) {
-      $weight = $details['display']['default']['weight'];
-      $element['field-'. $weight] = [
+
+      $defaults = [];
+      if (array_key_exists($field_name, $settings['transcript_fields'])) {
+        $defaults = $settings['transcript_fields'][$field_name];
+      }
+
+      $weight = array_key_exists('weight', $defaults) ? $defaults['weight'] : $details['display']['default']['weight'];
+      $checked =  array_key_exists('show', $defaults) ? $defaults['show'] : 0;
+      $element['themeable']['field-'. $weight] = [
         '#type' => 'value',
         '#value' => $field_name,
         '#weight' => $weight,
       ];
-      $element['field-'. $weight . '-check'] = [
+      $element['themeable']['field-'. $weight . '-check'] = [
         '#type' => 'checkbox',
         '#weight' => $weight,
+        '#default_value' => $checked,
       ];
-      $element['field-'. $weight . '-label'] = [
+      $element['themeable']['field-'. $weight . '-label'] = [
         '#type' => 'markup',
         '#markup' => t($details['label']),
         '#weight' => $weight,
       ];
-      $element['field-'. $weight . '-description'] = [
+      $element['themeable']['field-'. $weight . '-description'] = [
         '#type' => 'markup',
         '#markup' => t($details['description']),
         '#weight' => $weight,
       ];
-      $element['field-'. $weight . '-weight'] = [
+      $element['themeable']['field-'. $weight . '-weight'] = [
         '#type' => 'textfield',
         '#default_value' => $weight,
         '#weight' => $weight,
@@ -239,6 +247,45 @@ class so__transcript extends ChadoField {
     return $element;
   }
 
+  /**
+   * @see TripalField::instanceSettingsFormValidate()
+   */
+  public function instanceSettingsFormValidate($form, &$form_state) {
+    $settings = $form_state['values']['instance']['settings']['themeable'];
+
+    // Iterate through the themable elements to get the weights and
+    // checks for each field. Store that info in a temp array.
+    $temp = [];
+    foreach ($settings as $element => $value) {
+      $matches = [];
+      if (preg_match('/^field-(\d+)-check$/', $element, $matches)) {
+        $orig_weight = $matches[1];
+        $new_weight = $settings['field-' . $orig_weight . '-weight'];
+        $field_name = $settings['field-' . $orig_weight];
+        $temp[$new_weight]['name'] = $field_name;
+        $temp[$new_weight]['show'] = $value;
+      }
+    }
+
+    // Sort and resstart the weights from zero.
+    ksort($temp);
+    $temp = array_values($temp);
+
+    // Now build the transcirpt_fields setting value.
+    $transcript_fields = [];
+    foreach ($temp as $weight => $field) {
+      $field_name = $field['name'];
+      $transcript_fields[$field_name] = [
+        'weight' => $weight,
+        'show' => $field['show'],
+      ];
+    }
+
+    // Now store the values for the setting properly and remove the unwanted.
+    $form_state['values']['instance']['settings']['transcript_fields'] = $transcript_fields;
+    unset($form_state['values']['instance']['settings']['themeable']);
+  }
+
   /**
    * @see TripalField::settingsTheme()
    */
@@ -284,11 +331,14 @@ class so__transcript extends ChadoField {
     $fset = [];
     $fset['fields'] = [
       '#type' => 'fieldset',
-      '#title' => 'Field Selection',
-      '#description' => t('Select the fields that you want displayed on the gene page for each transcript that belongs to the gene.  If no fields are selected then a default display is provided.'),
+      '#title' => 'Transcipt (mRNA) Field Selection',
+      '#description' => t('Select the fields that you want displayed on the gene page for each transcript.  If no fields are selected then a default display is provided.'),
       '#collapsible' => TRUE,
       '#collapsed' => TRUE,
+      '#attributes' => ['class' => ['collapsible', 'collapsed']],
     ];
+    drupal_add_library('system', 'drupal.collapse');
+
     $fset['fields']['table'] = [
       '#type' => 'markup',
       '#markup' => theme_table($table),

+ 1 - 0
tripal_chado/includes/TripalFields/so__transcript/so__transcript_formatter.inc

@@ -16,6 +16,7 @@ class so__transcript_formatter extends ChadoFieldFormatter {
 
     // Get the settings
     $settings = $display['settings'];
+    dpm($this->instance['settings']);
 
     $headers = ['Transcript Name', 'Identifier', 'Type', 'Location'];
     $rows = [];