Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-4979: Make sure proxy path is configured #36

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions helfi_proxy.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\editor\Entity\Editor;
use Drupal\helfi_proxy\ProxyManagerInterface;

/**
* Converts uri to absolute.
Expand All @@ -21,7 +22,10 @@ function helfi_proxy_absolute_url(string $uri) : string {
static $relative_path = '';

if (!$relative_path) {
$relative_path = \Drupal::service('helfi_proxy.proxy_manager')->getConfig('asset_path');
/** @var \Drupal\helfi_proxy\ProxyManagerInterface $service */
$service = \Drupal::service('helfi_proxy.proxy_manager');
$relative_path = $service
->getConfig(ProxyManagerInterface::ASSET_PATH);
}
/** @var \Drupal\Core\File\FileUrlGenerator $fileGenerator */
$fileGenerator = \Drupal::service('file_url_generator');
Expand All @@ -30,13 +34,33 @@ function helfi_proxy_absolute_url(string $uri) : string {
);
}

/**
* Checks if proxy is configured.
*
* @return bool
* TRUE if proxy should be used.
*/
function helfi_proxy_is_configured() : bool {
static $isConfigured = NULL;

if ($isConfigured === NULL) {
/** @var \Drupal\helfi_proxy\ProxyManagerInterface $service */
$service = \Drupal::service('helfi_proxy.proxy_manager');
$isConfigured = $service->isConfigured(ProxyManagerInterface::ASSET_PATH);
}
return $isConfigured;
}

/**
* Implements hook_js_settings_alter().
*/
function helfi_proxy_js_settings_alter(
array &$settings,
AttachedAssetsInterface $assets
) {
if (!helfi_proxy_is_configured()) {
return;
}
foreach (['designSelect', 'paragraphSelect'] as $type) {
if (!isset($settings[$type]['pathToImages'])) {
continue;
Expand All @@ -56,7 +80,7 @@ function helfi_proxy_js_settings_alter(
* Implements hook_editor_js_settings_alter().
*/
function helfi_proxy_editor_js_settings_alter(array &$settings) {
if (!\Drupal::moduleHandler()->moduleExists('ckeditor')) {
if (!\Drupal::moduleHandler()->moduleExists('ckeditor') || !helfi_proxy_is_configured()) {
return;
}
$ckeditor_plugin_manager = \Drupal::service('plugin.manager.ckeditor.plugin');
Expand All @@ -66,7 +90,7 @@ function helfi_proxy_editor_js_settings_alter(array &$settings) {
$editor = Editor::load($format);
// @see \Drupal\ckeditor\Plugin\Editor\CKEditor::getJSSettings()
$external_plugin_files = $ckeditor_plugin_manager->getEnabledPluginFiles($editor);
// Convert editor css and plugin js to use absolute path.
// Convert editor css and plugin js to use absolute asset path.
$format_settings['editorSettings']['drupalExternalPlugins'] = array_map('helfi_proxy_absolute_url', $external_plugin_files);
$format_settings['editorSettings']['contentsCss'] = array_map('helfi_proxy_absolute_url', $format_settings['editorSettings']['contentsCss'] ?? []);
}
Expand Down