Browse Source

Spearated field and field_storage hooks for tripal module into separate include files

Stephen Ficklin 8 years ago
parent
commit
1098091ab6

+ 75 - 0
tripal/includes/tripal.field_storage.inc

@@ -0,0 +1,75 @@
+<?php
+
+/**
+ * Implements hook_field_storage_info().
+ *
+ * The Tripal module does not provide a storage back-end.  But it does provide
+ * a placeholder when no storage backend is needed but a field
+ * is still desired.  The 'tripal_no_storage' backend is used for the
+ * content_type field which adds a type field to every entity.
+ */
+function tripal_field_storage_info() {
+  return array(
+    'tripal_no_storage' => array(
+      'label' => t('Tripal'),
+      'description' => t('The NULL storage is a placeholder for field values
+          that are not stored in any storage backend (e.g. entity types).'),
+      '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_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.
+    $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
+      // 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'];
+
+      // 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 default value.
+      $load_function = $field_module . '_' . $field_type . '_field_load';
+      module_load_include('inc', $field_module, 'includes/fields/' . $field_type);
+      if (function_exists($load_function)) {
+        $load_function($field, $entity);
+      }
+
+    } // end: foreach ($fields as $field_id => $ids) {
+  } // end: foreach ($entities as $id => $entity) {
+}
+
+/**
+ * Implements hook_field_storage_query().
+ *
+ * Used by EntityFieldQuery to find the entities having certain entity
+ * and field conditions and sort them in the given field order.
+ *
+ * NOTE: This function needs to exist or errors are triggered but so far it doesn't
+ * appear to actually need to do anything...
+ */
+function tripal_field_storage_query($query) {
+
+}

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

@@ -0,0 +1,103 @@
+<?php
+
+/**
+ * Implements hook_field_info().
+ */
+function tripal_field_info() {
+  $fields = array(
+    'content_type' => array(
+      'label' => t('Record Type'),
+      'description' => t('The content type.'),
+      'default_widget' => 'tripal_content_type_widget',
+      'default_formatter' => 'tripal_content_type_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'tripal_no_storage',
+        'module' => 'tripal',
+        'active' => TRUE
+      ),
+    ),
+  );
+  return $fields;
+}
+/**
+ * Implements hook_field_widget_info();
+ */
+function tripal_field_widget_info() {
+  return array(
+    'tripal_content_type_widget' => array(
+      'label' => t('Record Type'),
+      'field types' => array('content_type')
+    ),
+  );
+}
+
+/**
+ * Implements hook_field_formatter_info().
+ */
+function tripal_field_formatter_info() {
+  return array(
+    'tripal_content_type_formatter' => array(
+      'label' => t('Record Type'),
+      'field types' => array('content_type')
+    ),
+  );
+}
+/**
+ * Implements hook_field_widget_form().
+ */
+function tripal_field_widget_form(&$form, &$form_state, $field,
+    $instance, $langcode, $items, $delta, $element) {
+
+  $widget = $element;
+  switch ($instance['widget']['type']) {
+    case 'tripal_content_type_widget':
+      // There is no widget for this type.
+      break;
+  }
+}
+/**
+ * Implements hook_field_formatter_view().
+ */
+function tripal_field_formatter_view($entity_type, $entity, $field,
+    $instance, $langcode, $items, $display) {
+
+  $element = array();
+  switch ($display['type']) {
+    case 'tripal_content_type_formatter':
+      module_load_include('inc', 'tripal', 'includes/fields/content_type');
+      tripal_content_type_formatter($element, $entity_type, $entity, $field,
+          $instance, $langcode, $items, $display);
+      break;
+  }
+  return $element;
+}
+/**
+ * Implements hook_field_is_empty().
+ */
+function tripal_field_is_empty($item, $field) {
+
+  // If there is no value field then the field is empty.
+  if (!array_key_exists('value', $item)) {
+    return TRUE;
+  }
+
+  // Iterate through all of the fields and if at least one has a value
+  // the field is not empty.
+  foreach ($item as $form_field_name => $value) {
+    if (isset($value) and $value != NULL and $value != '') {
+      return FALSE;
+    }
+  }
+
+  // Otherwise, the field is empty.
+  return TRUE;
+}
+
+/**
+ * Simple provides a message indicating that the field cannot be deleted.
+ */
+function tripal_field_no_delete() {
+  drupal_set_message('This field cannot be removed.', 'warning');
+  return '';
+}

