From d8aa5eef45049c15eac20040c36b338427e11ca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Wed, 5 Feb 2025 09:10:42 +0200 Subject: [PATCH 1/7] UHF-11329: Added HelfiChart bundle class and applied it to the helfi_media_chart. --- .../helfi_media_chart.module | 10 +++++ .../src/Entity/HelfiChart.php | 43 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 modules/helfi_media_chart/src/Entity/HelfiChart.php diff --git a/modules/helfi_media_chart/helfi_media_chart.module b/modules/helfi_media_chart/helfi_media_chart.module index 7dc2ac8c0..0daacd870 100644 --- a/modules/helfi_media_chart/helfi_media_chart.module +++ b/modules/helfi_media_chart/helfi_media_chart.module @@ -9,6 +9,16 @@ declare(strict_types=1); use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\Entity\BaseFieldOverride; +use Drupal\helfi_media_chart\Entity\HelfiChart; + +/** + * Implements hook_entity_bundle_info_alter(). + */ +function helfi_media_chart_entity_bundle_info_alter(array &$bundles): void { + if (isset($bundles['media']['helfi_chart'])) { + $bundles['media']['helfi_chart']['class'] = HelfiChart::class; + } +} /** * Implements hook_entity_bundle_field_info_alter(). diff --git a/modules/helfi_media_chart/src/Entity/HelfiChart.php b/modules/helfi_media_chart/src/Entity/HelfiChart.php new file mode 100644 index 000000000..691ef0703 --- /dev/null +++ b/modules/helfi_media_chart/src/Entity/HelfiChart.php @@ -0,0 +1,43 @@ +get('field_helfi_chart_url') + ?->first() + ?->getString(); + $url_parts = parse_url($chart_url); + return $url_parts['scheme'] . "://" . $url_parts['host']; + } + + /** + * Get the title of map. + * + * @return string|null + * The title of the map. + */ + public function getMediaTitle(): ?string { + $title = (string) $this->get('field_helfi_chart_title') + ?->first() + ?->getString(); + + return empty($title) ? NULL : $title; + } + +} From 37c316094dc4c696fbac5e472bc6af90f92f202a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Wed, 5 Feb 2025 09:33:03 +0200 Subject: [PATCH 2/7] UHF-11329: Removed obsolete chart-iframe template. --- modules/helfi_media_chart/helfi_media_chart.module | 8 -------- .../helfi_media_chart/templates/chart-iframe.html.twig | 3 --- 2 files changed, 11 deletions(-) delete mode 100644 modules/helfi_media_chart/templates/chart-iframe.html.twig diff --git a/modules/helfi_media_chart/helfi_media_chart.module b/modules/helfi_media_chart/helfi_media_chart.module index 0daacd870..970abad6a 100644 --- a/modules/helfi_media_chart/helfi_media_chart.module +++ b/modules/helfi_media_chart/helfi_media_chart.module @@ -58,14 +58,6 @@ function helfi_media_chart_entity_bundle_field_info(EntityTypeInterface $entity_ */ function helfi_media_chart_theme($existing, $type, $theme, $path) : array { return [ - 'chart_iframe' => [ - 'variables' => [ - 'title' => NULL, - 'url' => NULL, - 'domain' => NULL, - ], - 'template' => 'chart-iframe', - ], 'chart_iframe__media_library' => [ 'variables' => [ 'title' => NULL, diff --git a/modules/helfi_media_chart/templates/chart-iframe.html.twig b/modules/helfi_media_chart/templates/chart-iframe.html.twig deleted file mode 100644 index 62ce37741..000000000 --- a/modules/helfi_media_chart/templates/chart-iframe.html.twig +++ /dev/null @@ -1,3 +0,0 @@ -{% if url and title %} - -{% endif %} From f4ee1edacd9a21265b75502fdac2ee8b97751372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Wed, 5 Feb 2025 09:43:42 +0200 Subject: [PATCH 3/7] Revert "UHF-11329: Removed obsolete chart-iframe template." This reverts commit 37c316094dc4c696fbac5e472bc6af90f92f202a. --- modules/helfi_media_chart/helfi_media_chart.module | 8 ++++++++ .../helfi_media_chart/templates/chart-iframe.html.twig | 3 +++ 2 files changed, 11 insertions(+) create mode 100644 modules/helfi_media_chart/templates/chart-iframe.html.twig diff --git a/modules/helfi_media_chart/helfi_media_chart.module b/modules/helfi_media_chart/helfi_media_chart.module index 970abad6a..0daacd870 100644 --- a/modules/helfi_media_chart/helfi_media_chart.module +++ b/modules/helfi_media_chart/helfi_media_chart.module @@ -58,6 +58,14 @@ function helfi_media_chart_entity_bundle_field_info(EntityTypeInterface $entity_ */ function helfi_media_chart_theme($existing, $type, $theme, $path) : array { return [ + 'chart_iframe' => [ + 'variables' => [ + 'title' => NULL, + 'url' => NULL, + 'domain' => NULL, + ], + 'template' => 'chart-iframe', + ], 'chart_iframe__media_library' => [ 'variables' => [ 'title' => NULL, diff --git a/modules/helfi_media_chart/templates/chart-iframe.html.twig b/modules/helfi_media_chart/templates/chart-iframe.html.twig new file mode 100644 index 000000000..62ce37741 --- /dev/null +++ b/modules/helfi_media_chart/templates/chart-iframe.html.twig @@ -0,0 +1,3 @@ +{% if url and title %} + +{% endif %} From feccd8e3bb6258b61629a5cbd3857bd409907ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Wed, 5 Feb 2025 10:16:02 +0200 Subject: [PATCH 4/7] UHF-11329: Removed cookie category custom functionality in favour of embedded-content-cookie-compliance.js. --- .../js/helfi-charts-cookie-compliance.js | 35 -------- ...view_display.media.helfi_chart.default.yml | 9 +- .../helfi_media_chart.install | 4 +- .../helfi_media_chart.libraries.yml | 7 -- .../Field/FieldFormatter/ChartFormatter.php | 84 ------------------- .../templates/chart-iframe.html.twig | 3 - 6 files changed, 9 insertions(+), 133 deletions(-) delete mode 100644 modules/helfi_media_chart/assets/js/helfi-charts-cookie-compliance.js delete mode 100644 modules/helfi_media_chart/helfi_media_chart.libraries.yml delete mode 100644 modules/helfi_media_chart/src/Plugin/Field/FieldFormatter/ChartFormatter.php delete mode 100644 modules/helfi_media_chart/templates/chart-iframe.html.twig diff --git a/modules/helfi_media_chart/assets/js/helfi-charts-cookie-compliance.js b/modules/helfi_media_chart/assets/js/helfi-charts-cookie-compliance.js deleted file mode 100644 index e284fc749..000000000 --- a/modules/helfi_media_chart/assets/js/helfi-charts-cookie-compliance.js +++ /dev/null @@ -1,35 +0,0 @@ -/** - * @file - * Load chart once the user has approved the required cookie category. - */ -(function ($, Drupal) { - 'use strict'; - - let loadHelfiCharts = () => { - if (Drupal.cookieConsent.getConsentStatus(['statistics'])) { - const chartContentElements = document.querySelectorAll('.helfi-charts-content'); - - // Populate all chart content elements with iframes on page - for (let i = 0; i < chartContentElements.length; ++i) { - if (chartContentElements[i].dataset && chartContentElements[i].dataset.src && chartContentElements[i].dataset.title) { - const iframeElement = document.createElement('iframe'); - iframeElement.src = chartContentElements[i].dataset.src; - iframeElement.title = chartContentElements[i].dataset.title; - iframeElement.allow = 'fullscreen'; - chartContentElements[i].replaceChildren(iframeElement); - } - } - } else { - $('.js-helfi-charts-cookie-compliance').show(); - } - - // Only load once. - loadHelfiCharts = function () {}; - }; - - if (Drupal.cookieConsent.initialized()) { - loadHelfiCharts(); - } else { - Drupal.cookieConsent.loadFunction(loadHelfiCharts); - } -})(jQuery, Drupal); diff --git a/modules/helfi_media_chart/config/install/core.entity_view_display.media.helfi_chart.default.yml b/modules/helfi_media_chart/config/install/core.entity_view_display.media.helfi_chart.default.yml index 2d084fe6b..efad804f7 100644 --- a/modules/helfi_media_chart/config/install/core.entity_view_display.media.helfi_chart.default.yml +++ b/modules/helfi_media_chart/config/install/core.entity_view_display.media.helfi_chart.default.yml @@ -31,9 +31,14 @@ content: weight: 2 region: content field_helfi_chart_url: - type: helfi_chart + type: link label: hidden - settings: { } + settings: + trim_length: 80 + url_only: true + url_plain: false + rel: '' + target: '' third_party_settings: { } weight: 1 region: content diff --git a/modules/helfi_media_chart/helfi_media_chart.install b/modules/helfi_media_chart/helfi_media_chart.install index b81681c3b..c214d2ad3 100644 --- a/modules/helfi_media_chart/helfi_media_chart.install +++ b/modules/helfi_media_chart/helfi_media_chart.install @@ -50,9 +50,9 @@ function helfi_media_chart_install($is_syncing) : void { } /** - * UHF-9088 Updated translations for media chart. + * UHF-11329 Removed ChartFormatter and use Link formatter instead. */ -function helfi_media_chart_update_9002(): void { +function helfi_media_chart_update_9003(): void { \Drupal::service('helfi_platform_config.config_update_helper') ->update('helfi_media_chart'); } diff --git a/modules/helfi_media_chart/helfi_media_chart.libraries.yml b/modules/helfi_media_chart/helfi_media_chart.libraries.yml deleted file mode 100644 index 8204c0962..000000000 --- a/modules/helfi_media_chart/helfi_media_chart.libraries.yml +++ /dev/null @@ -1,7 +0,0 @@ -helfi_charts: - version: 1.0.0 - js: - assets/js/helfi-charts-cookie-compliance.js: {} - dependencies: - - core/jquery - - core/drupal diff --git a/modules/helfi_media_chart/src/Plugin/Field/FieldFormatter/ChartFormatter.php b/modules/helfi_media_chart/src/Plugin/Field/FieldFormatter/ChartFormatter.php deleted file mode 100644 index 397f48c61..000000000 --- a/modules/helfi_media_chart/src/Plugin/Field/FieldFormatter/ChartFormatter.php +++ /dev/null @@ -1,84 +0,0 @@ -logger = $container->get('logger.factory')->get('helfi_chart'); - return $instance; - } - - /** - * {@inheritdoc} - */ - public function viewElements(FieldItemListInterface $items, $langcode) :array { - $element = []; - $entity = $items->getEntity(); - - foreach ($items as $delta => $item) { - ['uri' => $uri] = $item->getValue(); - - try { - $url = $this->mediaUrlToUri($uri); - } - catch (\InvalidArgumentException $e) { - Error::logException($this->logger, $e); - continue; - } - $element[$delta] = [ - '#theme' => 'chart_iframe', - '#title' => $entity->get('field_helfi_chart_title')->value, - '#url' => (string) $url, - '#domain' => $url->getHost(), - '#attached' => [ - 'library' => [ - 'helfi_media_chart/helfi_charts', - ], - ], - ]; - } - - return $element; - } - -} diff --git a/modules/helfi_media_chart/templates/chart-iframe.html.twig b/modules/helfi_media_chart/templates/chart-iframe.html.twig deleted file mode 100644 index 62ce37741..000000000 --- a/modules/helfi_media_chart/templates/chart-iframe.html.twig +++ /dev/null @@ -1,3 +0,0 @@ -{% if url and title %} - -{% endif %} From c0844386b96408153c9ac9dd995923bdc2a5b866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Wed, 5 Feb 2025 12:11:46 +0200 Subject: [PATCH 5/7] UHF-11329: Added test for the HelfiChart. --- .../src/Entity/HelfiChart.php | 4 + .../src/Kernel/Entity/HelfiChartTest.php | 81 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 modules/helfi_media_chart/tests/src/Kernel/Entity/HelfiChartTest.php diff --git a/modules/helfi_media_chart/src/Entity/HelfiChart.php b/modules/helfi_media_chart/src/Entity/HelfiChart.php index 691ef0703..56870c9c6 100644 --- a/modules/helfi_media_chart/src/Entity/HelfiChart.php +++ b/modules/helfi_media_chart/src/Entity/HelfiChart.php @@ -17,6 +17,8 @@ class HelfiChart extends MediaEntityBundle implements MediaInterface { * * @return string * Url of the service provider. + * + * @throws \Drupal\Core\TypedData\Exception\MissingDataException */ public function getServiceUrl(): string { $chart_url = $this->get('field_helfi_chart_url') @@ -31,6 +33,8 @@ public function getServiceUrl(): string { * * @return string|null * The title of the map. + * + * @throws \Drupal\Core\TypedData\Exception\MissingDataException */ public function getMediaTitle(): ?string { $title = (string) $this->get('field_helfi_chart_title') diff --git a/modules/helfi_media_chart/tests/src/Kernel/Entity/HelfiChartTest.php b/modules/helfi_media_chart/tests/src/Kernel/Entity/HelfiChartTest.php new file mode 100644 index 000000000..08c848e03 --- /dev/null +++ b/modules/helfi_media_chart/tests/src/Kernel/Entity/HelfiChartTest.php @@ -0,0 +1,81 @@ +installConfig(['system', 'media', 'media_library']); + $this->installEntitySchema('user'); + $this->installEntitySchema('media'); + $this->installEntitySchema('file'); + $this->installSchema('file', ['file_usage']); + $this->installConfig('helfi_media_chart'); + } + + /** + * Tests Helfi Chart bundle class. + */ + public function testBundleClass() : void { + /** @var \Drupal\media\MediaStorage $storage */ + $storage = $this->container->get('entity_type.manager') + ->getStorage('media'); + + $data = [ + 'uri' => 'https://playground.powerbi.com/sampleReportEmbed', + ]; + + $entity = $storage->create([ + 'name' => 'test', + 'bundle' => 'helfi_chart', + 'field_helfi_chart_url' => $data, + ]); + $entity->save(); + $this->assertInstanceOf(HelfiChart::class, $entity); + + $this->assertEquals('https://playground.powerbi.com', $entity->getServiceUrl()); + $this->assertNull($entity->getMediaTitle()); + + $entity->set('field_helfi_chart_title', 'Test title'); + $entity->save(); + + $this->assertEquals('Test title', $entity->getMediaTitle()); + } + +} From 6d1d6d12eba132e3a8ace3b0dc566debbaa3b332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Wed, 5 Feb 2025 12:13:03 +0200 Subject: [PATCH 6/7] UHF-11329: Configure HDBT cookie banner module when test content module is being installed. --- .../helfi_test_content.info.yml | 1 + .../helfi_test_content.install | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 modules/helfi_test_content/helfi_test_content.install diff --git a/modules/helfi_test_content/helfi_test_content.info.yml b/modules/helfi_test_content/helfi_test_content.info.yml index 57ddd62d0..43974803f 100644 --- a/modules/helfi_test_content/helfi_test_content.info.yml +++ b/modules/helfi_test_content/helfi_test_content.info.yml @@ -4,6 +4,7 @@ core_version_requirement: ^9 || ^10 package: HELfi dependencies: - default_content:default_content + - helfi_platform_config:hdbt_cookie_banner - helfi_platform_config:helfi_calculator - helfi_platform_config:helfi_etusivu_entities - helfi_platform_config:helfi_paragraphs_image_gallery diff --git a/modules/helfi_test_content/helfi_test_content.install b/modules/helfi_test_content/helfi_test_content.install new file mode 100644 index 000000000..54b01f000 --- /dev/null +++ b/modules/helfi_test_content/helfi_test_content.install @@ -0,0 +1,46 @@ +moduleExists('hdbt_cookie_banner')) { + return; + } + + // Install the default configuration for the hdbt_cookie_banner module. + $config_factory = \Drupal::configFactory(); + $module_path = \Drupal::service('extension.list.module') + ->getPath('hdbt_cookie_banner'); + $json_file_path = $module_path . '/assets/json/siteSettingsTemplate.json'; + + try { + $json_content = file_get_contents($json_file_path); + } + catch (\Throwable $e) { + return; + } + + $config = $config_factory->getEditable('hdbt_cookie_banner.settings'); + $config + ->set('use_custom_settings', TRUE) + ->set('site_settings', $json_content) + ->save(); +} From 7d94ada5e58659eabe65eb13dc0f1392ae2df231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Kalij=C3=A4rvi?= Date: Wed, 5 Feb 2025 14:46:30 +0200 Subject: [PATCH 7/7] Update HelfiChart.php --- modules/helfi_media_chart/src/Entity/HelfiChart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/helfi_media_chart/src/Entity/HelfiChart.php b/modules/helfi_media_chart/src/Entity/HelfiChart.php index 56870c9c6..37dccf21c 100644 --- a/modules/helfi_media_chart/src/Entity/HelfiChart.php +++ b/modules/helfi_media_chart/src/Entity/HelfiChart.php @@ -29,10 +29,10 @@ public function getServiceUrl(): string { } /** - * Get the title of map. + * Get the title of chart. * * @return string|null - * The title of the map. + * The title of the chart. * * @throws \Drupal\Core\TypedData\Exception\MissingDataException */