Browse Source

fixed issue with field not being instantiated

Stephen Ficklin 7 years ago
parent
commit
326ccb5419

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

@@ -534,8 +534,6 @@ function tripal_create_bundle_fields($bundle, $term) {
     // If the field already exists then skip it.
     $field = field_info_field($details['field_name']);
     if ($field) {
-      tripal_set_message(t("Could not create new field: %field because it already exists.",
-        array('%field' =>  $details['field_name'])), TRIPAL_WARNING);
       continue;
     }
 

+ 7 - 27
tripal_ws/includes/TripalFields/remote__data/remote__data.inc

@@ -7,6 +7,7 @@
  * Assumptions:
  */
 class remote__data extends WebServicesField {
+
   // --------------------------------------------------------------------------
   //                     EDITABLE STATIC CONSTANTS
   //
@@ -16,8 +17,10 @@ class remote__data extends WebServicesField {
   // --------------------------------------------------------------------------
   // The default label for this field.
   public static $default_label = 'Remote Data';
+
   // The default description for this field.
   public static $default_description = 'remote data';
+
   // The default widget for this field.
   //public static $default_widget = '';
   // The default formatter for this field.
@@ -30,7 +33,7 @@ class remote__data extends WebServicesField {
   // Once instances exist for a field type then these settings cannot be
   // changed.
   public static $default_settings = array(
-    'storage' => 'tripal_no_storage',
+    'storage' => 'field_tripal_ws_storage',
     // It is expected that all fields set a 'value' in the load() function.
     // In many cases, the value may be an associative array of key/value pairs.
     // In order for Tripal to provide context for all data, the keys should
@@ -73,32 +76,9 @@ class remote__data extends WebServicesField {
   // could be a quick search field that appears on the page that redirects
   // the user but otherwise provides no data.
   public static $no_data = FALSE;
- /**
-   * Loads the field values from the underlying data store.
-   *
-   * @param $entity
-   *
-   * @return
-   *   An array of the following format:
-   *     $entity->{$field_name}['und'][0]['value'] = $value;
-   *   where:
-   *     - $entity is the entity object to which this field is attached.
-   *     - $field_name is the name of this field
-   *     - 'und' is the language code (in this case 'und' == undefined)
-   *     - 0 is the cardinality.  Increment by 1 when more than one item is
-   *       available.
-   *     - 'value' is the key indicating the value of this field. It should
-   *       always be set.  The value of the 'value' key will be the contents
-   *       used for web services and for downloadable content.  The value
-   *       should be of the follow format types: 1) A single value (text,
-   *       numeric, etc.) 2) An array of key value pair. 3) If multiple entries
-   *       then cardinality should incremented and format types 1 and 2 should
-   *       be used for each item.
-   *   The array may contain as many other keys at the same level as 'value'
-   *   but those keys are for internal field use and are not considered the
-   *   value of the field.
-   *
-   *
+
+  /**
+   * @see WebServicesField::load()
    */
   public function load($entity) {
   }

+ 68 - 0
tripal_ws/includes/tripal_ws.field_storage.inc

@@ -0,0 +1,68 @@
+<?php
+
+/**
+ * Implements hook_field_storage_info().
+ */
+function tripal_ws_field_storage_info() {
+  return array(
+    'field_tripal_ws_storage' => array(
+      'label' => t('Tripal Web Services'),
+      'description' => t('Retreives fields data from a remote site using Tripal web services.'),
+      'settings' => array(),
+    ),
+  );
+}
+
+/**
+ * Implements hook_field_storage_load().
+ *
+ * Responsible for loading the fields from the Chado database and adding
+ * their values to the entity.
+ */
+function tripal_ws_field_storage_load($entity_type, $entities, $age,
+    $fields, $options) {
+
+  $load_current = $age == FIELD_LOAD_CURRENT;
+  global $language;
+  $langcode = $language->language;
+
+  foreach ($entities as $id => $entity) {
+
+    // Iterate through the entity's fields so we can get the column names
+    // that need to be selected from each of the tables represented.
+    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
+      // memory upfront with field_info_field_by_ids().
+      $field = field_info_field_by_id($field_id);
+      $field_name = $field['field_name'];
+      $field_type = $field['type'];
+      $field_module = $field['module'];
+
+      // Set an empty value by default, and let the hook function update it.
+      $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, $instance);
+        $tfield->load($entity, array('record' => $record));
+      }
+
+    } // end: foreach ($fields as $field_id => $ids) {
+  } // end: foreach ($entities as $id => $entity) {
+}
+
+/**
+ * Implements hook_field_storage_query().
+ */
+function tripal_ws_field_storage_query($query) {
+
+}
+/**
+ * Implements hook_field_storage_write().
+ */
+function tripal_ws_field_storage_write($entity_type, $entity, $op, $fields) {
+
+}
+

+ 61 - 91
tripal_ws/includes/tripal_ws.fields.inc

@@ -6,120 +6,90 @@
 
 /**
  * Implements hook_bundle_fields_info().
- *
- * This hook tells Drupal/Tripal about your new field type. Make sure you've created the
- * field (handles basic storage of your data), widget (provides user UI to set data),
- * and formatter (describes display of data on Entity Page) classes. These should be
- * located in the following directory: [your module]/includes/TripalFields/[classname].inc
- * with one file per class. Your field name should be [cv name]__[cvterm name] and the
- * classes should be named [field name], [field_name]_widget, [field name]_formatter
- * for the field, widget and formatter respectively. MAKE SURE YOU'VE CLEARED THE CACHE
- * SINCE ADDING THESE FILES so Tripal magic can find them or the following will fail.
- *
- * @param $entity_type
- *   This should be 'TripalEntity' for all Tripal Content.
- * @param $bundle
- *   This object describes the Type of Tripal Entity (e.g. Organism or Gene) this hook is
- *   being called for. However, since this hook creates field types (by definition not
- *   tied to a specific Tripal Content Type (bundle)) and since a field type will only be
- *   created if it doesn't already exist, this parameter doesn't actually matter.
- *   NOTE: If you do need to determine the bundle in this hook, we suggest inspecting
- *   the data_table since the label can be changed by site administrators.
- *
- * @return
- *   An array of field definitions. Each field in this array will be created if it
- *   doesn't already exist. To trigger create of fields when developing call
- *   tripal_refresh_bundle_fields() for the specific bundle.
  */
 function tripal_ws_bundle_fields_info($entity_type, $bundle) {
   $fields = array();
-  $field_name = 'remote__data';
-  $field_type = 'remote__data';
-  $fields[$field_name] = array(
-    'field_name' => $field_name,
-    'type' => $field_type,
-    'cardinality' => 1,
-    'locked' => FALSE,
-    'storage' => array(
-    'type' => 'field_no_storage',
-    ),
-  );/*
-  $field_name = 'germplasm_summary';
-  $field_type = 'local';
-  $fields[$field_name] = array(
-    'field_name' => $field_name,
-    'type' => $field_type,
-    'cardinality' => 1,
-    'locked' => FALSE,
-    'storage' => array(
-    'type' => 'field_chado_storage',
-    ),
-  );*/
+
+  // No fields are added programmatically.
   return $fields;
 }
 
 /**
  * Implements hook_bundle_instances_info().
- *
- * This hook tells Drupal/Tripal to create a field instance of a given field
- * type on a specific Tripal Content type (otherwise known as the bundle). Make
- * sure to implement hook_bundle_fields_info() to create your field type
- * before trying to create an instance of that field.
- *
- * @param $entity_type
- *   This should be 'TripalEntity' for all Tripal Content.
- * @param $bundle
- *   This object describes the Type of Tripal Entity (e.g. Organism or Gene)
- *   the field instances are being created for. Thus this hook is called once
- *   per Tripal Content Type on your site. The name of the bundle is the
- *   machine name of the type (e.g. bio_data_1) and the label of the bundle
- *   (e.g. Organism) is what you see in the interface. Since the label can be
- *   changed by site admin, we suggest checking the data_table to determine if
- *   this is the entity you want to add field instances to.
- * @return
- *   An array of field instance definitions. This is where you can define the
- *   defaults for any settings you use in your field. Each entry in this array
- *   will be used to create an instance of an already existing field.
  */
 function tripal_ws_bundle_instances_info($entity_type, $bundle) {
   $instances = array();
+
+  // No field instances are added programmatically.
+  return $instances;
+}
+
 /**
-  // ORGANISM.
-  //===============
-  if (isset($bundle->data_table) AND ($bundle->data_table == 'organism')) {
+ * Implements hook_bundle_create_user_field().
+ *
+ * A priviledged user has the ability to add new fields to the bundle. The
+ * remote__data field is allowed to be added dynamically by the user.
+ * But, Drupal doesn't know how to deal with it, so this function is called
+ * for any field attached to a TripalEntity bundle type. Any fields whose
+ * TripalField::$module argument is set to 'tripal_ws' and that can be
+ * added dynamically will result in a call to this function.
+ */
+function tripal_ws_bundle_create_user_field($new_field, $bundle) {
+
+  // Get the table this bundle is mapped to.
+  $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
+  $vocab = $term->vocab;
+  $params = array(
+    'vocabulary' => $vocab->vocabulary,
+    'accession' => $term->accession,
+  );
+
+  // We allow site admins to add new chado_linker__prop fields to an entity.
+  // This function will allow us to properly add them.  But at this point we
+  // don't know the controlled vocabulary term.  We'll have to use the
+  // defaults and let the user set it using the interface.
+  if ($new_field['type'] == 'remote__data') {
+    $field_name = $new_field['field_name'];
+    $field_type = 'remote__data';
+
+    // First add the field.
+    field_create_field(array(
+      'field_name' => $field_name,
+      'type' => $field_type,
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'locked' => FALSE,
+      'storage' => array(
+        'type' => 'field_tripal_ws_storage',
+      ),
+    ));
 
-    // Germplasm Summary Field.
-    //---------------------------------
-    // Sumarizes germplasm on the organism page.
-    $field_name = 'local__germplasm_summary';
-    $field_type = 'local__germplasm_summary';
-    $instances[$field_name] =  array(
+    // Now add the instance
+    field_create_instance(array(
       'field_name' => $field_name,
-      'entity_type' => $entity_type,
+      'entity_type' => 'TripalEntity',
       'bundle' => $bundle->name,
-      'label' => 'Germplasm Summary',
-      'description' => 'Summarizes germplasm for this organism.',
+      'label' => $new_field['label'],
+      'description' => '',
       'required' => FALSE,
       'settings' => array(
-        'auto_attach' => FALSE,
-        'chado_table' => $bundle->data_table,
-        'chado_column' => 'organism_id',
-        'base_table' => $bundle->data_table,
+        'auto_attach' => TRUE,
+        'term_vocabulary' => '',
+        'term_accession' => '',
+        'term_name' => ''
       ),
       'widget' => array(
-        'type' => 'local__germplasm_summary_widget',
-        'settings' => array(),
+        'type' => 'remote__data_widget',
+        'settings' => array(
+          'display_label' => 1,
+        ),
       ),
       'display' => array(
         'default' => array(
-          'label' => 'hidden',
-          'type' => 'local__germplasm_summary_formatter',
+          'label' => 'inline',
+          'type' => 'remote__data_formatter',
           'settings' => array(),
         ),
       ),
-    );
-
+    ));
   }
-*/
-  return $instances;
-}
+}

+ 2 - 0
tripal_ws/tripal_ws.module

@@ -1,6 +1,8 @@
 <?php
 
 require_once  "api/tripal_ws.api.inc";
+require_once  "includes/tripal_ws.field_storage.inc";
+require_once  "includes/tripal_ws.fields.inc";
 require_once  "includes/TripalWebService.inc";
 require_once  "includes/TripalWebServiceResource.inc";
 require_once  "includes/TripalWebServiceCollection.inc";