+ 4 - 169
tripal/tripal.module

@@ -7,6 +7,9 @@
 // Import the full Tripal API into scope.
 tripal_import_api();
 
+require_once "includes/tripal.field_storage.inc";
+require_once "includes/tripal.fields.inc";
+
 require_once "includes/TripalVocab.inc";
 require_once "includes/TripalVocabController.inc";
 require_once "includes/TripalVocabViewsController.inc";
@@ -824,13 +827,7 @@ function tripal_menu_alter(&$items) {
   $items['admin/structure/bio_data/manage/%TripalBundle/fields/%field_ui_menu/delete']['page arguments'] = array();
 }
 
-/**
- * Simple provides a message indicating that the field cannot be deleted.
- */
-function tripal_field_no_delete() {
-  drupal_set_message('This field cannot be removed.', 'warning');
-  return '';
-}
+
 
 /**
  * Imports all of the Tripal API into scope.
@@ -856,96 +853,6 @@ function tripal_import_api() {
   module_load_include('inc', 'tripal', 'api/tripal.variables.api');
 }
 
-/**
- * Implements hook_field_storage_info().
- *
- * The Tripal module does not provide a storage back-end.  But it does provide
- * a placeholder when no storage backend is needed but a field
- * is still desired.  The 'tripal_no_storage' backend is used for the
- * content_type field which adds a type field to every entity.
- */
-function tripal_field_storage_info() {
-  return array(
-    'tripal_no_storage' => array(
-      'label' => t('Tripal'),
-      'description' => t('The NULL storage is a placeholder for field values
-          that are not stored in any storage backend (e.g. entity types).'),
-      'settings' => array(),
-    ),
-  );
-}
-/**
- * Implements hook_field_info().
- */
-function tripal_field_info() {
-  $fields = array(
-    'content_type' => array(
-      'label' => t('Record Type'),
-      'description' => t('The content type.'),
-      'default_widget' => 'tripal_content_type_widget',
-      'default_formatter' => 'tripal_content_type_formatter',
-      'settings' => array(),
-      'storage' => array(
-        'type' => 'tripal_no_storage',
-        'module' => 'tripal',
-        'active' => TRUE
-      ),
-    ),
-  );
-  return $fields;
-}
-/**
- * Implements hook_field_widget_info();
- */
-function tripal_field_widget_info() {
-  return array(
-    'tripal_content_type_widget' => array(
-      'label' => t('Record Type'),
-      'field types' => array('content_type')
-    ),
-  );
-}
-
-/**
- * Implements hook_field_formatter_info().
- */
-function tripal_field_formatter_info() {
-  return array(
-    'tripal_content_type_formatter' => array(
-      'label' => t('Record Type'),
-      'field types' => array('content_type')
-    ),
-  );
-}
-/**
- * Implements hook_field_widget_form().
- */
-function tripal_field_widget_form(&$form, &$form_state, $field,
-    $instance, $langcode, $items, $delta, $element) {
-
-  $widget = $element;
-  switch ($instance['widget']['type']) {
-    case 'tripal_content_type_widget':
-      // There is no widget for this type.
-      break;
-  }
-}
-/**
- * Implements hook_field_formatter_view().
- */
-function tripal_field_formatter_view($entity_type, $entity, $field,
-    $instance, $langcode, $items, $display) {
-
-  $element = array();
-  switch ($display['type']) {
-    case 'tripal_content_type_formatter':
-      module_load_include('inc', 'tripal', 'includes/fields/content_type');
-      tripal_content_type_formatter($element, $entity_type, $entity, $field,
-          $instance, $langcode, $items, $display);
-      break;
-  }
-  return $element;
-}
 
 /**
  * Implemenation of hook_add_bundle_fields().
@@ -983,77 +890,5 @@ function tripal_add_bundle_fields($entity_type, $bundle, $term) {
   tripal_add_bundle_field($field_name, $field_info, $entity_type, $bundle_name);
 }
 
-/**
- * Implements hook_field_storage_query().
- *
- * Used by EntityFieldQuery to find the entities having certain entity
- * and field conditions and sort them in the given field order.
- *
- * NOTE: This function needs to exist or errors are triggered but so far it doesn't
- * appear to actually need to do anything...
- */
-function tripal_field_storage_query($query) { }
-
-/**
- * Implements hook_field_storage_load().
- *
- * Responsible for loading the fields from the Chado database and adding
- * their values to the entity.
- */
-function tripal_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.
-    $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
-      // 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'];
-
-      // 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 default value.
-      $load_function = $field_module . '_' . $field_type . '_field_load';
-      module_load_include('inc', $field_module, 'includes/fields/' . $field_type);
-      if (function_exists($load_function)) {
-        $load_function($field, $entity);
-      }
-
-    } // end: foreach ($fields as $field_id => $ids) {
-  } // end: foreach ($entities as $id => $entity) {
-}
-
-/**
- * Implements hook_field_is_empty().
- */
-function tripal_field_is_empty($item, $field) {
 
-  // If there is no value field then the field is empty.
-  if (!array_key_exists('value', $item)) {
-    return TRUE;
-  }
 
-  // Iterate through all of the fields and if at least one has a value
-  // the field is not empty.
-  foreach ($item as $form_field_name => $value) {
-    if (isset($value) and $value != NULL and $value != '') {
-      return FALSE;
-    }
-  }
-
-  // Otherwise, the field is empty.
-  return TRUE;
-}

