Parcourir la source

Fixed bug with loading fields

Stephen Ficklin il y a 8 ans
Parent
commit
3e452e8b6c

+ 2 - 2
tripal/api/tripal.entities.api.inc

@@ -789,9 +789,9 @@ function tripal_replace_entity_tokens($string, &$entity, $bundle_entity = NULL)
         $field_ids[$field['id']] = array($entity->id);
       }
       $entities = array($entity->id => $entity);
-      module_invoke($storage['module'], 'field_storage_load', 'TripalEntity',
-        $entities, FIELD_LOAD_CURRENT, $field_ids, array());
     }
+    module_invoke($storage['module'], 'field_storage_load', 'TripalEntity',
+        $entities, FIELD_LOAD_CURRENT, $field_ids, array());
   }
 
   // Now that all necessary fields are attached process the tokens.

+ 14 - 20
tripal/includes/TripalEntityController.inc

@@ -488,8 +488,7 @@ class TripalEntityController extends EntityAPIController {
     // to keep looking them up in our loop below).
     $instances = array();
 
-    // Add in the content_type field by default. It should always be
-    // attached.
+    // Add in the content_type field by default. It should always be attached.
     $ids = array();
     $ct = field_info_field('content_type');
     $ids[] = $ct['id'];
@@ -505,8 +504,6 @@ class TripalEntityController extends EntityAPIController {
         $function = 'field_attach_load';
       }
       foreach ($queried_entities as $entity) {
-        $entity_ids = array();
-        $entity_ids += $field_ids;
         // Iterate through the fields and find those that are set to
         // 'auto_attach' and which are attached to this bundle.
         foreach ($fields as $field) {
@@ -527,34 +524,31 @@ class TripalEntityController extends EntityAPIController {
                 if (array_key_exists('settings', $instance) and
                     array_key_exists('auto_attach', $instance['settings']) and
                     $instance['settings']['auto_attach'] === TRUE) {
-                  $entity_ids[] = $field['id'];
+                  $field_ids[] = $field['id'];
                 }
                 // If the field is not auto attached then we want to add an
                 // empty default valuea and add an 'unattached' key to
                 // clue in other things that there may be data, it's just
                 // not attached.
                 else {
-//                   $value = array(
-//                     'und' => array(
-//                       0 => array(
-//                         'value' => '',
-//                         '#unattached' => TRUE,
-//                       ),
-//                     ),
-//                   );
-//                   $entity->$field_name = $value;
+                   $value = array(
+                     'und' => array(
+                       0 => array(
+                         'value' => '',
+                         '#unattached' => TRUE,
+                       ),
+                     ),
+                   );
+                   $entity->$field_name = $value;
                 }
               }
             }
           }
         }
 
-        // Iterate through each field that should be added and attach it.
-        foreach ($entity_ids as $field_id) {
-          $options = array();
-          $options['field_id'] = $field_id;
-          $function($this->entityType, array($entity->id => $entity), FIELD_LOAD_CURRENT, $options);
-        }
+        $options = array();
+        $options['field_id'] = $field_ids;
+        $function($this->entityType, array($entity->id => $entity), FIELD_LOAD_CURRENT, $options);
       }
     }
 

+ 0 - 12
tripal/includes/TripalField.inc

