Skip to content

Commit

Permalink
Merge pull request #773 from City-of-Helsinki/UHF-9708
Browse files Browse the repository at this point in the history
UHF-9708: Survey content type
  • Loading branch information
hyrsky authored Aug 12, 2024
2 parents 97282ce + c2c5619 commit 10ca013
Show file tree
Hide file tree
Showing 62 changed files with 2,095 additions and 752 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This repository holds configuration for the Hel.fi platform.
- [Translations in Helfi Platform](documentation/translations.md)
- [Update instructions (2.x to 3.x)](documentation/update.md)
- [Two-factor authentication/TFA/MFA](/modules/helfi_tfa/README.md)
- [JSON:API remote entities](/modules/helfi_etusivu_entities/README.md)

## Contact

Expand Down
35 changes: 35 additions & 0 deletions helfi_platform_config.install
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,38 @@ function helfi_platform_config_update_9308() : void {
function helfi_platform_config_update_9309() {
helfi_platform_config_update_9307();
}

/**
* UHF-9708: Enable helfi_node_survey module.
*/
function helfi_platform_config_update_9310() : void {
$module_installer = \Drupal::service('module_installer');

// Enable helfi_node_survey module.
if (!\Drupal::moduleHandler()->moduleExists('helfi_node_survey')) {
$module_installer->install(['helfi_node_survey']);
}
}

/**
* UHF-9708: Fix "Mismatched entity and/or field definitions" for published_at.
*/
function helfi_platform_config_update_9311(): void {
if (!\Drupal::moduleHandler()->moduleExists('publication_date')) {
return;
}

$changeList = \Drupal::entityDefinitionUpdateManager()->getChangeList();

// Check if field storage definition for published_at is missing.
if (isset($changeList['node']['field_storage_definitions']['published_at'])) {
$entity_type = \Drupal::entityTypeManager()
->getStorage('node')
->getEntityType();

if ($field = publication_date_entity_base_field_info($entity_type)['published_at'] ?? NULL) {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('published_at', 'node', 'publication_date', $field);
}
}
}
12 changes: 12 additions & 0 deletions modules/helfi_etusivu_entities/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Remote entities

Remote entities module allows fetching announcements from `Etusivu`-instance.
It utilizes `json-api` and `external_entities`-module to transfer the data between instances.

## How to set up locally

Local setup requires Etusivu-instance to be up and running with some relevant data created to it.

# Cache

Remote entities are cached with custom cache tags. These tags are cleared when any entities are updated using [PubSub](https://github.com/City-of-Helsinki/drupal-module-helfi-api-base/blob/main/documentation/pubsub-messaging.md) by etusivu instance. See [`helfi_etusivu` -module](https://github.com/City-of-Helsinki/drupal-helfi-etusivu/tree/dev/public/modules/custom/helfi_etusivu).
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
uuid: be631905-d77b-418a-890e-b04fa3ed3e22
langcode: en
status: true
dependencies: { }
id: helfi_surveys
label: 'Helfi: Survey'
label_plural: 'Helfi: Surveys'
description: ''
read_only: true
field_mapper_id: jsonpath
field_mapper_config:
field_mappings:
id:
value: $.id
uuid:
value: $.id
title:
value: '$.attributes["title"]'
published_at:
value: '$.attributes["published_at"]'
unpublish_on:
value: '$.attributes["unpublish_on"]'
langcode:
value: '$.attributes["langcode"]'
body:
value: '$.attributes["body"]["value"]'
status:
value: '$.attributes["status"]'
survey_link_text:
value: '$.attributes["field_survey_link"]["title"]'
survey_link_url:
value: '$.attributes["field_survey_link"]["uri"]'
storage_client_id: helfi_surveys
storage_client_config: { }
persistent_cache_max_age: -1
annotation_entity_type_id: null
annotation_bundle_id: null
annotation_field_name: null
inherits_annotation_fields: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
block.settings.surveys:
type: block_settings
label: 'Survey block settings'
mapping:
use_remote_entities:
type: boolean

block.settings.announcements:
type: block_settings
label: 'Announcement block settings'
mapping:
use_remote_entities:
type: boolean
10 changes: 10 additions & 0 deletions modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: 'HELfi Etusivu Entities'
type: module
core_version_requirement: ^10
dependencies:
- external_entities:external_entities
- publication_date:publication_date
- helfi_api_base:helfi_api_base
- helfi_platform_config:helfi_platform_config
- helfi_platform_config:helfi_node_announcement
- helfi_platform_config:helfi_node_survey
75 changes: 75 additions & 0 deletions modules/helfi_etusivu_entities/helfi_etusivu_entities.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* @file
* Contains installation tasks for helfi_etusivu_entities module.
*/

declare(strict_types=1);

/**
* Implements hook_install().
*/
function helfi_etusivu_entities_install($is_syncing) : void {
// Do not perform following steps if the module is being installed as part
// of a configuration import.
if ($is_syncing) {
return;
}

helfi_etusivu_entities_grant_permissions();
helfi_etusivu_entities_create_blocks();
}

/**
* Create required blocks.
*/
function helfi_etusivu_entities_create_blocks(): void {
/** @var Drupal\helfi_platform_config\Helper\BlockInstaller $block_installer */
$block_installer = Drupal::service('helfi_platform_config.helper.block_installer');

/** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
$theme_handler = \Drupal::service('theme_handler');

if (!str_starts_with($theme_handler->getDefault(), 'hdbt')) {
return;
}

$theme = $theme_handler->getDefault();
foreach (helfi_etusivu_entities_get_block_configurations($theme) as $block_config) {
['block' => $block, 'variations' => $variations] = $block_config;
$block_installer->install($block, $variations);
}
}

/**
* Implements hook_themes_installed().
*/
function helfi_etusivu_entities_themes_installed($theme_list) : void {
/** @var Drupal\helfi_platform_config\Helper\BlockInstaller $block_installer */
$block_installer = Drupal::service('helfi_platform_config.helper.block_installer');

foreach ($theme_list as $theme) {
if (in_array($theme, ['stark', 'hdbt', 'hdbt_subtheme'])) {
foreach (helfi_etusivu_entities_get_block_configurations($theme) as $block_config) {
['block' => $block, 'variations' => $variations] = $block_config;
$block_installer->install($block, $variations);
}
}
}
}

/**
* Grants required permissions.
*/
function helfi_etusivu_entities_grant_permissions() : void {
$permissions = [
'view helfi_announcements external entity',
'view helfi_surveys external entity',
];

helfi_platform_config_grant_permissions([
'anonymous' => $permissions,
'authenticated' => $permissions,
]);
}
131 changes: 131 additions & 0 deletions modules/helfi_etusivu_entities/helfi_etusivu_entities.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/**
* @file
* Contains installation tasks for helfi_etusivu_entities module.
*/

declare(strict_types=1);

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity\BundleFieldDefinition;

/**
* Gets the block configurations.
*
* Example block:
*
* @code
* [
* 'breadbrumbs' => [
* 'block' => [
* ...
* ],
* 'variations' => [
* ...
* ],
* ],
* ];
* @endcode
*
* @return array[]
* The block configurations.
*/
function helfi_etusivu_entities_get_block_configurations(string $theme) : array {
return [
'announcements' => [
'block' => [
'id' => 'announcements',
'plugin' => 'announcements',
'settings' => [
'id' => 'announcements',
'label' => 'Announcements',
'provider' => 'helfi_node_announcement',
],
'provider' => 'helfi_node_announcement',
'weight' => -15,
],
'variations' => [
[
'theme' => $theme,
'region' => 'before_content',
],
[
'theme' => 'stark',
'region' => 'content',
],
],
],
'surveys' => [
'block' => [
'id' => 'surveys',
'plugin' => 'surveys',
'settings' => [
'id' => 'surveys',
'label' => 'Surveys',
'provider' => 'helfi_node_survey',
],
'provider' => 'helfi_node_survey',
'weight' => -15,
],
'variations' => [
[
'theme' => $theme,
'region' => 'before_content',
],
[
'theme' => 'stark',
'region' => 'content',
],
],
],
];
}

/**
* Implements hook_entity_bundle_field_info_alter().
*/
function helfi_etusivu_entities_entity_bundle_field_info_alter(
&$fields,
EntityTypeInterface $entity_type,
) : void {
$remote_entities = [
'helfi_announcements',
'helfi_surveys',
];

if (in_array($entity_type->id(), $remote_entities)) {
// Common fields.
$entity_info_fields = [
'published_at' => new TranslatableMarkup('Published at'),
'unpublish_on' => new TranslatableMarkup('Unpublish on'),
'langcode' => new TranslatableMarkup('Langcode'),
'body' => new TranslatableMarkup('Body'),
'status' => new TranslatableMarkup('Status'),
];

// Additional entity info fields.
$entity_info_fields += match ($entity_type->id()) {
'helfi_announcements' => [
'notification' => new TranslatableMarkup('Notification'),
'announcement_type' => new TranslatableMarkup('Announcement type'),
'announcement_link_text' => new TranslatableMarkup('Announcement link text'),
'announcement_link_url' => new TranslatableMarkup('Announcement link url'),
'announcement_assistive_technology_close_button_title' => new TranslatableMarkup('Assistive technology close button title'),
],
'helfi_surveys' => [
'survey_link_text' => new TranslatableMarkup('Survey link text'),
'survey_link_url' => new TranslatableMarkup('Survey link url'),
],
};

foreach ($entity_info_fields as $field_name => $field_label) {
$fields[$field_name] = BundleFieldDefinition::create('string')
->setName($field_name)
->setLabel($field_label)
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
logger.channel.helfi_etusivu_entities:
parent: logger.channel_base
arguments: [ 'helfi_etusivu_entities' ]


Loading

0 comments on commit 10ca013

Please sign in to comment.