From 9325207852f257977ac8f1d789d237d67bae765d Mon Sep 17 00:00:00 2001
From: Michael Paler <paler@civicsolar.com>
Date: Tue, 28 Nov 2017 13:56:01 -0800
Subject: [PATCH] Issue #2828477 by ChristianWespire,mpaler: Added drupal variable for setting
 api key.

---
 views_json_query_plugin_query_json.inc | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/views_json_query_plugin_query_json.inc b/views_json_query_plugin_query_json.inc
index 2590d25..c2b9de9 100755
--- a/views_json_query_plugin_query_json.inc
+++ b/views_json_query_plugin_query_json.inc
@@ -72,6 +72,11 @@ class views_json_query_plugin_query_json extends views_plugin_query {
       }
     }
 
+    /* Add API KEY to headers if set */
+    if(array_key_exists('api_key', $this->options)){
+      $headers['API-KEY'] = $this->options['api_key'];
+    }
+
     $result = drupal_http_request($uri, array('headers' => $headers));
     if (isset($result->error)) {
       $args = array('%error' => $result->error, '%uri' => $uri);
@@ -333,6 +338,7 @@ class views_json_query_plugin_query_json extends views_plugin_query {
   function option_definition() {
     $options = parent::option_definition();
     $options['json_file'] = array('default' => '');
+    $options['api_key'] = array('default' => '');
     $options['row_apath'] = array('default' => '');
     $options['show_errors'] = array('default' => TRUE);
 
@@ -343,6 +349,10 @@ class views_json_query_plugin_query_json extends views_plugin_query {
    * Options form.
    */
   function options_form(&$form, &$form_state) {
+
+    $api_key_variable = $form_state['view']->name . '-' . $form_state['view']->current_display . '-' . 'api-key';
+    $api_key = variable_get($api_key_variable);
+
     $form['json_file'] = array(
       '#type' => 'textfield',
       '#title' => t('Json File'),
@@ -350,6 +360,13 @@ class views_json_query_plugin_query_json extends views_plugin_query {
       '#description' => t("The URL or path to the Json file."),
       '#maxlength' => 1024,
     );
+    $form['api_key'] = array(
+      '#type' => 'textfield',
+      '#title' => t('API KEY'),
+      '#default_value' => ($api_key ? $api_key : $this->options['api_key']),
+      '#description' => t("Set an API KEY value which will be added to HTTP request headers with the key \"api-key\". Set manually here or set via Drupal system variable called \"$api_key_variable\".<br />For example: drush vset $api_key_variable [your api key]<br />Note: System variable name is comprised of \"[view machine name]-[display name]-api-key\""),
+      '#maxlength' => 1024,
+    );
     $form['row_apath'] = array(
       '#type' => 'textfield',
       '#title' => t('Row Apath'),
-- 
2.13.6 (Apple Git-96)

