diff --git a/helfi_platform_config.libraries.yml b/helfi_platform_config.libraries.yml index c59a45676..d0613e5c4 100644 --- a/helfi_platform_config.libraries.yml +++ b/helfi_platform_config.libraries.yml @@ -11,6 +11,9 @@ clear_localstorage: - core/jquery - core/drupal +# This library is loaded via chat_leijuke.js. +# Setting the preprocess to false will not affect this library. +# See: ChatLeijuke.php::build(). genesys_suunte: version: 1.0.2 header: true @@ -24,7 +27,6 @@ genesys_suunte: } assets/js/genesys_suunte.js: { attributes: { - preprocess: false, onload: "javascript:var checkExist = setInterval(function() {if(typeof CXBus != 'undefined') {clearInterval(checkExist);Drupal.behaviors.genesys_suunte.attach();console.log('suunte attaching');}}, 100);" } } @@ -57,10 +59,13 @@ chat_leijuke: version: 1.0.2 header: true js: - assets/js/chat_leijuke.js: {} + assets/js/chat_leijuke.js: { + preprocess: false + } dependencies: - core/drupal - core/drupalSettings + - eu_cookie_compliance/eu_cookie_compliance user_consent_functions: version: 1.0.x diff --git a/phpstan.neon b/phpstan.neon index bd62e77e7..745a4292e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -13,9 +13,6 @@ parameters: - message: '#^Unsafe usage of new static#' path: src/Plugin/Block/ContentBlockBase.php - - - message: '#^\\Drupal calls should be avoided in classes, use dependency injection instead#' - path: src/Plugin/Block/ChatLeijuke.php - message: '#^\\Drupal calls should be avoided in classes, use dependency injection instead#' path: src/Commands/MajorUpdateCommands.php diff --git a/src/Plugin/Block/ChatLeijuke.php b/src/Plugin/Block/ChatLeijuke.php index 34dd7e5b6..853e376db 100644 --- a/src/Plugin/Block/ChatLeijuke.php +++ b/src/Plugin/Block/ChatLeijuke.php @@ -3,24 +3,66 @@ namespace Drupal\helfi_platform_config\Plugin\Block; use Drupal\Component\Utility\Xss; +use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Yaml\Yaml; /** * Provides a Chat Leijuke block. - * - * @Block( - * id = "chat_leijuke", - * admin_label = @Translation("Chat Leijuke"), - * ) */ -class ChatLeijuke extends BlockBase { +#[Block( + id: "chat_leijuke", + admin_label: new TranslatableMarkup('Chat Leijuke'), +)] +final class ChatLeijuke extends BlockBase implements ContainerFactoryPluginInterface { + + /** + * Constructs a Chat Leijuke Block object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Extension\ModuleExtensionList $moduleList + * The module extension list. + * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory + * The config factory. + */ + public function __construct( + array $configuration, + string $plugin_id, + mixed $plugin_definition, + protected ModuleExtensionList $moduleList, + protected ConfigFactoryInterface $configFactory, + ) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { + return new self( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('extension.list.module'), + $container->get('config.factory'), + ); + } /** * {@inheritdoc} */ - public function blockForm($form, FormStateInterface $form_state) { + public function blockForm($form, FormStateInterface $form_state): array { $form = parent::blockForm($form, $form_state); $config = $this->getConfiguration(); @@ -30,7 +72,7 @@ public function blockForm($form, FormStateInterface $form_state) { '#description' => $this->t('Choose the approriate chat/bot provider?'), '#default_value' => $config['chat_selection'] ?? '', '#options' => [ - 'genesys_suunte' => 'Genesys SUUNTE', + 'genesys_suunte' => $this->t('Genesys SUUNTE'), ], ]; @@ -46,28 +88,24 @@ public function blockForm($form, FormStateInterface $form_state) { /** * {@inheritdoc} */ - public function blockSubmit($form, FormStateInterface $formState) { - $this->configuration['chat_selection'] = $formState->getValue('chat_selection'); - $this->configuration['chat_title'] = $formState->getValue('chat_title'); + public function blockSubmit($form, FormStateInterface $form_state): void { + $this->configuration['chat_selection'] = $form_state->getValue('chat_selection'); + $this->configuration['chat_title'] = $form_state->getValue('chat_title'); } /** * {@inheritdoc} */ - public function build() { + public function build(): array { $library = ['helfi_platform_config/chat_leijuke']; $config = $this->getConfiguration(); $build = []; - $chatLibrary = []; - // @todo Use dependency injection. - // phpcs:ignore DrupalPractice.Objects.GlobalDrupal.GlobalDrupal - $modulePath = \Drupal::service('extension.list.module')->getPath('helfi_platform_config'); - // phpcs:ignore DrupalPractice.Objects.GlobalDrupal.GlobalDrupal - $assetPath = \Drupal::config('helfi_proxy.settings')->get('asset_path'); - - $librariesYml = Yaml::parseFile($modulePath . '/helfi_platform_config.libraries.yml'); + $chat_library = []; + $module_path = $this->moduleList->getPath('helfi_platform_config'); + $asset_path = $this->configFactory->get('helfi_proxy.settings')->get('asset_path'); + $libraries_yaml = Yaml::parseFile($module_path . '/helfi_platform_config.libraries.yml'); - foreach ($librariesYml as $library_name => $library_configuration) { + foreach ($libraries_yaml as $library_name => $library_configuration) { if ($library_name !== $config['chat_selection']) { continue; } @@ -81,7 +119,7 @@ public function build() { 'async' => $value['attributes']['async'] ?? FALSE, 'data_container_id' => $value['attributes']['data-container-id'] ?? FALSE, ]; - $chatLibrary['js'][] = $js; + $chat_library['js'][] = $js; } } @@ -92,7 +130,7 @@ public function build() { 'ext' => $value['type'] ?? FALSE, ]; - $chatLibrary['css'][] = $css; + $chat_library['css'][] = $css; } } } @@ -107,8 +145,8 @@ public function build() { 'leijuke_data' => [ $config['chat_selection'] => [ 'name' => $config['chat_selection'], - 'libraries' => $chatLibrary, - 'modulepath' => $assetPath . '/' . $modulePath, + 'libraries' => $chat_library, + 'modulepath' => $asset_path . '/' . $module_path, 'title' => $config['chat_title'] ? Xss::filter($config['chat_title']) : 'Chat', ], ],