Browse Source

Fixed remote__data formatter to handle errors and improve layout

Stephen Ficklin 6 years ago
parent
commit
1be4902acd

+ 0 - 2
tripal_ws/api/tripal_ws.api.inc

@@ -241,7 +241,6 @@ function tripal_get_remote_content($site_id, $path = '', $query = '') {
   if ($query) {
     $ws_url = $ws_url . '?' . $query;
   }
-  dpm($ws_url);
 
   // TODO: something is wrong here, the query is not being recognized on
   // the remote Tripal site. It's just returning the default.
@@ -269,7 +268,6 @@ function tripal_get_remote_content($site_id, $path = '', $query = '') {
     tripal_report_error('tripal_ws', TRIPAL_ERROR,
         'Tripal remote web services reports the following error: !error. Using URL: !url',
         array('!error' => $error, '!url' => $ws_url));
-    return FALSE;
   }
 
   return $data;

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

@@ -136,6 +136,7 @@ class remote__data extends WebServicesField {
     $entity->{$field_name}['und'][0] = array(
       'value' => array(),
       'remote_entity' => array(),
+      'error' => NULL,
     );
 
     // If this field is being loaded via web services then just return.
@@ -155,11 +156,11 @@ class remote__data extends WebServicesField {
     }
 
     $total_items = $data['totalItems'];
-dpm($data);
+
     // Iterate through the members returned and save those for the field.
     for ($i = 0; $i < $total_items; $i++) {
       $member = $data['member'][$i];
-      dpm($member);
+
 
       // Get the cotent type and remote entity id
       $content_type = $member['@type'];
@@ -177,16 +178,14 @@ dpm($data);
       // Next get the the details about this member.
       $query_field_url =  $content_type . '/' . $remote_entity_id . '/' . $query_field;
       $field_data = $this->makeRemoteRequest($query_field_url);
+
       if(!$field_data){
         // If we encounter any type of error, we'll reset the field and return.
-        $entity->{$field_name}['und'] = array();
-        $entity->{$field_name}['und'][0] = array(
-          'value' => array(),
-          'remote_entity' => array(),
-        );
+        $entity->{$field_name}['und'][$i]['value'] = 'Unable to retrieve remote content';
+        $entity->{$field_name}['und'][$i]['error'] = TRUE;
         return;
       }
-
+      
       // Set the field data as the value.
       $field_data_type = $field_data['@type'];
       $entity->{$field_name}['und'][$i]['value'] = $field_data;

+ 19 - 10
tripal_ws/includes/TripalFields/remote__data/remote__data_formatter.inc

@@ -69,6 +69,8 @@ class remote__data_formatter extends WebServicesFieldFormatter {
    *    hook_field_formatter_view() function.
    */
   public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
+    $content = '';
+    
     // Get the settings
     $settings = $display['settings'];
     $field_name = $this->field['field_name'];
@@ -94,14 +96,9 @@ class remote__data_formatter extends WebServicesFieldFormatter {
       ->execute()
       ->fetchObject();
 
-      $content = '<p>';
-    if (is_object($site_logo)) {
-      $content .= '<img class="tripal-remote--data-field-logo" src="' . file_create_url($site_logo->uri) . '"><br/>';
-    }
-    $content .=  t('This content provided by !site.',
-        array('!site' => l($site->name, $site->url, array('attributes' => array("target" => '_blank')))));
-    $content .= '</p>';
+    // Iterate through the results and create a generic table.
     $rows = array();
+    $headers = array('');
     foreach ($items as $index => $item) {
       $remote_entity_label = $item['remote_entity']['label'];
       $remote_entity_page = $item['remote_entity']['ItemPage'];
@@ -112,7 +109,11 @@ class remote__data_formatter extends WebServicesFieldFormatter {
       if (!$value) {
         continue;
       }
-      $headers = array('');
+      $error = $item['error'];
+      if ($error) {
+        $rows[] = [['error' => $value]];
+        continue;
+      }
 
       // If this is a collection then handle it as a list of members.
       if (array_key_exists('members', $value)) {
@@ -128,7 +129,7 @@ class remote__data_formatter extends WebServicesFieldFormatter {
         }
         else {
           if (array_key_exists($flabel, $value)) {
-            $rows[] = array(l($value[$flabel], $remote_entity_page, array('attributes' => array('target' => '_blank'))));
+            $rows[] = array($value[$flabel]);
           }
           else {
             $value['Link'] = l('View content on ' . $site->name, $remote_entity_page, array('attributes' => array('target' => '_blank')));
@@ -136,7 +137,6 @@ class remote__data_formatter extends WebServicesFieldFormatter {
           }
         }
       }
-
     }
 
     $has_sub_tables = FALSE;
@@ -171,6 +171,15 @@ class remote__data_formatter extends WebServicesFieldFormatter {
         $content .= $rows[$i][0];
       }
     }
+    
+    $content .= '<p>';
+    if (is_object($site_logo)) {
+      $content .= '<img class="tripal-remote--data-field-logo" src="' . file_create_url($site_logo->uri) . '"><br/>';
+    }
+    $content .=  t('This content provided by !site.',
+      array('!site' => l($site->name, $site->url, array('attributes' => array("target" => '_blank')))));
+    $content .= '</p>';
+    
     // Return the content for this field.
     $element[0] = array(
       '#type' => 'markup',

+ 1 - 0
tripal_ws/theme/css/tripal_ws.css

@@ -5,6 +5,7 @@
 }
 .tripal-remote--data-field-logo {
   max-height: 100px;
+  max-width: 150px;
 }
 
 .tripal-remote--data-field-dl dt {