Browse Source

Reworking TripalField class to be more intuitive

Stephen Ficklin 8 years ago
parent
commit
19ba11e8dc

+ 37 - 57
tripal/includes/TripalField.inc

@@ -44,9 +44,6 @@ class TripalField {
   // of this object come directly from the field_config table of Drupal.
   protected $field;
 
-  // A list of instances of this field. These are attached to bundles.
-  protected $instances;
-
   // --------------------------------------------------------------------------
   //                          STATIC CONSTANTS
   //
@@ -93,8 +90,19 @@ class TripalField {
    *
    * The field must have already been previously created.
    */
-  public function __construct($field) {
-    $this->field = $field;
+  public function __construct($info = array()) {
+    // If the field array has been passed in then just link it.
+    if (array_key_exists('field', $info)) {
+      $this->field = $field;
+    }
+    // If the field name has been passed in then retreive the form info.
+    if (array_key_exists('field_name', $info)) {
+      $this->field = field_info_field($field_name);
+    }
+    // If the field info has been passed in then create the field.
+    if (array_key_exists('info', $info)) {
+      $this->field = field_create_field($info['info']);
+    }
 
     // Include any instances that have been created for this field.
     if (is_array($field) and array_key_exists('id', $this->field)) {
@@ -109,27 +117,6 @@ class TripalField {
     }
   }
 
-  /**
-   * Instantiates a new TripalField from an existing Drupal field by name.
-   * @param $field_name
-   */
-  public static function byName($field_name) {
-    $field = field_info_field($field_name);
-    return new self($field);
-  }
-
-  /**
-   * Creates a new Drupal field and instantiates a new TripalField object
-   *
-   * If the field has been created before and Drupal knows about it then use
-   * the constructor that only requires a field_id.
-   *
-   */
-  public static function byCreate($info = array()) {
-    $this->field = field_create_field($info);
-    return new self($this->field);
-  }
-
   // --------------------------------------------------------------------------
   //                      STATIC INFO FUNCTIONS
   //
@@ -146,7 +133,7 @@ class TripalField {
    *   describing the field type. The keys are the same as for the
    *   hook_field_info() function.
    */
-  public static function fieldInfo() {
+  public static function globalInfo() {
     $field_type = get_called_class();
     return array(
       'label' => self::$default_label,
@@ -222,23 +209,12 @@ class TripalField {
     return $this->field['field_name'];
   }
 
-  /**
-   * Retrieves the type of field for this field.
-   *
-   * @return
-   *   The field's type.
-   */
-  public function getType() {
-    return $this->field['type'];
-  }
-
   // --------------------------------------------------------------------------
   //                     FIELD SPECIFIC FUNCTIONS
   //
   // Child classes SHOULD NOT override these functions as needed.
   // --------------------------------------------------------------------------
 
-
   /**
    * Creates an instance of a field and attaches it to a bundle.
    *
@@ -246,13 +222,13 @@ class TripalField {
    *
    * @param $info
    */
-  public function addInstance($info = array()) {
-    if ($this->canAttach($info['entity_type'], $info['bundle'])) {
+  public function createInstance($info = array()) {
+    //if ($this->canAttach($info['entity_type'], $info['bundle'])) {
       return field_create_instance($info);
-    }
-    else {
-      return FALSE;
-    }
+    //}
+    //else {
+    //  return FALSE;
+    //}
   }
 
   // --------------------------------------------------------------------------
@@ -292,18 +268,18 @@ class TripalField {
    *   TRUE is returned then an instance of the field can be attached to the
    *   bundle.
    */
-  protected function canAttach($entity_type, $bundle_name) {
-    // Don't attach if it's already attached.
-    if (array_key_exists('bundles', $this->field) and
-        array_key_exists('TripalEntity', $this->field['bundles']) and
-        in_array($bundle_name, $this->field['bundles']['TripalEntity'])) {
-      return FALSE;
-    }
+//   protected function canAttach($entity_type, $bundle_name) {
+//     // Don't attach if it's already attached.
+//     if (array_key_exists('bundles', $this->field) and
+//         array_key_exists('TripalEntity', $this->field['bundles']) and
+//         in_array($bundle_name, $this->field['bundles']['TripalEntity'])) {
+//       return FALSE;
+//     }
 
-    // Child classes should check to see if this field can be attached
-    // to the bundle.
-    return NULL;
-  }
+//     // Child classes should check to see if this field can be attached
+//     // to the bundle.
+//     return NULL;
+//   }
 
 
 
@@ -535,6 +511,10 @@ class TripalField {
 
   }
 
+
+  public function instanceLoad($instance) {
+
+  }
   /**
    * Provides a form for the 'Field Settings' of the field management page.
    *
@@ -593,7 +573,7 @@ class TripalField {
    * @param $has_data
    *   TRUE if the field already has data, FALSE if not.
    */
-  public static function fieldSettingsForm($field, $instance, $has_data) {
+  public static function globalSettingsForm($field, $instance, $has_data) {
     $settings = $field['settings'];
     $element = array();
 
@@ -621,7 +601,7 @@ class TripalField {
    * @param unknown $form
    * @param unknown $form_state
    */
-  public static function fieldSettingsFormValidate($field, $instance, $form, &$form_state) {
+  public static function globalSettingsFormValidate($field, $instance, $form, &$form_state) {
 
   }
 

+ 24 - 3
tripal/includes/tripal.fields.inc

@@ -2,18 +2,39 @@
 
 /**
  * Implements hook_field_info().
+ *
+ * We want the Tripal module to handle all TripalFields.  This will allow
+ * other modules to be more easily disabled/enabled because Drupal won't
+ * let a module be disabled if it supports fields that are actively attached
+ * to bundles.  Therefore any module that provides a new TripalField will be
+ * discovered and listed for Drupal by this function.
  */
 function tripal_field_info() {
 
   $info = array();
 
-  $field_types = tripal_get_field_types('tripal');
-  foreach ($field_types as $field_type) {
-    $info[$field_type] = $field_type::fieldInfo();
+  $modules = module_list();
+  foreach ($modules as $module) {
+    $field_types = tripal_get_field_types($module);
+    foreach ($field_types as $field_type) {
+      $info[$field_type] = $field_type::globalInfo();
+    }
   }
   return $info;
 }
 
+/**
+ * Creates TripalField fields.
+ *
+ *
+ */
+function tripal_create_tripal_fields() {
+  $info = tripal_field_info();
+  foreach ($info as $field_type => $info) {
+    $field_info = $field_type::fieldInfo();
+  }
+}
+
 /**
  * Implements hook_field_create_info().
  *

+ 208 - 8
tripal_chado/includes/tripal_chado.fields.inc

@@ -14,16 +14,16 @@
  * the cache must be cleared.
  *
  */
-function tripal_chado_field_info() {
-  $info = array();
+//function tripal_chado_field_info() {
+//   $info = array();
 
-  $field_types = tripal_get_field_types('tripal_chado');
-  foreach ($field_types as $field_type) {
-    $info[$field_type] = $field_type::fieldDefaults();
-  }
-  return $info;
+//   $field_types = tripal_get_field_types('tripal_chado');
+//   foreach ($field_types as $field_type) {
+//     $info[$field_type] = $field_type::fieldDefaults();
+//   }
+//   return $info;
 
-}
+//}
 
 /**
  * Implements hook_field_create_info().
@@ -56,6 +56,206 @@ function tripal_chado_field_create_info($entity_type, $bundle) {
   return array_merge($base_fields, $custom_fields);
 }
 
+
+function tripal_chado_field_create_linkers($entity_type, $bundle, $details) {
+  $table_name = $details['chado_table'];
+  $type_table = $details['chado_type_table'];
+  $type_field = $details['chado_type_column'];
+  $cv_id      = $details['chado_cv_id'];
+  $cvterm_id  = $details['chado_cvterm_id'];
+
+  // CONTACTS
+  $contact_table = $table_name . '_contact';
+  if (chado_table_exists($contact_table)) {
+    $schema = chado_get_schema($contact_table);
+    $pkey = $schema['primary key'][0];
+    $field_name = $table_name . '_contact';
+    return array(
+      'field_name' => $field_name,
+      'type' => 'chado_linker__contact',
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'field_chado_storage',
+      ),
+      'settings' => array(
+        'chado_table' => $contact_table,
+        'chado_column' => 'contact_id',
+        'base_table' => $table_name,
+        'semantic_web' => 'local:contact'
+      ),
+    );
+  }
+
+  // CVTERM
+
+  // DBXREF
+  $dbxref_table = $table_name . '_dbxref';
+  if (chado_table_exists($dbxref_table)) {
+    $dbxref_table = $table_name . '_dbxref';
+    $schema = chado_get_schema($dbxref_table);
+    $pkey = $schema['primary key'][0];
+    $field_name = $table_name . '_dbxref';
+    return array(
+      'field_name' =>  $field_name,
+      'type' => 'chado_linker__dbxref',
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'field_chado_storage',
+      ),
+      'settings' => array(
+        'chado_table' => $dbxref_table,
+        'chado_column' => $pkey,
+        'base_table' => $table_name,
+        'semantic_web' => 'SBO:0000554',
+      ),
+    );
+  }
+  // EXPRESSION
+  $expression_table = $table_name . '_expression';
+  if (chado_table_exists($expression_table)) {
+    $schema = chado_get_schema($expression_table);
+    $pkey = $schema['primary key'][0];
+    $field_name = $table_name . '_expression';
+    return array(
+      'field_name' => $field_name,
+      'type' => 'chado_linker__expression',
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'field_chado_storage',
+      ),
+      'settings' => array(
+        'chado_table' => $expression_table,
+        'chado_column' => $pkey,
+        'base_table' => $table_name,
+        'semantic_web' => 'local:expression',
+      ),
+    );
+  }
+
+  // FEATURELOC
+  if ($table_name == 'feature') {
+    $schema = chado_get_schema('featureloc');
+    $pkey = $schema['primary key'][0];
+    $field_name = 'featureloc';
+    return array(
+      'field_name' => $field_name,
+      'type' => 'chado_linker__featureloc',
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'field_chado_storage',
+      ),
+      'settings' => array(
+        'chado_table' => 'featureloc',
+        'chado_column' => $pkey,
+        'base_table' => 'feature',
+        'semantic_web' => 'SO:position_of',
+      ),
+    );
+  }
+  // GENOTYPE
+  $genotype_table = $table_name . '_genotype';
+  if (chado_table_exists($genotype_table)) {
+    $schema = chado_get_schema($genotype_table);
+    $pkey = $schema['primary key'][0];
+    $field_name = $table_name . '_genotype';
+    return array(
+      'field_name' => $field_name,
+      'type' => 'chado_linker__genotype',
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'field_chado_storage',
+      ),
+      'settings' => array(
+        'chado_table' => $genotype_table,
+        'chado_column' => $pkey,
+        'semantic_web' => 'SO:0001027',
+        'base_table' => $table_name,
+      ),
+    );
+  }
+
+  // PHENOTYPE
+  $phenotype_table = $table_name . '_phenotype';
+  if (chado_table_exists($phenotype_table)) {
+    $schema = chado_get_schema($phenotype_table);
+    $pkey = $schema['primary key'][0];
+    $field_name = $table_name . '_phenotype';
+    return array(
+      'field_name' => $field_name,
+      'type' => 'chado_linker__phenotype',
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'field_chado_storage',
+      ),
+      'settings' => array(
+        'chado_table' => $phenotype_table,
+        'chado_column' => $pkey,
+        'base_table' => $table_name,
+        'semantic_web' => 'SBO:0000358',
+      ),
+    );
+  }
+
+  // PROPERTIES
+
+  // PUBLICATIONS
+  $pub_table = $table_name . '_pub';
+  if (chado_table_exists($pub_table)) {
+    $schema = chado_get_schema($pub_table);
+    $pkey = $schema['primary key'][0];
+    $field_name = $table_name . '_pub';
+    return array(
+      'field_name' => $field_name,
+      'type' => 'chado_linker__pub',
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'field_chado_storage',
+      ),
+      'settings' => array(
+        'chado_table' => $pub_table,
+        'chado_column' => $pkey,
+        'base_table' => $table_name,
+        'semantic_web' => 'schema:publication',
+      ),
+    );
+  }
+
+  // RELATIONSHIPS
+  // If the linker table does not exists then we don't want to add attach.
+  $rel_table = $table_name . '_relationship';
+  if (chado_table_exists($rel_table)) {
+    $schema = chado_get_schema($rel_table);
+    $pkey = $schema['primary key'][0];
+    $field_name = $table_name . '_relationship';
+    return array(
+      'field_name' => $field_name,
+      'type' => 'chado_linker__relationship',
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'field_chado_storage',
+      ),
+      'settings' => array(
+        'chado_table' => $rel_table,
+        'chado_column' => $pkey,
+        'base_table' => $table_name,
+        'semantic_web' => 'SBO:0000374',
+      ),
+    );
+
+  }
+
+  // SYNONYMS
+
+}
+
 /**
  * A helper function for the tripal_chado_field_create_info() function.
  *