Browse Source

Experimenting with reworking the TripalField class

Stephen Ficklin 8 years ago
parent
commit
265db1e724

+ 1 - 0
tripal/api/tripal.fields.api.inc

@@ -1,5 +1,6 @@
 <?php
 
+
 /**
  * Executes a TripalFieldQuery using the provided conditions.
  *

+ 1 - 3
tripal/includes/TripalEntityController.inc

@@ -435,7 +435,7 @@ class TripalEntityController extends EntityAPIController {
       $entities += $queried_entities;
     }
 
-    // Entitycache module support: Add entities to the entity cache if we are
+    // Entity cache module support: Add entities to the entity cache if we are
     // not loading a revision.
     if (!empty($this->entityInfo['entity cache']) && !empty($queried_entities) && !$revision_id) {
       EntityCacheControllerHelper::entityCacheSet($this, $queried_entities);
@@ -470,8 +470,6 @@ class TripalEntityController extends EntityAPIController {
    * slow down the loading of pages and web services.  Therefore, we only
    * want to attach fields that are needed.
    *
-   *
-   *
    * @param $queried_entities
    *   The list of queried
    * @param $revision_id

+ 2 - 2
tripal/includes/TripalField.inc

@@ -387,8 +387,8 @@ class TripalField {
   /**
    *  Perform validation of the field regardless how it is updated.
    *
-   *  Any errors encountered should be indicatd by adding a value to the $errors
-   *  array according to the instructions below.
+   *  Any errors encountered should be indicated by adding a value to the
+   *  $errors array according to the instructions below.
    *
    *  @param $entity_type
    *    The type of $entity.

+ 14 - 2
tripal/includes/TripalTerm.inc

@@ -45,9 +45,21 @@ class TripalTerm extends Entity {
     return array('path' => '/vocabulary/' . $vocab . '/term/' . $this->id);
   }
 
+  // Getters //
+  public function getName() {
+    return $this->name;
+  }
+  public function getAccession() {
+    return $this->name;
+  }
   public function getDefinition() {
-
-    return '';
+    return $this->definition;
+  }
+  public function getURL() {
+    return $this->url;
+  }
+  public function getVocab() {
+    return $this->vocab;
   }
 
 }

+ 215 - 103
tripal/includes/tripal.fields.inc

@@ -11,38 +11,87 @@
  */
 function tripal_field_info() {
 
-  $info = array();
-  $field_types = tripal_get_field_types();
-  foreach ($field_types as $field_type) {
-    $info[$field_type] = $field_type::globalInfo();
+//   $info = array();
+//   $field_types = tripal_get_field_types();
+//   foreach ($field_types as $field_type) {
+//     $info[$field_type] = $field_type::globalInfo();
+//   }
+//   return $info;
+
+  return array(
+    'tripal_key_value' => array(
+      'label' => 'Tripal complex field',
+      'description' => 'A field specific to data managed by Tripal. ',
+      'settings' => array(
+        'tripal_term' => '',
+      ),
+      'instance_settings' => array(),
+      'default_widget' => 'tripal_generic_key_value_widget',
+      'default_formatter' => 'tripal_generic_key_value_formatter',
+      'storage' => array(
+        'type' => 'tripal_no_storage',
+        'module' => 'tripal',
+        'active' => TRUE
+      ),
+    ),
+  );
+}
+
+/**
+ * Implements hook_info_alter().
+ */
+function tripal_field_info_alter(&$info) {
+  // Make sure all fields have a 'tripal_term' setting so we can map
+  // all fields to a vocabulary term for the semantic web.
+  foreach ($info as $field_name => $details) {
+    if(array_key_exists('settings', $details)) {
+      if (!array_key_exists('tripal_term', $details['settings'])) {
+        $info[$field_name]['settings']['tripal_term'] = '';
+      }
+    }
+    else {
+      $info[$field_name]['settings']['tripal_term'] = '';
+    }
   }
-  return $info;
 }
 
 /**
  * Implements hook_field_widget_info();
  */
 function tripal_field_widget_info() {
-  $info = array();
-
-  $field_types = tripal_get_field_types();
-  foreach ($field_types as $field_type) {
-    $info += $field_type::widgetInfo();
-  }
-  return $info;
+//   $info = array();
+
+//   $field_types = tripal_get_field_types();
+//   foreach ($field_types as $field_type) {
+//     $info += $field_type::widgetInfo();
+//   }
+//   return $info;
+  return array(
+    'tripal_generic_key_value_widget' => array(
+      'label' => 'Generic key/value',
+      'field types' => array('tripal_key_value')
+    ),
+  );
 }
 
 /**
  * Implements hook_field_formatter_info().
  */
 function tripal_field_formatter_info() {
-  $info = array();
-
-  $field_types = tripal_get_field_types();
-  foreach ($field_types as $field_type) {
-    $info += $field_type::formatterInfo();
-  }
-  return $info;
+//   $info = array();
+
+//   $field_types = tripal_get_field_types();
+//   foreach ($field_types as $field_type) {
+//     $info += $field_type::formatterInfo();
+//   }
+//   return $info;
+  return array(
+    'tripal_generic_key_value_formatter' => array(
+      'label' => 'Values',
+      'field types' => array('tripal_key_value'),
+      'settings' => array(),
+    ),
+  );
 }
 
 /**
@@ -106,14 +155,14 @@ function tripal_create_tripalfield_instance($entity_type, $bundle) {
 function tripal_field_formatter_view($entity_type, $entity, $field,
     $instance, $langcode, $items, $display) {
 
-  $element = array();
-  $field_type = $field['type'];
-  $is_loaded = tripal_load_include_field_type($field_type);
-  if ($is_loaded) {
-    $tfield = new $field_type($field, $instance);
-    $tfield->formatterView($element, $entity_type, $entity, $langcode, $items, $display);
-  }
-  return $element;
+//   $element = array();
+//   $field_type = $field['type'];
+//   $is_loaded = tripal_load_include_field_type($field_type);
+//   if ($is_loaded) {
+//     $tfield = new $field_type($field, $instance);
+//     $tfield->formatterView($element, $entity_type, $entity, $langcode, $items, $display);
+//   }
+//   return $element;
 }
 
 /**
@@ -140,21 +189,41 @@ function tripal_field_no_delete() {
  * the table to let the user know where fields are storing their data.
  */
 function tripal_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
+  //dpm($form);
+
   // Add the 'Storage Location' to the table header.
+  $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);
     // For rows in the tables that aren't fields, just add an empty value
     // for the storage column.
     if (!$field) {
+      $form['fields'][$field_name][] = array(
+        '#markup' => '',
+      );
       $form['fields'][$field_name][] = array(
         '#markup' => '',
       );
       continue;
     }
