Browse Source

Merge pull request #552 from statonlab/551-fix_remote_field

Fix remote field
Lacey-Anne Sanderson 6 years ago
parent
commit
20df09b72a

+ 30 - 12
tripal_ws/includes/TripalFields/remote__data/remote__data.inc

@@ -105,7 +105,7 @@ class remote__data extends WebServicesField {
     // We don't want remote content to be available in web services.  There
     // is an if statement to not show this field in the web services but the
     // entity_load function doesn't know this field shouldn't be loaded so
-    // we need to short-circuit that.  
+    // we need to short-circuit that.
     if (preg_match('/web-services/', $_SERVER['REQUEST_URI'])) {
       $this->loaded_via_ws = TRUE;
       return;
@@ -127,7 +127,7 @@ class remote__data extends WebServicesField {
    * @see WebServicesField::load()
    */
   public function load($entity) {
-    
+
     // If this field is being loaded via web services then just return.
     if ($this->loaded_via_ws == TRUE) {
       return;
@@ -159,7 +159,7 @@ class remote__data extends WebServicesField {
       $entity->{$field_name}['und'][0]['remote_entity'] = NULL;
       $entity->{$field_name}['und'][0]['error'] = TRUE;
       $entity->{$field_name}['und'][0]['warning'] = FALSE;
-      $entity->{$field_name}['und'][0]['query_str'] = $this->buildRemoteURL($this->remote_site, $query_str);      
+      $entity->{$field_name}['und'][0]['query_str'] = $this->buildRemoteURL($this->remote_site, $query_str);
       return;
     }
     // Make sure we didn't have a problem
@@ -185,7 +185,7 @@ class remote__data extends WebServicesField {
       $entity->{$field_name}['und'][0]['query_str'] = $this->buildRemoteURL($this->remote_site, $query_str);
       return;
     }
-    
+
     // Iterate through the members returned and save those for the field.
     for ($i = 0; $i < $num_items; $i++) {
       $member = $data['member'][$i];
@@ -203,9 +203,9 @@ class remote__data extends WebServicesField {
       // 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 we encounter any type of error, we'll reset the field and return.
-      if (array_key_exists('error', $field_data)) {
+      if ($field_data && array_key_exists('error', $field_data)) {
         $entity->{$field_name} = [];
         $entity->{$field_name}['und'][0]['value'] = 'ERROR: there was a problem retrieving secific content for this field.';
         $entity->{$field_name}['und'][0]['admin_message'] = "While iterating through the list of results, the " .
@@ -216,7 +216,7 @@ class remote__data extends WebServicesField {
         $entity->{$field_name}['und'][0]['query_str'] = $this->buildRemoteURL($this->remote_site, $query_field_url);
         return;
       }
-      
+
       // Set the field data as the value.
       $field_data_type = $field_data['@type'];
       $entity->{$field_name}['und'][$i]['value'] = $field_data;
@@ -227,7 +227,7 @@ class remote__data extends WebServicesField {
       $entity->{$field_name}['und'][$i]['query_str'] = $this->buildRemoteURL($this->remote_site, $query_field_url);;
     }
   }
-  
+
   /**
    * Used to build the full URL for the query.
    */
@@ -237,6 +237,12 @@ class remote__data extends WebServicesField {
     if (preg_match('/\?/', $query)) {
       list($path, $q) = explode('?', $query);
     }
+
+    if(empty($remote_site)) {
+      tripal_report_error('tripal_ws', TRIPAL_ERROR, 'Unable to find remote_site in remote__data field while attempting to build the remote URL.');
+      return null;
+    }
+
     return tripal_build_remote_content_url($remote_site, $path, $q);
   }
    /**
@@ -245,6 +251,7 @@ class remote__data extends WebServicesField {
     * @param $query
     *   The query string. This string is added to the URL for the remote
     *   website.
+    * @return array on success or null if request fails.
     */
    private function makeRemoteRequest($query) {
      $path = $query;
@@ -252,7 +259,18 @@ class remote__data extends WebServicesField {
      if (preg_match('/\?/', $query)) {
        list($path, $q) = explode('?', $query);
      }
-     $data = tripal_get_remote_content($this->remote_site->id, $path, $q);
+
+    if(empty($this->remote_site)) {
+      tripal_report_error('tripal_ws', TRIPAL_ERROR, 'Unable to find remote_site while attempting to make the request.');
+      return null;
+    }
+
+     try {
+       $data = tripal_get_remote_content($this->remote_site->id, $path, $q);
+     } catch (Exception $exception) {
+      tripal_report_error('tripal_ws', TRIPAL_ERROR, $exception->getMessage());
+       return null;
+     }
 
      return $data;
    }
@@ -306,19 +324,19 @@ class remote__data extends WebServicesField {
     $element['data_info']['query'] = array(
       '#type' => 'textarea',
       '#title' => 'Query to Execute',
-      '#description' => 'Enter the query that will retreive the remote records. ' . 
+      '#description' => 'Enter the query that will retreive the remote records. ' .
         'If the full URL to the content web service is ' .
         'https://[tripal_site]/web-services/content/v0.1/. Then this field should ' .
         'contain the text immediately after the content/v0.1 portion of the URL. ' .
         'For information about building web services queries see the ' .
-        'online documentation at ' . l('The Tripal v3 User\'s Guide', 'http://tripal.info/tutorials/v3.x/web-services') . '. ' . 
+        'online documentation at ' . l('The Tripal v3 User\'s Guide', 'http://tripal.info/tutorials/v3.x/web-services') . '. ' .
         'For example, suppose this field is attached to an ' .
         'Organism content type on the local site, and you want to retrieve a ' .
         'field for the same organism on a remote Tripal site then you will ' .
         'want to query on the genus and species. Also, you want the genus and ' .
         'species to match the organism that this field is attached to. You can ' .
         'use tokens to do this (see the "Available Tokesn" fieldset below). ' .
-        'For this example, the query text should be ' . 
+        'For this example, the query text should be ' .
         'Organism?genus=[taxrank__genus]&species=[taxrank__species].',
       '#default_value' => $this->instance['settings']['data_info']['query'],
       '#rows' => 5,

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

@@ -15,7 +15,7 @@ class remote__data_formatter extends WebServicesFieldFormatter {
    */
   public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
     $content = '';
-    
+
     // Get the settings
     $settings = $display['settings'];
     $field_name = $this->field['field_name'];
@@ -51,7 +51,7 @@ class remote__data_formatter extends WebServicesFieldFormatter {
       $value = $item['value'];
       $error = $item['error'];
       $warning = $item['warning'];
-      
+
       // If there is an error or warning then clear the cache for this field
       // so that next time the page is loaded it will try to reload again.
       if ($error or $warning) {
@@ -68,7 +68,7 @@ class remote__data_formatter extends WebServicesFieldFormatter {
         $rows[] = [$value];
         continue;
       }
-      
+
       $remote_entity_label = array_key_exists('label', $item) ? $item['remote_entity']['label'] : '';
       $remote_entity_page = $item['remote_entity']['ItemPage'];
       $remote_entity_link = t('View !data on %site',
@@ -98,7 +98,7 @@ class remote__data_formatter extends WebServicesFieldFormatter {
         }
       }
     }
-    
+
     // TODO: we need to handle paged elements.
 
     $has_sub_tables = FALSE;
@@ -139,19 +139,27 @@ class remote__data_formatter extends WebServicesFieldFormatter {
         $content .= $rows[$i][0];
       }
     }
-    
+
     $content .= '<p>';
 
-    $content .=  t('This content provided by !site.',
-      array('!site' => l($site->name, $site->url, array('attributes' => array("target" => '_blank')))));
+    if($site) {
+      $content .= t('This content provided by !site.', [
+          '!site' => l($site->name, $site->url,
+            ['attributes' => ["target" => '_blank']])
+        ]);
+    }
+    else  {
+      tripal_report_error('tripal_ws', TRIPAL_ERROR, 'Unable to find remote site while attempting to format results.');
+    }
+
     if (is_object($site_logo)) {
       $content .= '<img class="tripal-remote--data-field-logo" src="' . file_create_url($site_logo->uri) . '"><br/>';
     }
     if (count($items) == 1) {
-      $content .= $remote_entity_link;
+      $content .= isset($remote_entity_link) ? $remote_entity_link : '';
     }
     $content .= '</p>';
-    
+
     // Return the content for this field.
     $element[0] = array(
       '#type' => 'markup',
@@ -346,4 +354,4 @@ class remote__data_formatter extends WebServicesFieldFormatter {
    */
   public function settingsSummary($view_mode) {
   }
-}
+}