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

Field config UX helpers. Comments. Drupal standards compliance.

Watch for changes to field definitions and field instances that are
relevant to Brandfolder, and prompting users to configure file storage
location (scheme) and field widgets that are compatible with each other.
Update BF admin code for standards compliance.
parent fc0c9337
Loading
Loading
Loading
Loading
+8 −6
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
 * Administration form for Brandfolder.
 */

use Brandfolder\Brandfolder;
use GuzzleHttp\Exception\GuzzleException;

/**
 * Builds the Settings form.
@@ -33,7 +33,7 @@ function brandfolder_settings() {
    }
  }
  $none_options = [
    '_none' => t('<None>')
    '_none' => t('<None>'),
  ];
  $collections_list = array_merge($none_options, $collections_list);

@@ -166,10 +166,12 @@ function brandfolder_settings() {
/**
 * Form validation handler for settings form.
 *
 * @param $form
 * @param $form_state
 * @param array $form
 *   The Drupal form array.
 * @param array $form_state
 *   The Drupal form state array containing values, etc.
 */
function brandfolder_settings_form_validate(&$form, &$form_state) {
function brandfolder_settings_form_validate(array &$form, array &$form_state) {
  $api_key = $form_state['values']['brandfolder_guest_api_key'];
  $api_success = FALSE;
  if (!empty($api_key)) {
@@ -183,7 +185,7 @@ function brandfolder_settings_form_validate(&$form, &$form_state) {
        $api_success = TRUE;
      }
    }
    catch (\Exception $e) {
    catch (GuzzleException $e) {
      $api_success = FALSE;
    }
    if (!$api_success) {
+2 −0
Original line number Diff line number Diff line
@@ -138,4 +138,6 @@ function brandfolder_uninstall() {
    }
  }
  variable_del('brandfolder_image_toolkit_fallback');

  // @todo: Restore/clean up all image fields using "Brandfolder Browser" widget and/or bf:// file scheme.
}
+68 −0
Original line number Diff line number Diff line
@@ -1694,3 +1694,71 @@ function brandfolder_webhook_listener() {
    }
  }
}

/**
 * Implements hook_field_update_instance().
 */
function brandfolder_field_update_instance($instance, $prior_instance) {
  // If this instance's widget was changed to/from the "Brandfolder Browser"
  // widget, check to see if that results in a mismatch with the field's URI
  // scheme. If so, prompt users to make additional changes.
  $old_widget = $prior_instance['widget']['type'];
  $new_widget = $instance['widget']['type'];
  $newly_bf = ($old_widget != 'brandfolder_browser' && $new_widget == 'brandfolder_browser');
  $formerly_bf = ($old_widget == 'brandfolder_browser' && $new_widget != 'brandfolder_browser');
  if ($newly_bf || $formerly_bf) {
    $field_name = $instance['field_name'];
    if ($field = field_read_field($field_name)) {
      if (module_exists('field_ui')) {
        $path = _field_ui_bundle_admin_path($instance['entity_type'], $instance['bundle']) . "/fields/$field_name/field-settings";
        $update = l(t('update'), $path);
      }
      else {
        $update = 'update';
      }
      if ($newly_bf && $field['settings']['uri_scheme'] != 'bf') {
        $message = 'To use the %field_name field with Brandfolder, !update the field settings and choose "Brandfolder (read-only)" as the "File storage location."';
      }
      elseif ($formerly_bf && $field['settings']['uri_scheme'] == 'bf') {
        $message = 'If you no longer wish to use the %field_name field with Brandfolder, !update the field settings and choose a non-Brandfolder option as the "File storage location."';
      }
      if (isset($message)) {
        drupal_set_message(t($message, ['!update' => $update, '%field_name' => $instance['label']]), 'warning');
      }
    }
  }
}

/**
 * Implements hook_field_update_field().
 */
function brandfolder_field_update_field($field, $prior_field, $has_data) {
  // Check to see if the field has just been configured to use the bf:// URI
  // scheme. If so, prompt users to enable the "Brandfolder Browser" widget.
  // Conversely, if this field has just had its URI scheme changed from
  // Brandfolder to something else, prompt users to enable a different widget.
  $old_uri_scheme = $prior_field['settings']['uri_scheme'];
  $new_uri_scheme = $field['settings']['uri_scheme'];
  $newly_bf = ($old_uri_scheme != 'bf' && $new_uri_scheme == 'bf');
  $formerly_bf = ($old_uri_scheme == 'bf' && $new_uri_scheme != 'bf');
  if ($newly_bf || $formerly_bf) {
    // Load all instances of this field and see whether their widgets ought to
    // change.
    $field_name = $field['field_name'];
    $instances = field_read_instances(['field_name' => $field_name]);
    $widgets = array_map(function($instance) {
      return $instance['widget']['type'];
    }, $instances);
    $widgets = array_flip($widgets);
    $field_label = isset($_POST['instance']) && !empty($_POST['instance']['label']) ? $_POST['instance']['label'] : $field_name;
    if ($newly_bf) {
      unset($widgets['brandfolder_browser']);
      if (count($widgets) > 0) {
        drupal_set_message(t('To finish integrating the %field_name field with Brandfolder, you should enable the "Brandfolder Browser" widget wherever this field is used.', ['%field_name' => $field_label]), 'warning');
      }
    }
    elseif ($formerly_bf && isset($widgets['brandfolder_browser'])) {
      drupal_set_message(t('It looks like you no longer want to integrate the %field_name field with Brandfolder. If so, please update the field settings everywhere this field is used and switch to a widget other than the "Brandfolder Browser" widget.', ['%field_name' => $field_label]), 'warning');
    }
  }
}