Skip to content
Snippets Groups Projects
Commit 2ec5b9ee authored by Ivica Puljic's avatar Ivica Puljic
Browse files

Merge branch '3492782-drupal-11-compatibility' into 2.x

parents 949974d6 861a412e
No related branches found
No related tags found
1 merge request!3Add D11 compatibility fixes
...@@ -3,9 +3,13 @@ ...@@ -3,9 +3,13 @@
namespace Drupal\umami_analytics\Form; namespace Drupal\umami_analytics\Form;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\umami_analytics\JavascriptLocalCache; use Drupal\umami_analytics\JavascriptLocalCache;
use Drupal\user\RoleInterface;
use Drupal\user\Entity\Role;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
...@@ -28,8 +32,8 @@ class SettingsForm extends ConfigFormBase { ...@@ -28,8 +32,8 @@ class SettingsForm extends ConfigFormBase {
* @param JavascriptLocalCache $umami_analytics_javascript * @param JavascriptLocalCache $umami_analytics_javascript
* The JS Local Cache service. * The JS Local Cache service.
*/ */
public function __construct(ConfigFactoryInterface $config_factory, JavascriptLocalCache $umami_analytics_javascript) { public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typedConfigManager, JavascriptLocalCache $umami_analytics_javascript) {
parent::__construct($config_factory); parent::__construct($config_factory, $typedConfigManager);
$this->uaJavascript = $umami_analytics_javascript; $this->uaJavascript = $umami_analytics_javascript;
} }
...@@ -39,6 +43,7 @@ class SettingsForm extends ConfigFormBase { ...@@ -39,6 +43,7 @@ class SettingsForm extends ConfigFormBase {
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('config.factory'), $container->get('config.factory'),
$container->get('config.typed'),
$container->get('umami_analytics.javascript_cache') $container->get('umami_analytics.javascript_cache')
); );
} }
...@@ -179,11 +184,14 @@ class SettingsForm extends ConfigFormBase { ...@@ -179,11 +184,14 @@ class SettingsForm extends ConfigFormBase {
'#default_value' => $config->get('visibility.user_role_mode'), '#default_value' => $config->get('visibility.user_role_mode'),
]; ];
$visibility_user_role_roles = $config->get('visibility.user_role_roles'); $visibility_user_role_roles = $config->get('visibility.user_role_roles');
$roles = Role::loadMultiple();
$role_names = array_map(fn(RoleInterface $role) => Html::escape($role->label()), $roles);
$form['tracking']['role_visibility_settings']['visibility_user_role_roles'] = [ $form['tracking']['role_visibility_settings']['visibility_user_role_roles'] = [
'#type' => 'checkboxes', '#type' => 'checkboxes',
'#title' => $this->t('Roles'), '#title' => $this->t('Roles'),
'#default_value' => !empty($visibility_user_role_roles) ? $visibility_user_role_roles : [], '#default_value' => !empty($visibility_user_role_roles) ? $visibility_user_role_roles : [],
'#options' => array_map('\Drupal\Component\Utility\Html::escape', user_role_names()), '#options' => $role_names,
'#description' => $this->t('If none of the roles are selected, all users will be tracked. If a user has any of the roles checked, that user will be tracked (or excluded, depending on the setting above).'), '#description' => $this->t('If none of the roles are selected, all users will be tracked. If a user has any of the roles checked, that user will be tracked (or excluded, depending on the setting above).'),
]; ];
......
...@@ -3,13 +3,17 @@ ...@@ -3,13 +3,17 @@
namespace Drupal\umami_analytics; namespace Drupal\umami_analytics;
use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Crypt;
use Drupal\Core\Asset\AssetQueryStringInterface;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileExists;
use Drupal\Core\File\FileSystemInterface; use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\State\StateInterface; use Drupal\Core\State\StateInterface;
use Drupal\Core\Utility\Error;
use GuzzleHttp\ClientInterface; use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\GuzzleException;
/** /**
* Javascript local cache helper service class. * Javascript local cache helper service class.
...@@ -23,6 +27,13 @@ class JavascriptLocalCache { ...@@ -23,6 +27,13 @@ class JavascriptLocalCache {
*/ */
protected FileSystemInterface $fileSystem; protected FileSystemInterface $fileSystem;
/**
* The file url generator service.
*
* @var FileUrlGeneratorInterface
*/
protected FileUrlGeneratorInterface $fileUrlGenerator;
/** /**
* The configuration factory. * The configuration factory.
* *
...@@ -51,14 +62,24 @@ class JavascriptLocalCache { ...@@ -51,14 +62,24 @@ class JavascriptLocalCache {
*/ */
protected StateInterface $state; protected StateInterface $state;
/**
* The asset query string service.
*
* @var AssetQueryStringInterface
*/
protected AssetQueryStringInterface $assetQueryString;
/** /**
* The construct. * The construct.
*/ */
public function __construct(ClientInterface $http_client, FileSystemInterface $file_system, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, StateInterface $state) { public function __construct(ClientInterface $http_client, FileSystemInterface $file_system, FileUrlGeneratorInterface $file_url_generator, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, StateInterface $state, AssetQueryStringInterface $asset_query_string) {
$this->httpClient = $http_client; $this->httpClient = $http_client;
$this->fileSystem = $file_system; $this->fileSystem = $file_system;
$this->fileUrlGenerator = $file_url_generator;
$this->configFactory = $config_factory; $this->configFactory = $config_factory;
$this->state = $state; $this->state = $state;
$this->assetQueryString = $asset_query_string;
$this->logger = $logger_factory->get('umami_analytics'); $this->logger = $logger_factory->get('umami_analytics');
} }
...@@ -96,16 +117,16 @@ class JavascriptLocalCache { ...@@ -96,16 +117,16 @@ class JavascriptLocalCache {
// Check that the file's directory is writable. // Check that the file's directory is writable.
if ($data_hash_local != $data_hash_remote && $this->fileSystem->prepareDirectory($path)) { if ($data_hash_local != $data_hash_remote && $this->fileSystem->prepareDirectory($path)) {
// Save updated tracking code file to disk. // Save updated tracking code file to disk.
$this->fileSystem->saveData($data, $file_destination, FileSystemInterface::EXISTS_REPLACE); $this->fileSystem->saveData($data, $file_destination, FileExists::Replace);
// Based on Drupal Core class AssetDumper. // Based on Drupal Core class AssetDumper.
if (extension_loaded('zlib') && $this->configFactory->get('system.performance')->get('js.gzip')) { if (extension_loaded('zlib') && $this->configFactory->get('system.performance')->get('js.gzip')) {
$this->fileSystem->saveData(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FileSystemInterface::EXISTS_REPLACE); $this->fileSystem->saveData(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FileExists::Replace);
} }
$this->logger->info('Locally cached tracking code file has been updated.'); $this->logger->info('Locally cached tracking code file has been updated.');
// Change query-strings on css/js files to enforce reload for all // Change query-strings on css/js files to enforce reload for all
// users. // users.
_drupal_flush_css_js(); $this->assetQueryString->reset();
} }
} }
else { else {
...@@ -116,20 +137,20 @@ class JavascriptLocalCache { ...@@ -116,20 +137,20 @@ class JavascriptLocalCache {
$this->fileSystem->saveData($data, $file_destination, FileSystemInterface::EXISTS_REPLACE); $this->fileSystem->saveData($data, $file_destination, FileSystemInterface::EXISTS_REPLACE);
// Based on Drupal Core class AssetDumper. // Based on Drupal Core class AssetDumper.
if (extension_loaded('zlib') && $this->configFactory->get('system.performance')->get('js.gzip')) { if (extension_loaded('zlib') && $this->configFactory->get('system.performance')->get('js.gzip')) {
$this->fileSystem->saveData(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FileSystemInterface::EXISTS_REPLACE); $this->fileSystem->saveData(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FileExists::Replace);
} }
$this->logger->info('Locally cached tracking code file has been saved.'); $this->logger->info('Locally cached tracking code file has been saved.');
} }
} }
} }
catch (RequestException $exception) { catch (GuzzleException $exception) {
watchdog_exception('umami_analytics', $exception); Error::logException($this->logger, $exception);
return $remote_url; return $remote_url;
} }
} }
// Return the local JS file path. // Return the local JS file path.
$query_string = '?' . ($this->state->get('system.css_js_query_string') ?: '0'); $query_string = '?' . ($this->state->get('system.css_js_query_string') ?: '0');
return \Drupal::service('file_url_generator')->generateString($file_destination) . $query_string; return $this->fileUrlGenerator->generateString($file_destination) . $query_string;
} }
/** /**
...@@ -141,7 +162,7 @@ class JavascriptLocalCache { ...@@ -141,7 +162,7 @@ class JavascriptLocalCache {
$this->fileSystem->deleteRecursive($path); $this->fileSystem->deleteRecursive($path);
// Change query-strings on css/js files to enforce reload for all users. // Change query-strings on css/js files to enforce reload for all users.
_drupal_flush_css_js(); $this->assetQueryString->reset();
$this->logger->info('Local Umami Analytics file cache has been purged.'); $this->logger->info('Local Umami Analytics file cache has been purged.');
} }
......
...@@ -2,5 +2,5 @@ name: 'Umami Analytics' ...@@ -2,5 +2,5 @@ name: 'Umami Analytics'
type: module type: module
description: 'Allows your site to be tracked by Umami by adding a Javascript tracking code to every page.' description: 'Allows your site to be tracked by Umami by adding a Javascript tracking code to every page.'
package: Statistics package: Statistics
core_version_requirement: ^9.3 || ^10 core_version_requirement: ^10 || ^11
configure: umami_analytics.admin_settings_form configure: umami_analytics.admin_settings_form
...@@ -4,4 +4,4 @@ services: ...@@ -4,4 +4,4 @@ services:
arguments: [ '@config.factory', '@path_alias.manager', '@path.matcher', '@path.current' ] arguments: [ '@config.factory', '@path_alias.manager', '@path.matcher', '@path.current' ]
umami_analytics.javascript_cache: umami_analytics.javascript_cache:
class: Drupal\umami_analytics\JavascriptLocalCache class: Drupal\umami_analytics\JavascriptLocalCache
arguments: [ '@http_client', '@file_system', '@config.factory', '@logger.factory', '@state' ] arguments: [ '@http_client', '@file_system', '@file_url_generator', '@config.factory', '@logger.factory', '@state', '@asset.query_string' ]
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