+    $term = $field['settings']['tripal_term'] ? $field['settings']['tripal_term'] : 'N/A';
+    $form['fields'][$field_name][] = array(
+      '#markup' => $term,
+    );
+
     $storage_type = $field['storage']['type'];
     $storage_label = array_key_exists('label', $storage_info[$storage_type]) ? $storage_info[$storage_type]['label'] : '';
     if ($storage_type == 'field_sql_storage') {
@@ -181,65 +250,108 @@ function tripal_form_field_ui_field_overview_form_alter(&$form, &$form_state, $f
   );
 }
 
+/**
+ * Implements hook_module_implements_alter()
+ *
+ * We want our edits to the field_ui_field_overview_form form to occur after
+ * all modules have implemented their changes.
+ */
+function tripal_module_implements_alter(&$implementations, $hook) {
+  if ($hook == 'form_alter') {
+    $group = $implementations['tripal'];
+    unset($implementations['tripal']);
+    $implementations['tripal'] = $group;
+  }
+}
 
 
 /**
  * Implements hook_field_settings_form()
  */
 function tripal_field_settings_form($field, $instance, $has_data) {
-  $form = array();
-  $field_type = $field['type'];
-  //$is_loaded = tripal_load_include_field_type($field_type);
-  tripal_load_include_field_type($field_type);
-  if (class_exists($field_type)) {
-    $tfield = new $field_type($field, $instance);
-    $form = $tfield->globalSettingsForm($field, $instance, $has_data);
-  }
-  return $form;
+//   $form = array();
+//   $field_type = $field['type'];
+//   //$is_loaded = tripal_load_include_field_type($field_type);
+//   tripal_load_include_field_type($field_type);
+//   if (class_exists($field_type)) {
+//     $tfield = new $field_type($field, $instance);
+//     $form = $tfield->globalSettingsForm($field, $instance, $has_data);
+//   }
+//   return $form;
+}
+
+/**
+ * Allows for altering of a field's instance setting form.
+ *
+ * This appears to be a Drupal hook but is actually a custom function created
+ * by this module. It is called by the tripal_form_alter() function of this
+ * module.
+ *
+ * @param $form
+ *   The form array.  Alterations to the form can be made within this array.
+ * @param $form_state
+ *   The form state array.
+ */
+function tripal_field_instance_settings_form_alter(&$form, $form_state) {
+  $field = $form['#field'];
+  $instance = $form['#instance'];
+
+  $form['tripal_additions'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Tripal Settings',
+  );
+  $form['tripal_additions']['semantic_web'] = array(
+    '#type' => 'textfield',
+    '#title' => 'Vocabulary Term'
+  );
+  $form['tripal_additions']['storage'] = array(
+    '#type' => 'textfield',
+    '#title' => 'Storage Backend'
+  );
 }
 /**
  * Implements hook_instance_settings_form()
  */
 function tripal_field_instance_settings_form($field, $instance) {
-  $form = array();
-  $field_type = $field['type'];
-  tripal_load_include_field_type($field_type);
-  if (class_exists($field_type)) {
-    $tfield = new $field_type($field, $instance);
-    $form = $tfield->instanceSettingsForm();
-  }
-  return $form;
+//   $form = array();
+//   $field_type = $field['type'];
+//   tripal_load_include_field_type($field_type);
+//   if (class_exists($field_type)) {
+//     $tfield = new $field_type($field, $instance);
+//     $form = $tfield->instanceSettingsForm();
+//   }
+//   return $form;
 }
 /**
  *
  */
 function tripal_field_instance_settings_form_validate($form, &$form_state) {
-  $field = $form['#field'];
-  $instance = $form['#instance'];
-  $field_type = $field['type'];
-  tripal_load_include_field_type($field_type);
-  if (class_exists($field_type)) {
-    $tfield = new $field_type($field, $instance);
-    $form = $tfield->instanceSettingsFormValidate($form, $form_state);
-  }
+//   $field = $form['#field'];
+//   $instance = $form['#instance'];
+//   $field_type = $field['type'];
+//   tripal_load_include_field_type($field_type);
+//   if (class_exists($field_type)) {
+//     $tfield = new $field_type($field, $instance);
+//     $form = $tfield->instanceSettingsFormValidate($form, $form_state);
+//   }
 }
 
 /**
  *
  */
 function tripal_field_widget_form_validate($form, &$form_state) {
-  $entity = $form['#entity'];
-  $entity_type = $form['#entity_type'];
-  $langcode = $form['#language'];
-  $delta = $form['#delta'];
-  $field = $form['#field'];
-  $field_type = $field['type'];
-  tripal_load_include_field_type($field_type);
-  if (class_exists($field_type)) {
-    $instance = $form['#instance'];
-    $tfield = new $field_type($field, $instance);
-    $form = $tfield->widgetFormValidate($form, $form_state, $entity_type, $entity, $langcode, $delta);
-  }
+//   $entity = $form['#entity'];
+//   $entity_type = $form['#entity_type'];
+//   $langcode = $form['#language'];
+//   $delta = $form['#delta'];
+//   $field = $form['#field'];
+//   $field_type = $field['type'];
+//   tripal_load_include_field_type($field_type);
+//   if (class_exists($field_type)) {
+//     $instance = $form['#instance'];
+//     $tfield = new $field_type($field, $instance);
+//     $form = $tfield->widgetFormValidate($form, $form_state, $entity_type, $entity, $langcode, $delta);
+//   }
 }
 
 