@@ -99,18 +99,6 @@ class TripalField {
   public function __construct($field, $instance = NULL) {
     $this->field = $field;
     $this->instance = $instance;
-
-    // Include any instances that have been created for this field.
-    if (is_array($this->field) and array_key_exists('id', $this->field)) {
-      $instances = db_select('field_config_instance', 'fci')
-        ->fields('fci')
-        ->condition('field_id', $this->field['id'])
-        ->execute();
-
-      while ($instance = $instances->fetchObject()) {
-        $this->instances[$instance->field_name] = $instance;
-      }
-    }
   }
 
   // --------------------------------------------------------------------------

+ 1 - 0
tripal_chado/includes/TripalFields/chado_linker__pub.inc

@@ -244,6 +244,7 @@ class chado_linker__pub extends TripalField {
     $field_type = $this->field['type'];
     $field_table = $this->field['settings']['chado_table'];
     $field_column = $this->field['settings']['chado_column'];
+    $base_table = $this->field['settings']['base_table'];
 
     // Get the FK that links to the base record.
     $schema = chado_get_schema($field_table);

+ 107 - 106
tripal_chado/includes/TripalFields/chado_linker__relationship.inc

@@ -118,24 +118,25 @@ class chado_linker__relationship extends TripalField {
    * @see TripalField::widgetForm()
    */
   public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
-    
+
     $field_name = $this->field['field_name'];
     $field_type = $this->field['type'];
     $field_table = $this->field['settings']['chado_table'];
     $field_column = $this->field['settings']['chado_column'];
-    
+
     // Get the instance settings
-    $instance_default = $this->instance['default_value'] ? TRUE : FALSE;
-    $instances = $this->instances;
-    $instance = $instances[$field_name];
-    $data = unserialize($instance->data);
-    $settings = $data['settings']['relationships'];
+    $instance = $this->instance;
+    $settings = $instance['settings']['relationships'];
+
     $option1_vocabs = $settings['option1_vocabs'];
-    $option1_test = $option1_vocabs;
-    $option2_vocab = $settings['option2_vocab'];
+    $option2_vocab  = $settings['option2_vocab'];
     $option2_parent = $settings['option2_parent'];
     $option3_rtypes = $settings['relationship_types'];
 
+    // For testing if there are selected vocabs for option1 we'll copy the
+    // contents in a special variable for later.
+    $option1_test   = $option1_vocabs;
+
     // Get the FK column that links to the base table.
     $chado_table = $this->field['settings']['chado_table'];
     $base_table = $this->field['settings']['base_table'];
@@ -244,7 +245,8 @@ class chado_linker__relationship extends TripalField {
     // Option 3: Custom list of Relationship Types
     $default_term = '';
     $rtype_options = array();
-    if ($option3_rtypes && !$instance_default) {
+    $rtype_options[] = 'Select a Type';
+    if ($option3_rtypes) {
       $rtypes = explode(PHP_EOL, $option3_rtypes);
       foreach($rtypes AS $type) {
         // Ignore empty lines
@@ -267,30 +269,30 @@ class chado_linker__relationship extends TripalField {
         '#options' => $rtype_options,
         '#default_value' => $default_term,
       );
-    } 
+    }
     // Option 2: Child terms of a selected cvterm
-    else if ($option2_vocab && !$instance_default) {
-      $parent_term = tripal_get_cvterm(
-        array(
-          'cv_id' => $option2_vocab,
-          'name' => $option2_parent
-        )
+    else if ($option2_vocab) {
+      $values = array(
+        'cv_id' => $option2_vocab,
+        'name' => $option2_parent
       );
-      //Try getting the term by using the synonym
+      $parent_term = tripal_get_cvterm($values);
+
+      // If the term wasn't found then see if it's a synonym.
       if(!$parent_term) {
-        $synonym = tripal_get_cvterm(
-            array(
-              'synonym' => array(
-                'name' => trim($option2_parent),
-              )
-            )
-            );
+        $values = array(
+          'synonym' => array(
+            'name' => trim($option2_parent),
+          )
+        );
+        $synonym = tripal_get_cvterm($values);
         if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
           $parent_term = $synonym;
         }
       }
-      $sql = 
-        "SELECT 
+      // Get the child terms of the parent term found above.
+      $sql =
+        "SELECT
            subject_id,
            (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
          FROM {cvtermpath}
@@ -298,15 +300,13 @@ class chado_linker__relationship extends TripalField {
            object_id = :parent_cvterm_id
          AND
            cv_id = :parent_cv_id
+         ORDER BY name
        ";
-      $results = 
-        chado_query(
-          $sql, 
-          array(
-            ':parent_cvterm_id' => $parent_term->cvterm_id, 
-            ':parent_cv_id' => $parent_term->cv_id->cv_id
-          )
-        );
+      $args = array(
+        ':parent_cvterm_id' => $parent_term->cvterm_id,
+        ':parent_cv_id' => $parent_term->cv_id->cv_id
+      );
+      $results = chado_query($sql, $args);
       while($child = $results->fetchObject()) {
         $rtype_options[$child->subject_id] = $child->name;
       }
@@ -318,8 +318,8 @@ class chado_linker__relationship extends TripalField {
       );
     }
     // Option 1: All terms of selected vocabularies
-    else if (array_pop($option1_test) && !$instance_default) {
-      $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id)";
+    else if (array_pop($option1_test)) {
+      $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
       $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
       while ($obj = $results->fetchObject()) {
         $rtype_options[$obj->cvterm_id] = $obj->name;
@@ -335,10 +335,7 @@ class chado_linker__relationship extends TripalField {
     else {
       // Set up available cvterms for selection
       $vocs = array(0 => 'Select a vocabulary');
-      
-      if (!$instance_default) {
-        $vocs = tripal_get_cv_select_options();
-      }
+      $vocs = tripal_get_cv_select_options();
       $cv_id = isset($form_state['values'][$field_name]['und'][0]['vocabulary']) ? $form_state['values'][$field_name]['und'][0]['vocabulary'] : 0;
       // Try getting the cv_id from cvterm for existing records
       if (!$cv_id && $type_id) {
@@ -372,7 +369,7 @@ class chado_linker__relationship extends TripalField {
         );
       }
     }
-    
+
     $widget['object_name'] = array(
       '#type' => 'textfield',
       '#title' => t('Object'),
@@ -419,13 +416,13 @@ class chado_linker__relationship extends TripalField {
           'message' => t("Please provide the type of relationship."),
         );
       }
-      if (!$subject_name) {
+      if ($entity and !$subject_name) {
         $errors[$this->field['field_name']][$langcode][$delta][] = array(
           'error' => 'chado_linker__relationship',
           'message' => t("Please provide the subject of the relationship."),
         );
       }
-      if (!$object_name) {
+      if ($entity and !$object_name) {
         $errors[$this->field['field_name']][$langcode][$delta][] = array(
           'error' => 'chado_linker__relationship',
           'message' => t("Please provide the object of the relationship."),
@@ -442,31 +439,33 @@ class chado_linker__relationship extends TripalField {
       $subject_id = '';
       $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
       $matches = array();
-      if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
-        $subject_id =  $matches[1];
-        $values = array($fkey_rcolumn => $subject_id);
-        $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
-        if (count($subject) == 0) {
-          $errors[$this->field['field_name']][$langcode][$delta][] = array(
-            'error' => 'chado_linker__relationship',
-            'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
-          );
-        }
-      }
-      else {
-        $values = array('uniquename' => $subject_name);
-        $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
-        if (count($subject) == 0) {
-          $errors[$this->field['field_name']][$langcode][$delta][] = array(
-            'error' => 'chado_linker__relationship',
-            'message' => t("The subject record cannot be found. Please check spelling."),
-          );
+      if ($entity) {
+        if(preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
+          $subject_id =  $matches[1];
+          $values = array($fkey_rcolumn => $subject_id);
+          $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
+          if (count($subject) == 0) {
+            $errors[$this->field['field_name']][$langcode][$delta][] = array(
+              'error' => 'chado_linker__relationship',
+              'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
+            );
+          }
         }
-        elseif (count($subject) > 1) {
-          $errors[$this->field['field_name']][$langcode][$delta][] = array(
-            'error' => 'chado_linker__relationship',
-            'message' => t("The subject is not unique and therefore the relationship cannot be made."),
-          );
+        else {
+          $values = array('uniquename' => $subject_name);
+          $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
+          if (count($subject) == 0) {
+            $errors[$this->field['field_name']][$langcode][$delta][] = array(
+              'error' => 'chado_linker__relationship',
+              'message' => t("The subject record cannot be found. Please check spelling."),
+            );
+          }
+          elseif (count($subject) > 1) {
+            $errors[$this->field['field_name']][$langcode][$delta][] = array(
+              'error' => 'chado_linker__relationship',
+              'message' => t("The subject is not unique and therefore the relationship cannot be made."),
+            );
+          }
         }
       }
 
@@ -474,50 +473,54 @@ class chado_linker__relationship extends TripalField {
       $object_id = '';
       $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
       $matches = array();
-      if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
-        $object_id = $matches[1];
-        $values = array($fkey_rcolumn => $object_id);
-        $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
-        if (count($subject) == 0) {
-          $errors[$this->field['field_name']][$langcode][$delta][] = array(
-            'error' => 'chado_linker__relationship',
-            'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
-          );
+      if ($entity) {
+        if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
+          $object_id = $matches[1];
+          $values = array($fkey_rcolumn => $object_id);
+          $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
+          if (count($subject) == 0) {
+            $errors[$this->field['field_name']][$langcode][$delta][] = array(
+              'error' => 'chado_linker__relationship',
+              'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
+            );
+          }
+        }
+        else {
+          $values = array('uniquename' => $object_name);
+          $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
+          if (count($object) == 0) {
+            $errors[$this->field['field_name']][$langcode][$delta][] = array(
+              'error' => 'chado_linker__relationship',
+              'message' => t("The object record cannot be found. Please check spelling."),
+            );;
+          }
+          elseif (count($object) > 1) {
+            $errors[$this->field['field_name']][$langcode][$delta][] = array(
+              'error' => 'chado_linker__relationship',
+              'message' =>  t("The object is not unique and therefore the relationship cannot be made."),
+            );
+          }
         }
       }
-      else {
-        $values = array('uniquename' => $object_name);
-        $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
-        if (count($object) == 0) {
+
+      // Make sure that either our object or our subject refers to the base record.
+      if ($entity) {
+        $chado_record_id = $entity->chado_record_id;
+        if ($object_id != $chado_record_id  and $subject_id != $chado_record_id) {
           $errors[$this->field['field_name']][$langcode][$delta][] = array(
             'error' => 'chado_linker__relationship',
-            'message' => t("The object record cannot be found. Please check spelling."),
-          );;
+            'message' =>  t("Either the subject or the object in the relationship must refer to this record."),
+          );
         }
-        elseif (count($object) > 1) {
+
+        // Make sure that the object and subject are not both the same thing.
+        if ($object_id == $subject_id) {
           $errors[$this->field['field_name']][$langcode][$delta][] = array(
             'error' => 'chado_linker__relationship',
-            'message' =>  t("The object is not unique and therefore the relationship cannot be made."),
+            'message' =>  t("The subject and the object in the relationship cannot both refer to the same record."),
           );
         }
       }
-
-      // Make sure that either our object or our subject refers to the base record.
-      $chado_record_id = $entity->chado_record_id;
-      if ($object_id != $chado_record_id  and $subject_id != $chado_record_id) {
-        $errors[$this->field['field_name']][$langcode][$delta][] = array(
-          'error' => 'chado_linker__relationship',
-          'message' =>  t("Either the subject or the object in the relationship must refer to this record."),
-        );
-      }
-
-      // Make sure that the object and subject are not both the same thing.
-      if ($object_id == $subject_id) {
-        $errors[$this->field['field_name']][$langcode][$delta][] = array(
-          'error' => 'chado_linker__relationship',
-          'message' =>  t("The subject and the object in the relationship cannot both refer to the same record."),
-        );
-      }
     }
   }
   /**
@@ -586,7 +589,6 @@ class chado_linker__relationship extends TripalField {
    * @see TripalField::load()
    */
   public function load($entity, $details = array()) {
-
     $settings = $this->field['settings'];
 
     $record = $details['record'];
@@ -626,7 +628,6 @@ class chado_linker__relationship extends TripalField {
     $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
 
     // Set some defaults for the empty record.
-    // TODO: don't hardcode the uniquename as all tables won't have that.
     $entity->{$field_name}['und'][0] = array(
       'value' => array(),
       $field_table . '__' . $pkey => '',
@@ -969,7 +970,7 @@ class chado_linker__relationship extends TripalField {
       );
       return;
     }
-     
+
     // For option3, make sure the supplied types are valid cvterms
     if ($option3) {
       $rel_types = explode(PHP_EOL, $settings['relationship_types']);
@@ -1042,7 +1043,7 @@ class chado_linker__relationship extends TripalField {
         }
       }
     }
-    
+
     // For option2: Make sure the parent term is a valid cvterm
     if ($option2) {
       $cv_id = $settings['option2_vocab'];

+ 2 - 10
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -188,18 +188,11 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
   $langcode = $language->language;
 
   foreach ($entities as $id => $entity) {
-    // The chado_xxxx properties are added to the entity in the
-    // tripal_chado_entity_load() hook which unfortunately is called after
-    // attaching fields.  So, we may not have these properties. If that's
-    // the case we can get them by querying the chado_entity table directly.
-    // The chado_xxxx properites will be here in the case of syncing
-    // records.
-    // TODO: perhaps we should add a hook_entity_preattach() hook?
     if (property_exists($entity, 'chado_table')) {
       // Get the base table and record id for the fields of this entity.
       $base_table = $entity->chado_table;
       $type_field = $entity->chado_column;
-      $record_id = $entity->chado_record_id;
+      $record_id  = $entity->chado_record_id;
     }
     else {
       // Get the base table and record id for the fields of this entity.
@@ -214,7 +207,7 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
       // Get some values needed for loading the values from Chado.
       $base_table = isset($details->data_table) ? $details->data_table : '';
       $type_field = isset($details->field) ? $details->field : '';
-      $record_id = isset($details->record_id) ? $details->record_id : '';
+      $record_id  = isset($details->record_id) ? $details->record_id : '';
     }
 
     // Get this table's schema.
@@ -242,7 +235,6 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
     // that need to be selected from each of the tables represented.
     $tables = array();
     foreach ($fields as $field_id => $ids) {
-
       // By the time this hook runs, the relevant field definitions have been
       // populated and cached in FieldInfo, so calling field_info_field_by_id()
       // on each field individually is more efficient than loading all fields in