Commit fc0c9337 authored by Nathanael Dewhurst's avatar Nathanael Dewhurst
Browse files

Configuration and API helper function improvements.

More robust config form handling and better UX. Support various
scenarios including invalid API keys, clearing out dependent values
on update events, allowing users to select a default Brandfolder but
no default collection, etc.
Update API helper/wrapper/factory function to accept an optional API
key, falling back to the Drupal var previously used.
parent 22e4b2b6
Loading
Loading
Loading
Loading
+63 −2
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ function brandfolder_settings() {
  $brandfolders_list = $collections_list = [];
  $api_key = variable_get('brandfolder_guest_api_key', FALSE);
  if ($api_key) {
    $bf = new Brandfolder($api_key);
    $bf = brandfolder_api($api_key);
    try {
      $brandfolders_list = $bf->getBrandfolders();
    }
@@ -32,6 +32,10 @@ function brandfolder_settings() {
      drupal_set_message(t('After you choose a default Brandfolder, you can select a default collection if you wish'));
    }
  }
  $none_options = [
    '_none' => t('<None>')
  ];
  $collections_list = array_merge($none_options, $collections_list);

  /************************************
   * Credentials
@@ -75,7 +79,7 @@ function brandfolder_settings() {
    '#type'          => 'select',
    '#title'         => t('Default Collection'),
    '#options'       => $collections_list,
    '#default_value' => $default_collection,
    '#default_value' => is_null($default_collection) ? '_none' : $default_collection,
    '#description'   => t('The collection to use for all operations unless otherwise specified.'),
  ];

@@ -125,6 +129,15 @@ function brandfolder_settings() {

  $form = system_settings_form($form);

  if (isset($form['#validate']) && is_array($form['#validate'])) {
    array_unshift($form['#validate'], 'brandfolder_settings_form_validate');
  }
  else {
    $form['#validate'] = [
      'brandfolder_settings_form_validate',
    ];
  }

  // Display some images from the selected Brandfolder/collection if applicable.
  if ($default_brandfolder) {
    $bf->default_brandfolder_id = $default_brandfolder;
@@ -149,3 +162,51 @@ function brandfolder_settings() {

  return $form;
}

/**
 * Form validation handler for settings form.
 *
 * @param $form
 * @param $form_state
 */
function brandfolder_settings_form_validate(&$form, &$form_state) {
  $api_key = $form_state['values']['brandfolder_guest_api_key'];
  $api_success = FALSE;
  if (!empty($api_key)) {
    $bf = brandfolder_api($api_key);
    try {
      $brandfolders = $bf->getBrandfolders();
      // Note that the getBrandfolders request will return a 200 response even
      // if the API key is invalid, and the brandfolders array will simply be
      // empty. This is a quirk of the Brandfolder API.
      if (!empty($brandfolders)) {
        $api_success = TRUE;
      }
    }
    catch (\Exception $e) {
      $api_success = FALSE;
    }
    if (!$api_success) {
      $message = t('Could not connect to Brandfolder using this API key. Make sure the key is correct and is linked to a Brandfolder user who has permission to access at least one Brandfolder.');
      form_set_error('brandfolder_guest_api_key', $message);
    }
  }
  if ($api_success) {
    $old_brandfolder = variable_get('brandfolder_default_brandfolder');
    $specified_brandfolder = $form_state['values']['brandfolder_default_brandfolder'];
    // If the Brandfolder selection is being changed, reset the collection,
    // which is dependent on the Brandfolder. Otherwise, use the value
    // specified by the form (while also ensuring that a _none selection is
    // stored as NULL).
    $specified_collection = $form_state['values']['brandfolder_default_collection'];
    if ($specified_brandfolder != $old_brandfolder || $specified_collection == '_none') {
      $form_state['values']['brandfolder_default_collection'] = NULL;
    }
  }
  else {
    // If no (good) API key is specified, clear out any existing Brandfolder and
    // Collection choices.
    $form_state['values']['brandfolder_default_brandfolder'] = NULL;
    $form_state['values']['brandfolder_default_collection'] = NULL;
  }
}
+8 −2
Original line number Diff line number Diff line
@@ -1456,11 +1456,17 @@ function brandfolder_file_update($file) {

/**
 * Helper function to load a Brandfolder API object with default settings.
 *
 * @param string|null $api_key
 *
 * @return bool|\Brandfolder\Brandfolder
 */
function brandfolder_api() {
function brandfolder_api($api_key = NULL) {
  $bf = FALSE;

  if (empty($api_key)) {
    $api_key = variable_get('brandfolder_guest_api_key', FALSE);
  }
  if ($api_key) {
    $default_brandfolder = variable_get('brandfolder_default_brandfolder');
    $bf = new Brandfolder($api_key);