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/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..37dccf21c --- /dev/null +++ b/modules/helfi_media_chart/src/Entity/HelfiChart.php @@ -0,0 +1,47 @@ +get('field_helfi_chart_url') + ?->first() + ?->getString(); + $url_parts = parse_url($chart_url); + return $url_parts['scheme'] . "://" . $url_parts['host']; + } + + /** + * Get the title of chart. + * + * @return string|null + * The title of the chart. + * + * @throws \Drupal\Core\TypedData\Exception\MissingDataException + */ + public function getMediaTitle(): ?string { + $title = (string) $this->get('field_helfi_chart_title') + ?->first() + ?->getString(); + + return empty($title) ? NULL : $title; + } + +} 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 %} 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()); + } + +} 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(); +}