Skip to content
Snippets Groups Projects
Commit 186639e2 authored by Nathanael Dewhurst's avatar Nathanael Dewhurst
Browse files

Issue #3478827 by wesleymusgrove, ndewhurst: clarify required API keys and...

Issue #3478827 by wesleymusgrove, ndewhurst: clarify required API keys and avoid config form errors.
parent e7e46ae2
No related branches found
No related tags found
No related merge requests found
......@@ -65,30 +65,6 @@ class BrandfolderSettingsForm extends ConfigFormBase {
$brandfolder_id = $config->get('brandfolder_id');
$bf = brandfolder_api();
if ($config->get('verbose_log_mode')) {
$bf->enableVerboseLogging();
}
$preview_collection_id = $config->get('preview_collection_id');
$none_option_array = ['none' => $this->t('< None >')];
$brandfolders_list = $collections_list = [];
if ($bf) {
$brandfolders_list = $bf->getBrandfolders();
if (!$brandfolders_list) {
$messenger->addMessage($this->t('Please fill in all the requested API keys. You will then be able to select a Brandfolder.'));
}
if ($brandfolder_id) {
$collections_list = $bf->getCollectionsInBrandfolder();
if (!$collections_list) {
$messenger->addMessage($this->t('Could not find collections for the selected Brandfolder.'));
}
}
}
$brandfolders_list = array_merge($none_option_array, $brandfolders_list);
$collections_list = array_merge($none_option_array, $collections_list);
/************************************
* Credentials
************************************/
......@@ -106,6 +82,7 @@ class BrandfolderSettingsForm extends ConfigFormBase {
'#description' => $this->t('An API key for a Brandfolder user who has the "@label" role for the Brandfolder you wish to integrate with your Drupal site. This can be found in Brandfolder under "My Profile > Integrations > API Keys."', ['@label' => $api_key_type_label]),
'#maxlength' => 255,
'#size' => 64,
'#required' => TRUE,
];
if (!empty($api_key)) {
$form['credentials']["brandfolder_api_key_$api_key_type"]['#attributes']['value'] = $this->password_field_preservation_token;
......@@ -116,183 +93,206 @@ class BrandfolderSettingsForm extends ConfigFormBase {
// Collapse it if we have all three.
$form['credentials']['#open'] = (count($api_keys) < 3);
// The rest of the config options become available if we can successfully
// connect to Brandfolder.
if ($bf) {
if ($config->get('verbose_log_mode')) {
$bf->enableVerboseLogging();
}
/************************************
* Basic Configuration
************************************/
$form['basic'] = [
'#type' => 'details',
'#title' => $this->t('Basic Configuration Options'),
'#open' => empty($brandfolder_id),
];
$preview_collection_id = $config->get('preview_collection_id');
$form['basic']['brandfolder_brandfolder_id'] = [
'#type' => 'select',
'#title' => $this->t('Choose a Brandfolder'),
'#options' => $brandfolders_list,
'#default_value' => $brandfolder_id ?? 'none',
'#description' => $this->t('The Brandfolder to integrate with this Drupal site.'),
];
$none_option_array = ['none' => $this->t('< None >')];
$form['basic']['brandfolder_preview_collection'] = [
'#type' => 'select',
'#title' => $this->t('Collection to Preview'),
'#options' => $collections_list,
'#default_value' => $preview_collection_id ?? 'none',
'#description' => $this->t('Choose a collection from which to display sample images. This can help confirm that the integration is successful.'),
];
$brandfolders_list = $bf->getBrandfolders();
if (!$brandfolders_list) {
$messenger->addMessage($this->t('Please fill in all the requested API keys. You will then be able to select a Brandfolder.'));
}
$collections_list = [];
if ($brandfolder_id) {
$collections_list = $bf->getCollectionsInBrandfolder();
if (!$collections_list) {
$messenger->addMessage($this->t('Could not find collections for the selected Brandfolder.'));
}
}
$brandfolders_list = array_merge($none_option_array, $brandfolders_list);
$collections_list = array_merge($none_option_array, $collections_list);
if (isset($bf) && $brandfolder_id) {
/************************************
* Metadata Sync
* Basic Configuration
************************************/
$form['metadata'] = [
$form['basic'] = [
'#type' => 'details',
'#title' => $this->t('Metadata Synchronization'),
];
$form['metadata']['metadata_sync_mode'] = [
'#type' => 'radios',
'#title' => $this->t('Sync Mode'),
'#options' => [
'empties_only' => $this->t('Only update Drupal fields that are empty (default).'),
'indiscriminate_bf_overwrite' => $this->t('Always update Drupal fields when Brandfolder data changes, regardless of whether Drupal fields are empty, have been changed in Drupal, etc. (feature currently in development)'),
'update_non_overridden_fields' => $this->t('When Brandfolder data changes, update all corresponding Drupal fields except those that have been changed (in Drupal) since the last sync (feature currently in development).'),
],
'indiscriminate_bf_overwrite' => ['#disabled' => TRUE],
'update_non_overridden_fields' => ['#disabled' => TRUE],
'#default_value' => $config->get('metadata_sync_mode') ?? 'empties_only',
'#description' => $this->t('Some metadata pertaining to Brandfolder assets can be mapped to corresponding fields/attributes in Drupal. Choose how you want this module to manage that relationship.'),
'#title' => $this->t('Basic Configuration Options'),
'#open' => empty($brandfolder_id),
];
$custom_field_options = $none_option_array;
if ($custom_field_ids_and_names = $bf->listCustomFields(NULL, FALSE, TRUE)) {
$custom_field_options = array_merge($custom_field_options, $custom_field_ids_and_names);
}
$existing_value = $config->get('alt_text_custom_field');
if (empty($existing_value)) {
$existing_value = 'none';
}
$form['metadata']['alt_text_custom_field'] = [
$form['basic']['brandfolder_brandfolder_id'] = [
'#type' => 'select',
'#title' => $this->t('Alt-Text Custom Field'),
'#options' => $custom_field_options,
'#default_value' => $existing_value,
'#description' => $this->t('You can use a custom field in Brandfolder to store alt-text for assets, and Drupal will pull text from that field for use with Brandfolder-sourced images, where applicable. To enable this functionality, select the Brandfolder field you plan to use to store alt-text values.'),
'#title' => $this->t('Choose a Brandfolder'),
'#options' => $brandfolders_list,
'#default_value' => $brandfolder_id ?? 'none',
'#description' => $this->t('The Brandfolder to integrate with this Drupal site.'),
];
/************************************
* Image Optimization
************************************/
$form['image_optimization'] = [
'#type' => 'details',
'#title' => $this->t('Image Optimization'),
'#description' => $this->t('These settings can help reduce image file size. Note that they will be applied to all Brandfolder images throughout your site.'),
$form['basic']['brandfolder_preview_collection'] = [
'#type' => 'select',
'#title' => $this->t('Collection to Preview'),
'#options' => $collections_list,
'#default_value' => $preview_collection_id ?? 'none',
'#description' => $this->t('Choose a collection from which to display sample images. This can help confirm that the integration is successful.'),
];
$form['image_optimization']['io_auto_webp'] = [
'#type' => 'checkbox',
'#title' => $this->t('Automatically use WEBP format for images if supported'),
'#default_value' => (bool) $config->get('io_auto_webp'),
'#description' => $this->t('This will deliver a WEBP version of an image if the user\'s browser supports that format.'),
];
if ($brandfolder_id) {
/************************************
* Metadata Sync
************************************/
$form['metadata'] = [
'#type' => 'details',
'#title' => $this->t('Metadata Synchronization'),
];
$form['image_optimization']['io_quality'] = [
'#type' => 'number',
'#min' => 1,
'#max' => 100,
'#title' => $this->t('Quality to use for all compressed images'),
'#default_value' => $config->get('io_quality') ?? '',
'#description' => $this->t('Choose a value between 1 and 100 (default). A lower value will result in smaller image file sizes (and faster image load time) but also less detail/fidelity. You can experiment to find something that reduces image sizes without too much obvious degradation. Note: this will not be applied to SVG images.'),
];
$form['metadata']['metadata_sync_mode'] = [
'#type' => 'radios',
'#title' => $this->t('Sync Mode'),
'#options' => [
'empties_only' => $this->t('Only update Drupal fields that are empty (default).'),
'indiscriminate_bf_overwrite' => $this->t('Always update Drupal fields when Brandfolder data changes, regardless of whether Drupal fields are empty, have been changed in Drupal, etc. (feature currently in development)'),
'update_non_overridden_fields' => $this->t('When Brandfolder data changes, update all corresponding Drupal fields except those that have been changed (in Drupal) since the last sync (feature currently in development).'),
],
'indiscriminate_bf_overwrite' => ['#disabled' => TRUE],
'update_non_overridden_fields' => ['#disabled' => TRUE],
'#default_value' => $config->get('metadata_sync_mode') ?? 'empties_only',
'#description' => $this->t('Some metadata pertaining to Brandfolder assets can be mapped to corresponding fields/attributes in Drupal. Choose how you want this module to manage that relationship.'),
];
$custom_field_options = $none_option_array;
if ($custom_field_ids_and_names = $bf->listCustomFields(NULL, FALSE, TRUE)) {
$custom_field_options = array_merge($custom_field_options, $custom_field_ids_and_names);
}
/************************************
* Advanced
************************************/
$form['advanced'] = [
'#type' => 'details',
'#title' => $this->t('Advanced Settings'),
];
$existing_value = $config->get('alt_text_custom_field');
if (empty($existing_value)) {
$existing_value = 'none';
}
$form['advanced']['verbose_log_mode'] = [
'#type' => 'checkbox',
'#title' => $this->t('Detailed logging'),
'#default_value' => $config->get('verbose_log_mode'),
'#description' => $this->t('Enable this setting to create log entries for all Brandfolder API queries, incoming webhooks, etc. This can be useful for troubleshooting, but should probably only be enabled for short periods lest it overwhelm your logs.'),
];
$form['metadata']['alt_text_custom_field'] = [
'#type' => 'select',
'#title' => $this->t('Alt-Text Custom Field'),
'#options' => $custom_field_options,
'#default_value' => $existing_value,
'#description' => $this->t('You can use a custom field in Brandfolder to store alt-text for assets, and Drupal will pull text from that field for use with Brandfolder-sourced images, where applicable. To enable this functionality, select the Brandfolder field you plan to use to store alt-text values.'),
];
$form['#attached']['library'][] = 'brandfolder/brandfolder-admin';
/************************************
* Image Optimization
************************************/
$form['image_optimization'] = [
'#type' => 'details',
'#title' => $this->t('Image Optimization'),
'#description' => $this->t('These settings can help reduce image file size. Note that they will be applied to all Brandfolder images throughout your site.'),
];
if ($config->get('verbose_log_mode')) {
foreach ($bf->getLogData() as $log_entry) {
$this->logger('brandfolder')->debug($log_entry);
}
$bf->clearLogData();
}
$form['image_optimization']['io_auto_webp'] = [
'#type' => 'checkbox',
'#title' => $this->t('Automatically use WEBP format for images if supported'),
'#default_value' => (bool) $config->get('io_auto_webp'),
'#description' => $this->t('This will deliver a WEBP version of an image if the user\'s browser supports that format.'),
];
$form['image_optimization']['io_quality'] = [
'#type' => 'number',
'#min' => 1,
'#max' => 100,
'#title' => $this->t('Quality to use for all compressed images'),
'#default_value' => $config->get('io_quality') ?? '',
'#description' => $this->t('Choose a value between 1 and 100 (default). A lower value will result in smaller image file sizes (and faster image load time) but also less detail/fidelity. You can experiment to find something that reduces image sizes without too much obvious degradation. Note: this will not be applied to SVG images.'),
];
/************************************
* Sample Images
************************************/
// Display some images from the selected Brandfolder/collection if
// applicable.
/************************************
* Advanced
************************************/
$form['advanced'] = [
'#type' => 'details',
'#title' => $this->t('Advanced Settings'),
];
$form['sample_image_width'] = [
'#type' => 'number',
'#min' => 16,
'#max' => 1920,
'#title' => $this->t('Sample image width'),
'#default_value' => $config->get('sample_image_width') ?? 400,
'#description' => $this->t('Optionally adjust the width of the sample images below, for testing. The default is 400px.'),
];
$form['advanced']['verbose_log_mode'] = [
'#type' => 'checkbox',
'#title' => $this->t('Detailed logging'),
'#default_value' => $config->get('verbose_log_mode'),
'#description' => $this->t('Enable this setting to create log entries for all Brandfolder API queries, incoming webhooks, etc. This can be useful for troubleshooting, but should probably only be enabled for short periods lest it overwhelm your logs.'),
];
$form['#attached']['library'][] = 'brandfolder/brandfolder-admin';
$params = [
'fields' => 'cdn_url',
'sort_by' => 'updated_at',
'order' => 'desc',
];
if ($preview_collection_id) {
$assets = $bf->listAssets($params, $preview_collection_id);
}
else {
$assets = $bf->listAssets($params);
}
if ($assets) {
$sample_image_width = $config->get('sample_image_width') ?? 400;
$cdn_url_param_string = "width=$sample_image_width";
// Apply image optimization settings to the sample images so users can
// do some basic testing.
if ($config->get('io_auto_webp')) {
$cdn_url_param_string .= '&auto=webp';
}
if ($config->get('io_quality')) {
$cdn_url_param_string .= '&quality=' . $config->get('io_quality');
}
$thumbnails = array_map(function ($asset) use ($cdn_url_param_string) {
$output = '';
$url = $asset->attributes->cdn_url;
if ($url) {
// Strip any query string from the URL.
// @todo: Decide whether/how to merge params (which should take priority in which circumstances, etc.).
$url = preg_replace('/^([^\?]+)\?.*$/', '$1', $url);
$url .= '?' . $cdn_url_param_string;
$output .= "<img src=\"$url\">";
if ($config->get('verbose_log_mode')) {
foreach ($bf->getLogData() as $log_entry) {
$this->logger('brandfolder')->debug($log_entry);
}
$bf->clearLogData();
}
/************************************
* Sample Images
************************************/
// Display some images from the selected Brandfolder/collection if
// applicable.
$form['sample_image_width'] = [
'#type' => 'number',
'#min' => 16,
'#max' => 1920,
'#title' => $this->t('Sample image width'),
'#default_value' => $config->get('sample_image_width') ?? 400,
'#description' => $this->t('Optionally adjust the width of the sample images below, for testing. The default is 400px.'),
];
return $output;
}, $assets->data);
$form['sample_pics'] = [
'#type' => 'markup',
'#prefix' => '<h2>Sample Images</h2>',
'#markup' => '<div class="brandfolder-sample-images">' . implode(' ', $thumbnails) . '</div>',
'#weight' => 999,
$params = [
'fields' => 'cdn_url',
'sort_by' => 'updated_at',
'order' => 'desc',
];
if ($preview_collection_id) {
$assets = $bf->listAssets($params, $preview_collection_id);
}
else {
$assets = $bf->listAssets($params);
}
if ($assets) {
$sample_image_width = $config->get('sample_image_width') ?? 400;
$cdn_url_param_string = "width=$sample_image_width";
// Apply image optimization settings to the sample images so users can
// do some basic testing.
if ($config->get('io_auto_webp')) {
$cdn_url_param_string .= '&auto=webp';
}
if ($config->get('io_quality')) {
$cdn_url_param_string .= '&quality=' . $config->get('io_quality');
}
$thumbnails = array_map(function ($asset) use ($cdn_url_param_string) {
$output = '';
$url = $asset->attributes->cdn_url;
if ($url) {
// Strip any query string from the URL.
// @todo: Decide whether/how to merge params (which should take priority in which circumstances, etc.).
$url = preg_replace('/^([^\?]+)\?.*$/', '$1', $url);
$url .= '?' . $cdn_url_param_string;
$output .= "<img src=\"$url\">";
}
return $output;
}, $assets->data);
$form['sample_pics'] = [
'#type' => 'markup',
'#prefix' => '<h2>Sample Images</h2>',
'#markup' => '<div class="brandfolder-sample-images">' . implode(' ', $thumbnails) . '</div>',
'#weight' => 999,
];
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment