Browse Source

Changed TripalField to always require an instance

Stephen Ficklin 8 years ago
parent
commit
c891051dbb

+ 20 - 12
tripal/includes/TripalFields/TripalField.inc

@@ -76,25 +76,33 @@ class TripalField {
    * Instantiates a new TripalField object.
    *
    * @param $field
-   *   An array containing the field data as returned by field_info_field()
+   *   An array containing the field data as returned by field_info_field().
    * @param $instance
-   *   (Optional). Set the instance of this field when one is available. This
-   *   is necessary when working with instance specific functions such as the
-   *   formatterSettingsForm, widgetForm, etc.
+   *   An array containing the instance data as returned by field_instance_info().
    */
-  public function __construct($field, $instance = NULL) {
+  public function __construct($field, $instance) {
     $this->field = $field;
     $this->instance = $instance;
 
     $class = get_called_class();
 
     // Make sure the term exist.
-    $vocabulary = $instance->settings['term_vocabulary'];
-    $accession = $instance->settings['term_accession'];
-    $term = tripal_get_term_details($vocabulary, $accession);
-    if (!$term) {
-      //throw new Error(t('Cannot create TripalField of type "%term" as that
-      //    term does not exist.', array('%term' => $class::$term)));
+    if (!$instance) {
+      tripal_set_message(t('Missing instance of field "%field"', array('%field' => $field['field_name'])), TRIPAL_ERROR);
+    } 
+    else {
+      if (!array_key_exists('term_vocabulary', $instance['settings'])) {
+        tripal_set_message(t('Missing controlled vocbulary for field "%field"', array('%field' => $field['field_name'])), TRIPAL_ERROR);
+      } 
+      else {
+        $vocabulary = $instance['settings']['term_vocabulary'];
+        $accession = $instance['settings']['term_accession'];
+        $term = tripal_get_term_details($vocabulary, $accession);
+        if (!$term) {
+          //throw new Error(t('Cannot create TripalField of type "%term" as that
+          //    term does not exist.', array('%term' => $class::$term)));
+        }
+      }
     }
   }
 
@@ -313,4 +321,4 @@ class TripalField {
 
   }
 
-}
+}

+ 5 - 4
tripal/includes/tripal.field_storage.inc

@@ -48,14 +48,15 @@ function tripal_field_storage_load($entity_type, $entities, $age,
       $field_type = $field['type'];
       $field_module = $field['module'];
 
+      // Get the instnace for this field
+      $instance = field_info_instance($entity_type, $field_name, $entity->bundle); 
+
       // Allow the creating module to alter the value if desired.  The
       // module should do this if the field has any other form elements
       // that need populationg besides the value which was set above.
       tripal_load_include_field_class($field_type);
       if (class_exists($field_type)) {
-
-
-        $tfield = new $field_type($field);
+        $tfield = new $field_type($field, $instance);
         $tfield->load($entity);
       }
 
@@ -118,4 +119,4 @@ function tripal_field_storage_query($query) {
     $result['TripalEntity'][$entity->id] = entity_create_stub_entity('TripalEntity', $ids);
   }
   return $result;
-}
+}

+ 4 - 11
tripal/includes/tripal.fields.inc

@@ -82,7 +82,7 @@ function tripal_field_formatter_info_alter(&$info) {
  */
 function tripal_bundle_create($bundle) {
   $field_type = 'rdfs__type';
-  $field_name = $bundle->name . '_' . $field_type;
+  $field_name = 'rdfs__type';
 
   // Add the field, unless it already exists.
   if (!field_info_field($field_name)) {
@@ -94,8 +94,6 @@ function tripal_bundle_create($bundle) {
       'storage' => array(
         'type' => 'tripal_no_storage'
       ),
-      'settings' => array(
-      ),
     ));
   }
 
@@ -111,6 +109,9 @@ function tripal_bundle_create($bundle) {
       'required' => FALSE,
       'settings' => array(
         'auto_attach' => TRUE,
+        'term_vocabulary' => 'rdfs',
+        'term_accession' => 'type',
+        'term_name' => 'type',
       ),
       'widget' => array(
         'type' => 'rdfs__type_widget',
@@ -174,18 +175,10 @@ function tripal_form_field_ui_field_overview_form_alter(&$form, &$form_state, $f
   $form['fields']['#header'][] = 'Term';
   $form['fields']['#header'][] = 'Supported By * ';
 
-  // TODO: remove widgets that aren't appropriate for this entity, if the
-  // type is 'tripal_key_value'.
-  // Why is this sort not working!!??
-  $options = $form['fields']['_add_new_field']['widget_type']['#options']['Tripal complex field'];
-  asort($options);
-  $form['fields']['_add_new_field']['widget_type']['#options']['Tripal complex field'] = $options;
-
   // Add the storage location as the final column for each field.
   $storage_info = module_invoke_all('field_storage_info');
   foreach (element_children($form['fields']) as $field_name) {
 
-
     $field = field_info_field($field_name);
     $instance = field_info_instance('TripalEntity', $field_name, $form['#bundle']);
     // For rows in the tables that aren't fields, just add an empty value

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

@@ -236,6 +236,9 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
       $field_type = $field['type'];
       $field_module = $field['module'];
 
+      // Get the instnace for this field
+      $instance = field_info_instance($entity_type, $field_name, $entity->bundle); 
+
       // Skip fields that don't map to a Chado table (e.g. kvproperty_adder).
       if (!array_key_exists('settings', $field) or !array_key_exists('chado_table', $field['settings'])) {
         continue;
@@ -273,7 +276,7 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
         // that need populationg besides the value which was set above.
         tripal_load_include_field_class($field_type);
         if (class_exists($field_type) and is_subclass_of($field_type, 'TripalField')) {
-          $tfield = new $field_type($field);
+          $tfield = new $field_type($field, $instance);
           $tfield->load($entity, array('record' => $record));
         }
       }
@@ -287,7 +290,7 @@ function tripal_chado_field_storage_load($entity_type, $entities, $age,
         $entity->{$field_name}['und'][0]['value'] = '';
         tripal_load_include_field_class($field_type);
         if (class_exists($field_type) && method_exists($field_type, 'load')) {
-          $tfield = new $field_type($field);
+          $tfield = new $field_type($field, $instance);
           $tfield->load($entity, array('record' => $record));
         }
       }