+ 30 - 6
tripal_ws/includes/tripal_ws.rest.inc

@@ -397,6 +397,22 @@ function tripal_ws_get_content_type($api_url, &$response, $ws_args, $ctype) {
   $response['@context']['label'] = 'rdfs:label';
   $response['@context']['itemPage'] = 'schema:itemPage';
 
+  $response['operation'][] = array(
+    '@type' => 'hydra:CreateResourceOperation',
+    'hydra:method' => 'PUT'
+  );
+
+  $response['query'] = array(
+    '@id' => $response['@id'],
+    '@type' => 'IriTemplate',
+    "template" => $response['@id'] . "{?name,}",
+    "mapping" => array(
+      array(
+        "hydra:variable" => 'name',
+        "hydra:property" => 'name',
+      )
+    )
+  );
 }
 
 /**
@@ -436,10 +452,10 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
 
   // Next add in the ID and Type for this resources.
   $response['@id'] = $api_url . '/content/' . $ctype . '/' . $entity_id;
-  $response['@type'] = $term->name;
+  $response['@type'] = $vocab->namespace . ':' . $term->accession;
   $response['label'] = $entity->title;
   $response['itemPage'] = url('/bio_data/' . $entity->id, array('absolute' => TRUE));
-  $response['@context'][$term->name] = $vocab->namespace . ':' . $term->accession;
+
 
   // Get information about the fields attached to this bundle and sort them
   // in the order they were set for the display.
@@ -455,7 +471,6 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
     }
     return ($a_weight < $b_weight) ? -1 : 1;
   });
-
   // Iterate throught the fields and add each value to the response.
   //$response['fields'] = $fields;
   foreach ($fields as $field_name => $field) {
@@ -504,15 +519,14 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
         $lterm_details = tripal_get_term_details($lvocab->namespace, $lterm->namespace);
         $value[] = array(
           '@id'   => $api_url . '/content/' . $lterm->name . '/' . $items[0]['entity_id'],
-          '@type' => $lterm->name,
+          '@type' => $lvocab->namespace . ':' . $lterm->accession,
           'label' => $lentity->title,
+          'type' => $lterm->name,
         );
 
         if (!array_key_exists($lvocab->namespace, $response['@context'])) {
           $response['@context'][$lvocab->namespace] = $lterm_details->url;
         }
-        // TODO: what if the term is used by multiple vocabularies.
-        $response['@context'][$lterm->name] = $lvocab->namespace . ':' . $lterm->accession;
       }
       else {
         $value[] = $items[$i]['value'];
@@ -530,6 +544,16 @@ function tripal_ws_get_content($api_url, &$response, $ws_args, $ctype, $entity_i
   // Lastly, add in the terms used into the @context section.
   $response['@context']['label'] = 'rdfs:label';
   $response['@context']['itemPage'] = 'schema:itemPage';
+
+  $response['operation'][] = array(
+    '@type' => 'hydra:DeleteResourceOperation',
+    'hydra:method' => 'DELETE'
+  );
+  $response['operation'][] = array(
+    '@type' => 'hydra:ReplaceResourceOperation',
+    'hydra:method' => 'POST'
+  );
+
 }