Skip to content

Commit

Permalink
Merge pull request #804 from City-of-Helsinki/UHF-10547
Browse files Browse the repository at this point in the history
UHF-10547 Suunte chat
  • Loading branch information
khalima authored Sep 10, 2024
2 parents f410b1e + 1432776 commit ae51b8d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 30 deletions.
9 changes: 7 additions & 2 deletions helfi_platform_config.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);"
}
}
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 63 additions & 25 deletions src/Plugin/Block/ChatLeijuke.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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'),
],
];

Expand All @@ -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;
}
Expand All @@ -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;
}
}

Expand All @@ -92,7 +130,7 @@ public function build() {
'ext' => $value['type'] ?? FALSE,
];

$chatLibrary['css'][] = $css;
$chat_library['css'][] = $css;
}
}
}
Expand All @@ -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',
],
],
Expand Down

0 comments on commit ae51b8d

Please sign in to comment.