Explorar el Código

Fixed issue saving properties. Moved field admin form alter to the Chado module. Fixed labels and descriptions on property widgets

Stephen Ficklin hace 10 años
padre
commit
32a1a3918c

+ 0 - 26
tripal/includes/tripal.fields.inc

@@ -128,32 +128,6 @@ function tripal_field_no_delete() {
   return '';
 }
 
-/**
- *
- * Implements hook_form_FORM_ID_alter().
- *
- * The field_ui_field_edit_form is used for customizing the settings of
- * a field attached to an entity.
- *
- * This alter function disables some of the form widgets when the storage
- * backend indicates they are not appropriate.
- */
-function tripal_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
-
-  // For entity fields added by Tripal Entities we don't want the
-  // the end-user to change the cardinality and the required fields
-  // such that record can't be saved in Chado.
-
-  // TODO: this shouldn't be hardcoded here.  These settings
-  // should be part of the field and handled by the tripal_entity module.
-  if ($form['#instance']['entity_type'] == 'TripalEntity') {
-    $form['field']['cardinality']['#access'] = FALSE;
-    $form['instance']['required']['#access'] = FALSE;
-  }
-
-  // TODO: don't the the maximum length be larger than the field size.
-}
-
 /**
  *
  * Implements hook_form_FORM_ID_alter().

+ 20 - 74
tripal_chado/includes/TripalFields/chado_linker__prop.inc

@@ -53,7 +53,7 @@ class chado_linker__prop extends TripalField {
     $field_type = $this->field['type'];
     $field_table = $this->field['settings']['chado_table'];
     $field_column = $this->field['settings']['chado_column'];
-
+    $instance = $this->instance;
 
     // Get the table name and cvterm that this field maps to.
     $matches = array();
@@ -81,17 +81,6 @@ class chado_linker__prop extends TripalField {
       $propval = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__value', $propval);
     }
 
-    // The group of elements all-together need some extra functionality
-    // after building up the full list (like draggable table rows).
-    $widget['#theme'] = 'field_multiple_value_form';
-    $widget['#title'] = $element['#title'];
-    $widget['#description'] = $element['#description'];
-    $widget['#field_name'] = $element['#field_name'];
-    $widget['#language'] = $element['#language'];
-    $widget['#weight'] = isset($element['#weight']) ? $element['#weight'] : 0;
-    //$widget['#element_validate'] = array('chado_linker__prop_widget_validate');
-    $widget['#cardinality'] = 1;
-
     $widget['value'] = array(
       '#type' => 'value',
       '#value' => key_exists($delta, $items) ? $items[$delta]['value'] : '',
@@ -108,6 +97,8 @@ class chado_linker__prop extends TripalField {
     $widget['chado-' . $table_name . '__value'] = array(
       '#type' => 'textfield',
       '#default_value' => $propval,
+      '#title' => $instance['label'] . ' value',
+      '#description' => $instance['description'],
     );
     $widget['chado-' . $table_name . '__type_id'] = array(
       '#type' => 'hidden',
@@ -124,68 +115,32 @@ class chado_linker__prop extends TripalField {
    * @see TripalField::validate()
    */
   public function validate($entity_type, $entity, $field, $items, &$errors) {
-/*     $field_name = $this->field['field_name'];
-
-    $matches = array();
-
-    // Get the record and table mapping info.
-    $chado_table = $entity->chado_table;
-    $chado_column = $entity->chado_column;
-
-    // Get the table name and cvterm_id for this field.
-    preg_match('/(.*?)__(\d+)/', $field_name, $matches);
-    $table_name = $matches[1];
-    $cvterm_id = $matches[2];
-
-    // Get the name of the pkey field for this property table and the name
-    // of the FK field that links to the base table.
-    $schema = chado_get_schema($table_name);
-    $pkey = $schema['primary key'][0];
-    $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
-
-    // If we don't have a property value then we need to set all other fields
-    // to be empty so that when the module tries to save the field on the
-    // entity it won't try to save a partial record.
-//     $pkey_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta);
-//     $prop_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . "__value");
-//     $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__' . $lfkey_field);
-//     $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__type_id');
 
-    if (!$prop_value) {
-      tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $lfkey_field);
-      tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__value');
-      tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__type_id');
-      tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__rank');
-    }
-    else {
-      $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, '_weight');
-      tripal_chado_set_field_form_values($field_name, $form_state, $rank, $delta, $table_name . '__rank');
-    }
-    // Remove the properties for this record. We will re-add it. Otherwise,
-    // if we change ranks, we wind up with multiple records in the property table.
-    if ($pkey_val) {
-      $match = array(
-        $pkey => $pkey_val
-      );
-      chado_delete_record($table_name, $match);
-    } */
   }
 
   /**
    * @see TripalField::widgetFormSubmit()
    */
   public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