@@ -251,14 +363,14 @@ function tripal_field_widget_form_validate($form, &$form_state) {
  * member function.
  */
 function tripal_field_settings_form_validate($form, &$form_state) {
-  $field = $form['#field'];
-  $instance = $form['#instance'];
-  $field_type = $field['type'];
-  tripal_load_include_field_type($field_type);
-  if (class_exists($field_type)) {
-    $tfield = new $field_type($field, $instance);
-    $form = $tfield->globalSettingsFormValidate($field, $instance, $form, $form_state);
-  }
+//   $field = $form['#field'];
+//   $instance = $form['#instance'];
+//   $field_type = $field['type'];
+//   tripal_load_include_field_type($field_type);
+//   if (class_exists($field_type)) {
+//     $tfield = new $field_type($field, $instance);
+//     $form = $tfield->globalSettingsFormValidate($field, $instance, $form, $form_state);
+//   }
 }
 
 
@@ -267,14 +379,14 @@ function tripal_field_settings_form_validate($form, &$form_state) {
  */
 function tripal_field_formatter_settings_summary($field, $instance, $view_mode) {
 
-  $summary = '';
-  $field_type = $field['type'];
-  tripal_load_include_field_type($field_type);
-  if (class_exists($field_type)) {
-    $tfield = new $field_type($field, $instance);
-    $form = $tfield->formatterSettingsSummary($view_mode);
-  }
-  return $summary;
+//   $summary = '';
+//   $field_type = $field['type'];
+//   tripal_load_include_field_type($field_type);
+//   if (class_exists($field_type)) {
+//     $tfield = new $field_type($field, $instance);
+//     $form = $tfield->formatterSettingsSummary($view_mode);
+//   }
+//   return $summary;
 }
 
 /**
@@ -283,14 +395,14 @@ function tripal_field_formatter_settings_summary($field, $instance, $view_mode)
 function tripal_formatter_settings_form($field, $instance,
     $view_mode, $form, &$form_state) {
 
-  $form = array();
-  $field_type = $field['type'];
-  tripal_load_include_field_type($field_type);
-  if (class_exists($field_type)) {
-    $tfield = new $field_type($field, $instance);
-    $form = $tfield->formatterSettingsForm($view_mode, $form, $form_state);
-  }
-  return $form;
+//   $form = array();
+//   $field_type = $field['type'];
+//   tripal_load_include_field_type($field_type);
+//   if (class_exists($field_type)) {
+//     $tfield = new $field_type($field, $instance);
+//     $form = $tfield->formatterSettingsForm($view_mode, $form, $form_state);
+//   }
+//   return $form;
 }
 
 
@@ -299,14 +411,14 @@ function tripal_formatter_settings_form($field, $instance,
  */
 function tripal_field_widget_form(&$form, &$form_state, $field,
     $instance, $langcode, $items, $delta, $element) {
-  $widget = $element;
-  $field_type = $field['type'];
-  tripal_load_include_field_type($field_type);
-  if (class_exists($field_type)) {
-    $tfield = new $field_type($field, $instance);
-    $tfield->widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
-  }
-  return $widget;
+//   $widget = $element;
+//   $field_type = $field['type'];
+//   tripal_load_include_field_type($field_type);
+//   if (class_exists($field_type)) {
+//     $tfield = new $field_type($field, $instance);
+//     $tfield->widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
+//   }
+//   return $widget;
 
 
 }
@@ -353,13 +465,13 @@ function tripal_field_widget_form_alter(&$element, &$form_state, $context) {
 function tripal_field_validate($entity_type, $entity, $field, $instance,
     $langcode, $items, &$errors) {
 
-  $field_type = $field['type'];
-  $is_loaded = tripal_load_include_field_type($field_type);
-  if ($is_loaded) {
-    $tfield = new $field_type($field, $instance);
-    $tfield->validate($entity_type, $entity, $langcode,
-        $items, $errors);
-  }
+//   $field_type = $field['type'];
+//   $is_loaded = tripal_load_include_field_type($field_type);
+//   if ($is_loaded) {
+//     $tfield = new $field_type($field, $instance);
+//     $tfield->validate($entity_type, $entity, $langcode,
+//         $items, $errors);
+//   }
 }
 
 /**

+ 12 - 0
tripal/tripal.module

@@ -561,3 +561,15 @@ function tripal_import_api() {
   module_load_include('inc', 'tripal', 'api/tripal.notice.api');
   module_load_include('inc', 'tripal', 'api/tripal.variables.api');
 }
+
+/**
+ * Implements hook_form_alter().
+ */
+function tripal_form_alter(&$form, $form_state, $form_id) {
+  // If this i the field_ui_field_edit_form (i.e. the form that appears
+  // when editing a field that is attached to an entity). Then we want
+  // to add semantic web settings.
+  if ($form_id == 'field_ui_field_edit_form') {
+    tripal_field_instance_settings_form_alter($form, $form_state);
+  }
+}

+ 1 - 2
tripal_chado/includes/TripalFields/chado_base__organism_id.inc

@@ -120,7 +120,6 @@ class chado_base__organism_id extends TripalField {
     // Set some defaults for the empty record.
     $entity->{$field_name}['und'][0] = array(
       'value' => array(
-        'chado-' . $field_table . '__organism_id' => '',
         'label' => '',
         'genus' => '',
         'species' => '',
@@ -168,7 +167,7 @@ class chado_base__organism_id extends TripalField {
   }
 
   /**
-   * @see TripalField::settings_form()
+   * @see TripalField::globalSettingsForm()
    */
   public function globalSettingsForm($has_data) {
     $element = parent::globalSettingsForm($has_data);

+ 48 - 3
tripal_chado/includes/tripal_chado.fields.inc

@@ -1,5 +1,50 @@
 <?php
 
+function tripal_chado_field_widget_info() {
+  return array(
+    'chado_base_dbxref_id' => array(
+      'label' => 'Site specific Accession',
+      'field types' => array('tripal_key_value'),
+    ),
+    'chado_base_organism_id' => array(
+      'label' => 'Organism',
+      'field types' => array('tripal_key_value')
+    ),
+    'chado_feature_md5checsum' => array(
+      'label' => 'Sequence Checksum',
+      'field types' => array('tripal_key_value')
+    ),
+    'chado_feature_residues' => array(
+      'label' => 'Sequence',
+      'field types' => array('tripal_key_value')
+    ),
+    'chado_feature_seqlen' => array(
+      'label' => 'Sequence Length',
+      'field types' => array('tripal_key_value')
+    ),
+    'chado_gene_transcripts' => array(
+      'label' => 'Transcripts',
+      'field types' => array('tripal_key_value')
+    ),
+    'chado_linker_contact' => array(
+      'label' => 'Contacts',
+      'field types' => array('tripal_key_value')
+    ),
+    'chado_linker_dbxref' => array(
+      'label' => 'Cross References',
+      'field types' => array('tripal_key_value')
+    ),
+    'chado_linker_expression' => array(
+      'label' => 'Expression',
+      'field types' => array('tripal_key_value')
+    ),
+    'chado_linker_featureloc' => array(
+      'label' => 'Positions',
+      'field types' => array('tripal_key_value')
+    ),
+  );
+}
+
 /**
  * Implements hook_field_create_info().
  *
@@ -448,7 +493,7 @@ function tripal_chado_create_tripalfields_linker(&$info, $details, $entity_type,
       ),
     );
   }
-  
+
   // FEATUREPOS
   if ($table_name == 'feature') {
     $schema = chado_get_schema('featurepos');
@@ -1220,7 +1265,7 @@ function tripal_chado_create_tripalfield_instance_linker(&$info, $entity_type, $
       ),
     );
   }
-  
+
   // FEATUREPOS
   if ($table_name == 'feature') {
     $field_name = 'featurepos';
@@ -1249,7 +1294,7 @@ function tripal_chado_create_tripalfield_instance_linker(&$info, $entity_type, $
       ),
     );
   }
-  
+
   // GENOTYPE
   $genotype_table = $table_name . '_genotype';
   if (chado_table_exists($genotype_table)) {