-    
+
     $field_name = $this->field['field_name'];
-    $field_table = $this->field['settings']['chado_table'];
+    $table_name = $this->field['settings']['chado_table'];
+    $schema = chado_get_schema($table_name);
+    $pkey = $schema['primary key'][0];
+    $base_table = $this->field['settings']['base_table'];
+    $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
+    $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
 
-    // Don't save if there is no value for the property
-    if (!trim($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . 'value'])) {
-      foreach($form_state['values'][$field_name][$langcode][$delta] AS $key => $value) {
-        if (preg_match('/^chado-/', $key)) {
-          $form_state['values'][$field_name][$langcode][$delta][$key] = '__NULL__';
-        }
-      }
+    $prop_value = $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__value'];
+
+    // If the user removed the contact from the contact_name field
+    // then we want to clear out the rest of the hidden values.
+    // Leave the primary key so the record can be deleted.
+    if (!$prop_value) {
+      $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $pkey] = '';
+      $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__rank'] = '';
+      $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__type_id'] = '';
+      $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $lfkey_field] = '';
     }
   }
 
@@ -248,12 +203,3 @@ class chado_linker__prop extends TripalField {
     }
   }
 }
-/**
- *
- * @param unknown $form
- * @param unknown $form_state
- */
-function chado_linker__prop_widget_form_ajax_callback($form, $form_state) {
-  $field_name = $form_state['triggering_element']['#parents'][0];
-  return $form[$field_name];
-}

+ 9 - 8
tripal_chado/includes/TripalFields/chado_linker__prop_adder.inc

@@ -137,7 +137,7 @@ class chado_linker__prop_adder extends TripalField {
         $widget['wrapper']['cardinality'] = array(
           '#title'       => t('Number of Values'),
           '#type'        => 'textfield',
-          '#description' => t("The number of values allowed for this property. 0 for unlimited values"),
+          '#description' => t("A number of 1 or more indicating the number of values allowed for this property. Enter -1 for unlimited values"),
           '#size' => 10,
           '#default_value' => 1,
         );
@@ -162,10 +162,10 @@ class chado_linker__prop_adder extends TripalField {
       // If the user has clicked the 'user_term_button' then we need to makes
       // sure that the cardinality textbox contains a positive integer.
       $cardinality = $form_state['values'][$field_name][$langcode][$delta]['wrapper']['cardinality'];
-      if (!preg_match('/^\d+$/', $cardinality) or $cardinality < 0) {
-        form_set_error("$field_name][$langcode][$delta][wrapper][cardinality", "Please provide positive number for the number of values.");
+      if (!preg_match('/^-*\d+$/', $cardinality) or $cardinality < -2 or $cardinality == 0) {
+        form_set_error("$field_name][$langcode][$delta][wrapper][cardinality", "Please provide positive number for the number of values, or -1 for unlimited.");
       }
-      
+
       // Get selected terms
       $terms_list = $form_state['values'][$field_name][$langcode][$delta]['wrapper']['terms_list'];
       $counter = 0;
@@ -204,7 +204,6 @@ class chado_linker__prop_adder extends TripalField {
    */
   public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
 
-
     // Add the new field to the entity but only if the property adder button
     // was clicked
     if (!array_key_exists('triggering_element', $form_state) or
@@ -274,9 +273,11 @@ class chado_linker__prop_adder extends TripalField {
         'label' => ucfirst(preg_replace('/_/', ' ', $cvterm->name)),
         'description' => $cvterm->definition ? $cvterm->definition : '',
         'required' => FALSE,
-        'settings' => array(),
+        'settings' => array(
+          'auto_attach' => FALSE,
+        ),
         'widget' => array(
-          'type' => $field_name . '_widget',
+          'type' => 'chado_linker__prop_widget',
           'settings' => array(
             'display_label' => 1,
           ),
@@ -284,7 +285,7 @@ class chado_linker__prop_adder extends TripalField {
         'display' => array(
           'default' => array(
             'label' => 'inline',
-            'type' => $field_name . '_formatter',
+            'type' => 'chado_linker__prop_formatter',
             'settings' => array(),
           ),
         ),

+ 15 - 12
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -352,7 +352,10 @@ function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $e
     // Iterate through the field's items. Fields with cardinality ($delta) > 1
     // are multi-valued.
     $items = field_get_items($entity_type, $entity, $field_name);
+    $temp = array();
+
     foreach ($items as $delta => $item) {
+
       // A field may have multiple items. The field can use items
       // indexed with "chado-" to represent values that should map directly
       // to chado tables and fields.
@@ -361,25 +364,25 @@ function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $e
         if (preg_match('/^chado-(.*?)__(.*?)$/', $item_name, $matches)) {
           $table_name = $matches[1];
           $column_name = $matches[2];
-          // If not seen this table and column then just add it. If we've
-          // already seen it then it means it's a FK field and we've already
-          // added subfields so do nothing.
-          if (!array_key_exists($table_name, $new_fields) or
-              !array_key_exists($delta, $new_fields[$table_name]) or
-              !array_key_exists($column_name, $new_fields[$table_name][$delta])) {
-            $new_fields[$table_name][$delta][$column_name] = $value;
-          }
+          $temp[$table_name][$delta][$column_name] = $value;
         }
       }
       // If there is no value set for the field using the
       // chado-[table_name]__[field name] naming schema then check if a 'value' item
       // is present and if so use that.
-      if ((!array_key_exists($chado_table, $new_fields) or
-           !array_key_exists($delta, $new_fields[$chado_table]) or
-           !array_key_exists($chado_column, $new_fields[$chado_table][$delta])) and
+      if ((!array_key_exists($chado_table, $temp) or
+           !array_key_exists($delta, $temp[$chado_table]) or
+           !array_key_exists($chado_column, $temp[$chado_table][$delta])) and
           array_key_exists('value', $items[$delta]) and
           !is_array($items[$delta]['value'])) {
-        $new_fields[$chado_table][$delta][$chado_column] = $items[$delta]['value'];
+        $temp[$chado_table][$delta][$chado_column] = $items[$delta]['value'];
+      }
+    }
+
+    // Now merge the records for this field with the $new_fields array
+    foreach ($temp as $table_name => $details) {
+      foreach ($details as $delta => $list) {
+        $new_fields[$table_name][] = $list;
       }
     }
   }

+ 2 - 1
tripal_chado/includes/tripal_chado.fields.inc

@@ -1403,4 +1403,5 @@ function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_sta
       unset($form['fields'][$field_name]);
     }
   }
-}
+}
+

+ 29 - 0
tripal_chado/tripal_chado.module

@@ -768,3 +768,32 @@ function tripal_chado_node_delete($node) {
   db_query($sql, array('nid' => $nid));
 }
 
+
+/**
+ *
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * The field_ui_field_edit_form is used for customizing the settings of
+ * a field attached to an entity.
+ *
+ * This alter function disables some of the form widgets when the storage
+ * backend indicates they are not appropriate.
+ */
+function tripal_chado_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
+  // For entity fields added by Tripal Entities we don't want the
+  // the end-user to change the cardinality and the required fields
+  // such that record can't be saved in Chado.
+
+  // TODO: we shouldn't check for specific field types here
+  // (e.g. chaod_linker_prop). That should be done via the TripalField
+  // functions.
+  if ($form['#instance']['entity_type'] == 'TripalEntity') {
+    if ($form['#field']['storage']['type'] == 'field_chado_storage' and
+        $form['#field']['type'] != 'chado_linker__prop') {
+      $form['field']['cardinality']['#access'] = FALSE;
+      $form['instance']['required']['#access'] = FALSE;
+    }
+  }
+
+  // TODO: don't the the maximum length be larger than the field size.
+}