From 27e973dbd457aad389c692fdf44a07ae7d81f4aa Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Fri, 7 Jun 2024 15:37:12 +0300 Subject: [PATCH 01/33] UHF-9708: Added survey node content type with required fields --- ...ase_field_override.node.survey.promote.yml | 22 +++ ...base_field_override.node.survey.status.yml | 22 +++ ....base_field_override.node.survey.title.yml | 18 ++ ...ntity_form_display.node.survey.default.yml | 160 ++++++++++++++++++ ...ntity_view_display.node.survey.default.yml | 52 ++++++ ...entity_view_display.node.survey.teaser.yml | 41 +++++ .../install/field.field.node.survey.body.yml | 28 +++ ...eld.node.survey.field_survey_all_pages.yml | 23 +++ ...node.survey.field_survey_content_pages.yml | 31 ++++ ...ld.field.node.survey.field_survey_link.yml | 23 +++ ...ld.storage.node.field_survey_all_pages.yml | 18 ++ ...torage.node.field_survey_content_pages.yml | 19 +++ .../field.storage.node.field_survey_link.yml | 19 +++ .../language.content_settings.node.survey.yml | 16 ++ .../config/install/node.type.survey.yml | 31 ++++ ...ase_field_override.node.survey.promote.yml | 4 + ...base_field_override.node.survey.status.yml | 4 + .../fi/field.field.node.survey.body.yml | 1 + ...eld.node.survey.field_survey_all_pages.yml | 4 + ...node.survey.field_survey_content_pages.yml | 1 + ...ld.field.node.survey.field_survey_link.yml | 1 + .../optional/language/fi/node.type.survey.yml | 2 + ...ase_field_override.node.survey.promote.yml | 4 + .../sv/field.field.node.survey.body.yml | 1 + ...eld.node.survey.field_survey_all_pages.yml | 3 + ...ld.field.node.survey.field_survey_link.yml | 1 + .../helfi_node_survey.info.yml | 11 ++ .../helfi_node_survey.install | 67 ++++++++ .../helfi_node_survey.module | 24 +++ 29 files changed, 651 insertions(+) create mode 100644 modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml create mode 100644 modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml create mode 100644 modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml create mode 100755 modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml create mode 100644 modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml create mode 100644 modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml create mode 100644 modules/helfi_node_survey/config/install/field.field.node.survey.body.yml create mode 100644 modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml create mode 100644 modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml create mode 100644 modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml create mode 100644 modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml create mode 100644 modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml create mode 100644 modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml create mode 100644 modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml create mode 100644 modules/helfi_node_survey/config/install/node.type.survey.yml create mode 100644 modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.promote.yml create mode 100644 modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.status.yml create mode 100644 modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.body.yml create mode 100644 modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_all_pages.yml create mode 100644 modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_content_pages.yml create mode 100644 modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_link.yml create mode 100644 modules/helfi_node_survey/config/optional/language/fi/node.type.survey.yml create mode 100644 modules/helfi_node_survey/config/optional/language/sv/core.base_field_override.node.survey.promote.yml create mode 100644 modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.body.yml create mode 100644 modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.field_survey_all_pages.yml create mode 100644 modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.field_survey_link.yml create mode 100644 modules/helfi_node_survey/helfi_node_survey.info.yml create mode 100644 modules/helfi_node_survey/helfi_node_survey.install create mode 100644 modules/helfi_node_survey/helfi_node_survey.module diff --git a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml new file mode 100644 index 000000000..b50c5b8fc --- /dev/null +++ b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml @@ -0,0 +1,22 @@ +uuid: 001ee859-77ac-45da-9a8e-eb936b8a1fa0 +langcode: en +status: true +dependencies: + config: + - node.type.survey +id: node.survey.promote +field_name: promote +entity_type: node +bundle: survey +label: 'Promoted to front page' +description: '' +required: false +translatable: true +default_value: + - + value: 0 +default_value_callback: '' +settings: + on_label: 'On' + off_label: 'Off' +field_type: boolean diff --git a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml new file mode 100644 index 000000000..7a891cd12 --- /dev/null +++ b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml @@ -0,0 +1,22 @@ +uuid: 41718c98-7e06-411a-842d-a1e54a358ba2 +langcode: en +status: true +dependencies: + config: + - node.type.survey +id: node.survey.status +field_name: status +entity_type: node +bundle: survey +label: Published +description: '' +required: false +translatable: true +default_value: + - + value: 0 +default_value_callback: '' +settings: + on_label: 'On' + off_label: 'Off' +field_type: boolean diff --git a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml new file mode 100644 index 000000000..db64f8430 --- /dev/null +++ b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml @@ -0,0 +1,18 @@ +uuid: e9b04e11-3c4e-46a4-95c8-e0933f93769c +langcode: en +status: true +dependencies: + config: + - node.type.survey +id: node.survey.title +field_name: title +entity_type: node +bundle: survey +label: 'Survey title' +description: '' +required: true +translatable: true +default_value: { } +default_value_callback: '' +settings: { } +field_type: string diff --git a/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml b/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml new file mode 100755 index 000000000..d9ee47c05 --- /dev/null +++ b/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml @@ -0,0 +1,160 @@ +uuid: f7df631b-ba7d-497f-810b-8db4744d5056 +langcode: en +status: true +dependencies: + config: + - field.field.node.survey.body + - field.field.node.survey.field_survey_all_pages + - field.field.node.survey.field_survey_content_pages + - field.field.node.survey.field_survey_link + - node.type.survey + module: + - hdbt_admin_tools + - link + - path + - publication_date + - scheduler + - select2 + - text +id: node.survey.default +targetEntityType: node +bundle: survey +mode: default +content: + body: + type: text_textarea_with_summary + weight: 18 + region: content + settings: + rows: 9 + summary_rows: 3 + placeholder: '' + show_summary: false + third_party_settings: { } + created: + type: datetime_timestamp + weight: 3 + region: content + settings: { } + third_party_settings: { } + field_survey_all_pages: + type: boolean_checkbox + weight: 13 + region: content + settings: + display_label: true + third_party_settings: { } + field_survey_content_pages: + type: select2_entity_reference + weight: 14 + region: content + settings: + width: 100% + autocomplete: true + match_operator: CONTAINS + match_limit: 20 + third_party_settings: { } + field_survey_link: + type: link_default + weight: 19 + region: content + settings: + placeholder_url: '' + placeholder_title: '' + third_party_settings: { } + field_survey_service_pages: + type: select2_entity_reference + weight: 16 + region: content + settings: + width: 100% + autocomplete: true + match_operator: CONTAINS + match_limit: 20 + third_party_settings: { } + field_survey_unit_pages: + type: select2_entity_reference + weight: 15 + region: content + settings: + width: 100% + autocomplete: true + match_operator: CONTAINS + match_limit: 20 + third_party_settings: { } + langcode: + type: language_select + weight: 1 + region: content + settings: + include_locked: false + third_party_settings: { } + path: + type: path + weight: 6 + region: content + settings: { } + third_party_settings: { } + publish_on: + type: datetime_timestamp_no_default + weight: 7 + region: content + settings: { } + third_party_settings: { } + published_at: + type: publication_date_timestamp + weight: 10 + region: content + settings: { } + third_party_settings: { } + scheduler_settings: + weight: 5 + region: content + settings: { } + third_party_settings: { } + simple_sitemap: + weight: 12 + region: content + settings: { } + third_party_settings: { } + status: + type: boolean_checkbox + weight: 9 + region: content + settings: + display_label: true + third_party_settings: { } + title: + type: string_textfield + weight: 0 + region: content + settings: + size: 60 + placeholder: '' + third_party_settings: { } + translation: + weight: 4 + region: content + settings: { } + third_party_settings: { } + uid: + type: entity_reference_autocomplete + weight: 2 + region: content + settings: + match_operator: CONTAINS + match_limit: 10 + size: 60 + placeholder: '' + third_party_settings: { } + unpublish_on: + type: datetime_timestamp_no_default + weight: 8 + region: content + settings: { } + third_party_settings: { } +hidden: + hide_sidebar_navigation: true + promote: true + sticky: true + url_redirects: true diff --git a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml new file mode 100644 index 000000000..b6d1e3d9b --- /dev/null +++ b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml @@ -0,0 +1,52 @@ +uuid: 4e0a02f2-f3ff-402b-9bc9-1cb66811b0b3 +langcode: en +status: true +dependencies: + config: + - field.field.node.survey.body + - field.field.node.survey.field_survey_all_pages + - field.field.node.survey.field_survey_content_pages + - field.field.node.survey.field_survey_link + - node.type.survey + module: + - link + - options + - text + - user +id: node.survey.default +targetEntityType: node +bundle: survey +mode: default +content: + body: + type: text_default + label: hidden + settings: { } + third_party_settings: { } + weight: 2 + region: content + field_survey_link: + type: link + label: hidden + settings: + trim_length: 80 + url_only: false + url_plain: false + rel: '' + target: '' + third_party_settings: { } + weight: 3 + region: content + links: + settings: { } + third_party_settings: { } + weight: 0 + region: content +hidden: + field_survey_all_pages: true + field_survey_content_pages: true + field_survey_service_pages: true + field_survey_unit_pages: true + langcode: true + published_at: true + toc_enabled: true diff --git a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml new file mode 100644 index 000000000..fba637da0 --- /dev/null +++ b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml @@ -0,0 +1,41 @@ +uuid: 33a1dd1d-eb4f-4a66-b98e-093c530acc72 +langcode: en +status: true +dependencies: + config: + - core.entity_view_mode.node.teaser + - field.field.node.survey.body + - field.field.node.survey.field_survey_all_pages + - field.field.node.survey.field_survey_content_pages + - field.field.node.survey.field_survey_link + - node.type.survey + module: + - text + - user +id: node.survey.teaser +targetEntityType: node +bundle: survey +mode: teaser +content: + body: + type: text_summary_or_trimmed + label: hidden + settings: + trim_length: 600 + third_party_settings: { } + weight: 101 + region: content + links: + settings: { } + third_party_settings: { } + weight: 100 + region: content +hidden: + field_survey_all_pages: true + field_survey_content_pages: true + field_survey_link: true + field_survey_service_pages: true + field_survey_unit_pages: true + langcode: true + published_at: true + toc_enabled: true diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml new file mode 100644 index 000000000..50d0cb07a --- /dev/null +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml @@ -0,0 +1,28 @@ +uuid: eba1ef2e-55ec-4dc8-95e6-c11437b85618 +langcode: en +status: true +dependencies: + config: + - field.storage.node.body + - node.type.survey + module: + - allowed_formats + - text +third_party_settings: + allowed_formats: + allowed_formats: + - minimal +id: node.survey.body +field_name: body +entity_type: node +bundle: survey +label: Body +description: '' +required: true +translatable: true +default_value: { } +default_value_callback: '' +settings: + display_summary: false + required_summary: false +field_type: text_with_summary diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml new file mode 100644 index 000000000..38ac88496 --- /dev/null +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml @@ -0,0 +1,23 @@ +uuid: 670b99a9-f19d-4716-8d3e-0ad8dc266c4d +langcode: en +status: true +dependencies: + config: + - field.storage.node.field_survey_all_pages + - node.type.survey +id: node.survey.field_survey_all_pages +field_name: field_survey_all_pages +entity_type: node +bundle: survey +label: 'Show on all pages' +description: '' +required: false +translatable: false +default_value: + - + value: 1 +default_value_callback: '' +settings: + on_label: 'On' + off_label: 'Off' +field_type: boolean diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml new file mode 100644 index 000000000..3dc4b8d57 --- /dev/null +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml @@ -0,0 +1,31 @@ +uuid: e8249c25-d21c-4c72-b2cf-33f10ed3e922 +langcode: en +status: true +dependencies: + config: + - field.storage.node.field_survey_content_pages + - node.type.survey + - node.type.landing_page + - node.type.page +id: node.survey.field_survey_content_pages +field_name: field_survey_content_pages +entity_type: node +bundle: survey +label: 'Show on content pages' +description: '' +required: false +translatable: false +default_value: { } +default_value_callback: '' +settings: + handler: 'default:node' + handler_settings: + target_bundles: + page: page + landing_page: landing_page + sort: + field: title + direction: ASC + auto_create: false + auto_create_bundle: page +field_type: entity_reference diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml new file mode 100644 index 000000000..0fd8a4a34 --- /dev/null +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml @@ -0,0 +1,23 @@ +uuid: 82845c69-4bcf-460a-8294-17fe126218a8 +langcode: en +status: true +dependencies: + config: + - field.storage.node.field_survey_link + - node.type.survey + module: + - link +id: node.survey.field_survey_link +field_name: field_survey_link +entity_type: node +bundle: survey +label: Link +description: '' +required: false +translatable: true +default_value: { } +default_value_callback: '' +settings: + title: 2 + link_type: 17 +field_type: link diff --git a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml new file mode 100644 index 000000000..c761fbe66 --- /dev/null +++ b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml @@ -0,0 +1,18 @@ +uuid: b1244d5b-00ee-4960-9f9c-92f6bf6c9a9b +langcode: en +status: true +dependencies: + module: + - node +id: node.field_survey_all_pages +field_name: field_survey_all_pages +entity_type: node +type: boolean +settings: { } +module: core +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml new file mode 100644 index 000000000..9db446bf1 --- /dev/null +++ b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml @@ -0,0 +1,19 @@ +uuid: 2a295db8-6558-4c83-b9e9-c0c83b960f2b +langcode: en +status: true +dependencies: + module: + - node +id: node.field_survey_content_pages +field_name: field_survey_content_pages +entity_type: node +type: entity_reference +settings: + target_type: node +module: core +locked: false +cardinality: -1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml new file mode 100644 index 000000000..f95903715 --- /dev/null +++ b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml @@ -0,0 +1,19 @@ +uuid: eb391805-478b-45bb-9161-5d2d413fe6e8 +langcode: en +status: true +dependencies: + module: + - link + - node +id: node.field_survey_link +field_name: field_survey_link +entity_type: node +type: link +settings: { } +module: link +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml b/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml new file mode 100644 index 000000000..d83294c16 --- /dev/null +++ b/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml @@ -0,0 +1,16 @@ +uuid: 1e3db0f3-422a-42ee-8d62-23a079797a09 +langcode: en +status: true +dependencies: + config: + - node.type.survey + module: + - content_translation +third_party_settings: + content_translation: + enabled: true +id: node.survey +target_entity_type_id: node +target_bundle: survey +default_langcode: fi +language_alterable: true diff --git a/modules/helfi_node_survey/config/install/node.type.survey.yml b/modules/helfi_node_survey/config/install/node.type.survey.yml new file mode 100644 index 000000000..8d6fa4963 --- /dev/null +++ b/modules/helfi_node_survey/config/install/node.type.survey.yml @@ -0,0 +1,31 @@ +uuid: f9819832-5612-4b35-ade9-62996e875942 +langcode: en +status: true +dependencies: + module: + - menu_ui + - scheduler +third_party_settings: + menu_ui: + available_menus: { } + parent: '' + scheduler: + expand_fieldset: when_required + fields_display_mode: vertical_tab + publish_enable: true + publish_past_date: error + publish_past_date_created: false + publish_required: false + publish_revision: false + publish_touch: true + show_message_after_update: true + unpublish_enable: true + unpublish_required: false + unpublish_revision: false +name: Survey +type: survey +description: 'Content type for surveys that can be shown on different pages.' +help: '' +new_revision: true +preview_mode: 1 +display_submitted: false diff --git a/modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.promote.yml b/modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.promote.yml new file mode 100644 index 000000000..a29359aab --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.promote.yml @@ -0,0 +1,4 @@ +label: 'Näytetään etusivulla' +settings: + on_label: Kyllä + off_label: Ei diff --git a/modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.status.yml b/modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.status.yml new file mode 100644 index 000000000..b939375b7 --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.status.yml @@ -0,0 +1,4 @@ +label: Julkaistu +settings: + on_label: Kyllä + off_label: Ei diff --git a/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.body.yml b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.body.yml new file mode 100644 index 000000000..9d61cbc9a --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.body.yml @@ -0,0 +1 @@ +label: Sisältö diff --git a/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_all_pages.yml b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_all_pages.yml new file mode 100644 index 000000000..b053189d9 --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_all_pages.yml @@ -0,0 +1,4 @@ +label: 'Näytä kaikilla sivuilla' +settings: + on_label: Kyllä + off_label: Ei diff --git a/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_content_pages.yml b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_content_pages.yml new file mode 100644 index 000000000..1abe9ed71 --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_content_pages.yml @@ -0,0 +1 @@ +label: 'Näytä sisältösivuilla' diff --git a/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_link.yml b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_link.yml new file mode 100644 index 000000000..d060ac6b2 --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_link.yml @@ -0,0 +1 @@ +label: Linkki diff --git a/modules/helfi_node_survey/config/optional/language/fi/node.type.survey.yml b/modules/helfi_node_survey/config/optional/language/fi/node.type.survey.yml new file mode 100644 index 000000000..d9e4a950b --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/fi/node.type.survey.yml @@ -0,0 +1,2 @@ +name: Kysely +description: 'Tämän sisältötyypin avulla voidaan näyttää kyselyitä eri sivuilla.' diff --git a/modules/helfi_node_survey/config/optional/language/sv/core.base_field_override.node.survey.promote.yml b/modules/helfi_node_survey/config/optional/language/sv/core.base_field_override.node.survey.promote.yml new file mode 100644 index 000000000..1c731d26d --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/sv/core.base_field_override.node.survey.promote.yml @@ -0,0 +1,4 @@ +label: 'Visas på startsidan' +settings: + on_label: Ja + off_label: Nej diff --git a/modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.body.yml b/modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.body.yml new file mode 100644 index 000000000..7e34baf6d --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.body.yml @@ -0,0 +1 @@ +label: Body diff --git a/modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.field_survey_all_pages.yml b/modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.field_survey_all_pages.yml new file mode 100644 index 000000000..83c1e8386 --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.field_survey_all_pages.yml @@ -0,0 +1,3 @@ +settings: + on_label: Ja + off_label: Nej diff --git a/modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.field_survey_link.yml b/modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.field_survey_link.yml new file mode 100644 index 000000000..cb5c23377 --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.field_survey_link.yml @@ -0,0 +1 @@ +label: Länk diff --git a/modules/helfi_node_survey/helfi_node_survey.info.yml b/modules/helfi_node_survey/helfi_node_survey.info.yml new file mode 100644 index 000000000..dacc8c0ea --- /dev/null +++ b/modules/helfi_node_survey/helfi_node_survey.info.yml @@ -0,0 +1,11 @@ +name: 'HELfi Node survey' +type: module +core_version_requirement: '^9 || ^10' +dependencies: + - allowed_formats:allowed_formats + - drupal:menu_ui + - helfi_base_content:helfi_base_content + - helfi_node_landing_page:helfi_node_landing_page + - helfi_node_page:helfi_node_page + - publication_date:publication_date + - helfi_api_base:helfi_api_base diff --git a/modules/helfi_node_survey/helfi_node_survey.install b/modules/helfi_node_survey/helfi_node_survey.install new file mode 100644 index 000000000..5f42dd30e --- /dev/null +++ b/modules/helfi_node_survey/helfi_node_survey.install @@ -0,0 +1,67 @@ + [ + 'create survey content', + 'delete survey revisions', + 'delete any survey content', + 'delete own survey content', + 'edit any survey content', + 'edit own survey content', + 'revert survey revisions', + 'set survey published on date', + 'translate survey node', + 'view survey revisions', + ], + 'content_producer' => [ + 'create survey content', + 'delete own survey content', + 'edit any survey content', + 'edit own survey content', + 'revert survey revisions', + 'set survey published on date', + 'view survey revisions', + ], + 'editor' => [ + 'create survey content', + 'delete survey revisions', + 'delete any survey content', + 'delete own survey content', + 'edit any survey content', + 'edit own survey content', + 'revert survey revisions', + 'set survey published on date', + 'translate survey node', + 'view survey revisions', + ], + 'read_only' => [ + 'view any unpublished survey content', + ], + ]; + helfi_platform_config_grant_permissions($permissions); +} + +/** + * Implements hook_install(). + */ +function helfi_node_survey_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_node_survey_grant_permissions(); +// _helfi_global_survey_enable(); +} diff --git a/modules/helfi_node_survey/helfi_node_survey.module b/modules/helfi_node_survey/helfi_node_survey.module new file mode 100644 index 000000000..4cbeefd66 --- /dev/null +++ b/modules/helfi_node_survey/helfi_node_survey.module @@ -0,0 +1,24 @@ + [ + ':input[name="field_survey_all_pages[value]"]' => ['checked' => FALSE], + ], + ]; + break; + } +} From 98163a0c1c489fd8efd28eeeac935cc282aaf6d9 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Fri, 7 Jun 2024 16:17:56 +0300 Subject: [PATCH 02/33] UHF-9708: Add missing translation for survey title --- .../language/fi/core.base_field_override.node.survey.title.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.title.yml diff --git a/modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.title.yml b/modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.title.yml new file mode 100644 index 000000000..933421645 --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/fi/core.base_field_override.node.survey.title.yml @@ -0,0 +1 @@ +label: 'Kyselyn otsikko' From 0122b4e6b14c62b62e86040142f59428c5cc8329 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Fri, 7 Jun 2024 16:23:33 +0300 Subject: [PATCH 03/33] UHF-9708: Limit survey permissions to only admins --- .../helfi_node_survey.install | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/modules/helfi_node_survey/helfi_node_survey.install b/modules/helfi_node_survey/helfi_node_survey.install index 5f42dd30e..275e6b14c 100644 --- a/modules/helfi_node_survey/helfi_node_survey.install +++ b/modules/helfi_node_survey/helfi_node_survey.install @@ -24,30 +24,6 @@ function helfi_node_survey_grant_permissions() : void { 'translate survey node', 'view survey revisions', ], - 'content_producer' => [ - 'create survey content', - 'delete own survey content', - 'edit any survey content', - 'edit own survey content', - 'revert survey revisions', - 'set survey published on date', - 'view survey revisions', - ], - 'editor' => [ - 'create survey content', - 'delete survey revisions', - 'delete any survey content', - 'delete own survey content', - 'edit any survey content', - 'edit own survey content', - 'revert survey revisions', - 'set survey published on date', - 'translate survey node', - 'view survey revisions', - ], - 'read_only' => [ - 'view any unpublished survey content', - ], ]; helfi_platform_config_grant_permissions($permissions); } @@ -63,5 +39,4 @@ function helfi_node_survey_install($is_syncing) : void { } helfi_node_survey_grant_permissions(); -// _helfi_global_survey_enable(); } From e130b81e735be740e07684286c87468ef492f82d Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Fri, 7 Jun 2024 16:26:07 +0300 Subject: [PATCH 04/33] UHF-9708: Enable helfi_node_survey module --- helfi_platform_config.install | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/helfi_platform_config.install b/helfi_platform_config.install index f3b60861b..e9f72d15a 100644 --- a/helfi_platform_config.install +++ b/helfi_platform_config.install @@ -133,3 +133,15 @@ 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']); + } +} From ae785d690d7148a957f7f71419f45ff52e28fba6 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Fri, 7 Jun 2024 16:38:36 +0300 Subject: [PATCH 05/33] UHF-9708: Remove the uuids from the config for now, disable the title on link field --- .../install/core.base_field_override.node.survey.promote.yml | 1 - .../install/core.base_field_override.node.survey.status.yml | 1 - .../install/core.base_field_override.node.survey.title.yml | 1 - .../install/core.entity_form_display.node.survey.default.yml | 1 - .../install/core.entity_view_display.node.survey.default.yml | 1 - .../install/core.entity_view_display.node.survey.teaser.yml | 1 - .../config/install/field.field.node.survey.body.yml | 1 - .../install/field.field.node.survey.field_survey_all_pages.yml | 1 - .../field.field.node.survey.field_survey_content_pages.yml | 1 - .../install/field.field.node.survey.field_survey_link.yml | 2 +- .../install/field.storage.node.field_survey_all_pages.yml | 1 - .../install/field.storage.node.field_survey_content_pages.yml | 1 - .../config/install/field.storage.node.field_survey_link.yml | 1 - .../config/install/language.content_settings.node.survey.yml | 1 - modules/helfi_node_survey/config/install/node.type.survey.yml | 1 - 15 files changed, 1 insertion(+), 15 deletions(-) diff --git a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml index b50c5b8fc..23d9bf1d3 100644 --- a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml +++ b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml @@ -1,4 +1,3 @@ -uuid: 001ee859-77ac-45da-9a8e-eb936b8a1fa0 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml index 7a891cd12..7c8da97f1 100644 --- a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml +++ b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml @@ -1,4 +1,3 @@ -uuid: 41718c98-7e06-411a-842d-a1e54a358ba2 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml index db64f8430..fb942253f 100644 --- a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml +++ b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml @@ -1,4 +1,3 @@ -uuid: e9b04e11-3c4e-46a4-95c8-e0933f93769c langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml b/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml index d9ee47c05..fae1cda46 100755 --- a/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml +++ b/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml @@ -1,4 +1,3 @@ -uuid: f7df631b-ba7d-497f-810b-8db4744d5056 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml index b6d1e3d9b..822ebeed2 100644 --- a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml +++ b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml @@ -1,4 +1,3 @@ -uuid: 4e0a02f2-f3ff-402b-9bc9-1cb66811b0b3 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml index fba637da0..697b29c06 100644 --- a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml +++ b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml @@ -1,4 +1,3 @@ -uuid: 33a1dd1d-eb4f-4a66-b98e-093c530acc72 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml index 50d0cb07a..76ad1a2e2 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml @@ -1,4 +1,3 @@ -uuid: eba1ef2e-55ec-4dc8-95e6-c11437b85618 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml index 38ac88496..86105e7b2 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml @@ -1,4 +1,3 @@ -uuid: 670b99a9-f19d-4716-8d3e-0ad8dc266c4d langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml index 3dc4b8d57..119777a2c 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml @@ -1,4 +1,3 @@ -uuid: e8249c25-d21c-4c72-b2cf-33f10ed3e922 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml index 0fd8a4a34..ba1be7cb5 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml @@ -18,6 +18,6 @@ translatable: true default_value: { } default_value_callback: '' settings: - title: 2 + title: 0 link_type: 17 field_type: link diff --git a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml index c761fbe66..3747e5dfb 100644 --- a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml +++ b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml @@ -1,4 +1,3 @@ -uuid: b1244d5b-00ee-4960-9f9c-92f6bf6c9a9b langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml index 9db446bf1..346e3d42e 100644 --- a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml +++ b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml @@ -1,4 +1,3 @@ -uuid: 2a295db8-6558-4c83-b9e9-c0c83b960f2b langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml index f95903715..8f7a13023 100644 --- a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml +++ b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml @@ -1,4 +1,3 @@ -uuid: eb391805-478b-45bb-9161-5d2d413fe6e8 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml b/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml index d83294c16..90a110a35 100644 --- a/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml +++ b/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml @@ -1,4 +1,3 @@ -uuid: 1e3db0f3-422a-42ee-8d62-23a079797a09 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/node.type.survey.yml b/modules/helfi_node_survey/config/install/node.type.survey.yml index 8d6fa4963..d59bda960 100644 --- a/modules/helfi_node_survey/config/install/node.type.survey.yml +++ b/modules/helfi_node_survey/config/install/node.type.survey.yml @@ -1,4 +1,3 @@ -uuid: f9819832-5612-4b35-ade9-62996e875942 langcode: en status: true dependencies: From 5db138064ed7ec8faf0b7e424bf95a84e7ebffc4 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Fri, 7 Jun 2024 16:58:57 +0300 Subject: [PATCH 06/33] UHF-9708: Add uuids for the node survey module configurations --- .../install/core.base_field_override.node.survey.promote.yml | 1 + .../install/core.base_field_override.node.survey.status.yml | 1 + .../install/core.base_field_override.node.survey.title.yml | 1 + .../install/core.entity_form_display.node.survey.default.yml | 1 + .../install/core.entity_view_display.node.survey.default.yml | 1 + .../install/core.entity_view_display.node.survey.teaser.yml | 1 + .../config/install/field.field.node.survey.body.yml | 1 + .../install/field.field.node.survey.field_survey_all_pages.yml | 1 + .../field.field.node.survey.field_survey_content_pages.yml | 1 + .../config/install/field.storage.node.field_survey_all_pages.yml | 1 + .../install/field.storage.node.field_survey_content_pages.yml | 1 + .../config/install/field.storage.node.field_survey_link.yml | 1 + .../config/install/language.content_settings.node.survey.yml | 1 + modules/helfi_node_survey/config/install/node.type.survey.yml | 1 + 14 files changed, 14 insertions(+) diff --git a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml index 23d9bf1d3..fe3f0a836 100644 --- a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml +++ b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.promote.yml @@ -1,3 +1,4 @@ +uuid: 0f67ac70-571e-43da-ae3f-eca271ee541a langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml index 7c8da97f1..1667da391 100644 --- a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml +++ b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.status.yml @@ -1,3 +1,4 @@ +uuid: 66c7c3ae-fd35-4cd8-a4c2-54bc24cf5895 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml index fb942253f..35af141cc 100644 --- a/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml +++ b/modules/helfi_node_survey/config/install/core.base_field_override.node.survey.title.yml @@ -1,3 +1,4 @@ +uuid: 12bf02be-360b-498c-9891-fe06f94408b8 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml b/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml index fae1cda46..f6389f42f 100755 --- a/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml +++ b/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml @@ -1,3 +1,4 @@ +uuid: 2dea284e-6cc0-4e34-9d2f-37baa7317502 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml index 822ebeed2..040ec94b3 100644 --- a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml +++ b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml @@ -1,3 +1,4 @@ +uuid: 1dab22c0-e5e8-487c-9050-9ba480532d07 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml index 697b29c06..54e6acb78 100644 --- a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml +++ b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml @@ -1,3 +1,4 @@ +uuid: 394e2865-baf3-4603-9d97-57f59e49f5a6 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml index 76ad1a2e2..9a8b7d023 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.body.yml @@ -1,3 +1,4 @@ +uuid: d1b45349-7c63-4ac3-b7b3-563eeb88f38c langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml index 86105e7b2..7590e04aa 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml @@ -1,3 +1,4 @@ +uuid: b7008a02-dd8e-42b1-b6fb-2cb4f642d9e5 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml index 119777a2c..a6eb80c80 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml @@ -1,3 +1,4 @@ +uuid: 9cb9a071-2d67-43d0-a8e3-6e2b7142ee17 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml index 3747e5dfb..009c86145 100644 --- a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml +++ b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml @@ -1,3 +1,4 @@ +uuid: 700a8e47-2a1a-4736-94de-5306cfe5148a langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml index 346e3d42e..467e7e8ad 100644 --- a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml +++ b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_content_pages.yml @@ -1,3 +1,4 @@ +uuid: 3a26e002-c1e2-4840-bb59-f29aea3489ea langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml index 8f7a13023..982307b36 100644 --- a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml +++ b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_link.yml @@ -1,3 +1,4 @@ +uuid: e9b298a6-f958-41b6-9c26-5fd7868e4120 langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml b/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml index 90a110a35..ecfb381a1 100644 --- a/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml +++ b/modules/helfi_node_survey/config/install/language.content_settings.node.survey.yml @@ -1,3 +1,4 @@ +uuid: c10c6c36-eccd-4664-bf08-bc58654eac6d langcode: en status: true dependencies: diff --git a/modules/helfi_node_survey/config/install/node.type.survey.yml b/modules/helfi_node_survey/config/install/node.type.survey.yml index d59bda960..fe7e73e12 100644 --- a/modules/helfi_node_survey/config/install/node.type.survey.yml +++ b/modules/helfi_node_survey/config/install/node.type.survey.yml @@ -1,3 +1,4 @@ +uuid: 1dc12a96-80cd-44c3-a566-f10debad1c49 langcode: en status: true dependencies: From aeaa7df69a82cf4e7f97ba4902d0b02a404906b9 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Fri, 7 Jun 2024 17:12:52 +0300 Subject: [PATCH 07/33] UHF-9708: Change the uuid on the survey link field --- .../install/field.field.node.survey.field_survey_link.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml index ba1be7cb5..48aaccc12 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml @@ -1,4 +1,4 @@ -uuid: 82845c69-4bcf-460a-8294-17fe126218a8 +uuid: 21a43a79-869a-4758-b64a-b665aec0561a langcode: en status: true dependencies: From 74d9fd94318a1d90afb9286e1398754b8a3c3733 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Mon, 10 Jun 2024 15:58:59 +0300 Subject: [PATCH 08/33] UHF-9708: Change permissions to belong to survey_editor role, add the new role configuration --- .../config/install/user.role.survey_editor.yml | 8 ++++++++ modules/helfi_node_survey/helfi_node_survey.install | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 modules/helfi_node_survey/config/install/user.role.survey_editor.yml diff --git a/modules/helfi_node_survey/config/install/user.role.survey_editor.yml b/modules/helfi_node_survey/config/install/user.role.survey_editor.yml new file mode 100644 index 000000000..7467fa73a --- /dev/null +++ b/modules/helfi_node_survey/config/install/user.role.survey_editor.yml @@ -0,0 +1,8 @@ +langcode: en +status: true +dependencies: { } +id: survey_editor +label: 'Survey editor' +weight: 5 +is_admin: null +permissions: { } diff --git a/modules/helfi_node_survey/helfi_node_survey.install b/modules/helfi_node_survey/helfi_node_survey.install index 275e6b14c..2a1bb4b53 100644 --- a/modules/helfi_node_survey/helfi_node_survey.install +++ b/modules/helfi_node_survey/helfi_node_survey.install @@ -12,7 +12,7 @@ declare(strict_types=1); */ function helfi_node_survey_grant_permissions() : void { $permissions = [ - 'admin' => [ + 'survey_creator' => [ 'create survey content', 'delete survey revisions', 'delete any survey content', From 77a878d1081a933d2853815a3bd6dabbfbc41ece Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Mon, 10 Jun 2024 16:14:44 +0300 Subject: [PATCH 09/33] UHF-9708: Add uuid and bigger weight for the survey_editor role --- .../config/install/user.role.survey_editor.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/helfi_node_survey/config/install/user.role.survey_editor.yml b/modules/helfi_node_survey/config/install/user.role.survey_editor.yml index 7467fa73a..e42c98be9 100644 --- a/modules/helfi_node_survey/config/install/user.role.survey_editor.yml +++ b/modules/helfi_node_survey/config/install/user.role.survey_editor.yml @@ -1,8 +1,9 @@ +uuid: 9ec2282e-0675-4746-9892-cd37d2b6fc8a langcode: en status: true dependencies: { } id: survey_editor label: 'Survey editor' -weight: 5 +weight: 8 is_admin: null permissions: { } From 3e6b1eecfd688c6fabee17ac8c148116b0440a7b Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Mon, 10 Jun 2024 16:27:08 +0300 Subject: [PATCH 10/33] UHF-9708: Fix typo --- modules/helfi_node_survey/helfi_node_survey.install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/helfi_node_survey/helfi_node_survey.install b/modules/helfi_node_survey/helfi_node_survey.install index 2a1bb4b53..d05392496 100644 --- a/modules/helfi_node_survey/helfi_node_survey.install +++ b/modules/helfi_node_survey/helfi_node_survey.install @@ -12,7 +12,7 @@ declare(strict_types=1); */ function helfi_node_survey_grant_permissions() : void { $permissions = [ - 'survey_creator' => [ + 'survey_editor' => [ 'create survey content', 'delete survey revisions', 'delete any survey content', From f8031c20b47df1750cd7620ca64b9bf5893487b2 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Thu, 13 Jun 2024 08:57:13 +0300 Subject: [PATCH 11/33] UHF-9708: Use PHP attributes instead of doctrine annotations https://www.drupal.org/project/drupal/issues/3252386 --- .../src/Plugin/Block/GlobalAnnouncementsBlock.php | 11 ++++++----- .../src/Plugin/Block/AnnouncementsBlock.php | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php b/modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php index 4e9b3c755..c4d982548 100644 --- a/modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php +++ b/modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php @@ -3,7 +3,9 @@ namespace Drupal\helfi_global_announcement\Plugin\Block; use Drupal\Component\Utility\Xss; +use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Cache\Cache; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\external_entities\ExternalEntityStorageInterface; use Drupal\helfi_global_announcement\Plugin\ExternalEntities\StorageClient\Announcements; use Drupal\helfi_node_announcement\Plugin\Block\AnnouncementsBlockBase; @@ -11,12 +13,11 @@ /** * Provides 'global announcements' block. - * - * @Block( - * id = "global_announcements", - * admin_label = @Translation("Global announcements"), - * ) */ +#[Block( + id: "global_announcements", + admin_label: new TranslatableMarkup("Global announcements"), +)] class GlobalAnnouncementsBlock extends AnnouncementsBlockBase { /** diff --git a/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlock.php b/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlock.php index 5718957e8..d829175b6 100644 --- a/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlock.php +++ b/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlock.php @@ -2,18 +2,19 @@ namespace Drupal\helfi_node_announcement\Plugin\Block; +use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Cache\Cache; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\node\NodeInterface; /** * Provides an 'Announcements' block. - * - * @Block( - * id = "announcements", - * admin_label = @Translation("Announcements"), - * ) */ +#[Block( + id: "announcements", + admin_label: new TranslatableMarkup("Announcements"), +)] class AnnouncementsBlock extends AnnouncementsBlockBase { /** From 2c62a5376b3142845bad754524019c52c40c137a Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Thu, 13 Jun 2024 08:57:15 +0300 Subject: [PATCH 12/33] UHF-9708: Move Global annoucements client to helfi_etusivu_entities module --- modules/helfi_etusivu_entities/README.md | 8 ++ ...ternal_entity_type.helfi_announcements.yml | 0 .../helfi_etusivu_entities.info.yml | 5 ++ .../helfi_etusivu_entities.module | 59 ++++++++++++++ .../helfi_etusivu_entities.services.yml | 6 ++ .../StorageClient/Announcements.php | 60 ++++++++++++++ .../StorageClient/EtusivuEntityBase.php} | 81 +++++-------------- .../helfi_global_announcement.info.yml | 1 + .../helfi_global_announcement.install | 14 ++++ .../helfi_global_announcement.module | 38 +-------- .../Plugin/Block/GlobalAnnouncementsBlock.php | 2 +- 11 files changed, 174 insertions(+), 100 deletions(-) create mode 100644 modules/helfi_etusivu_entities/README.md rename modules/{helfi_global_announcement => helfi_etusivu_entities}/config/install/external_entities.external_entity_type.helfi_announcements.yml (100%) create mode 100644 modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml create mode 100644 modules/helfi_etusivu_entities/helfi_etusivu_entities.module create mode 100644 modules/helfi_etusivu_entities/helfi_etusivu_entities.services.yml create mode 100644 modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php rename modules/{helfi_global_announcement/src/Plugin/ExternalEntities/StorageClient/Announcements.php => helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php} (66%) diff --git a/modules/helfi_etusivu_entities/README.md b/modules/helfi_etusivu_entities/README.md new file mode 100644 index 000000000..61c566275 --- /dev/null +++ b/modules/helfi_etusivu_entities/README.md @@ -0,0 +1,8 @@ +# 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. diff --git a/modules/helfi_global_announcement/config/install/external_entities.external_entity_type.helfi_announcements.yml b/modules/helfi_etusivu_entities/config/install/external_entities.external_entity_type.helfi_announcements.yml similarity index 100% rename from modules/helfi_global_announcement/config/install/external_entities.external_entity_type.helfi_announcements.yml rename to modules/helfi_etusivu_entities/config/install/external_entities.external_entity_type.helfi_announcements.yml diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml b/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml new file mode 100644 index 000000000..5480694c2 --- /dev/null +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml @@ -0,0 +1,5 @@ +name: 'HELfi Remote Entities' +type: module +core_version_requirement: ^10 +dependencies: + - external_entities:external_entities diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module new file mode 100644 index 000000000..f8a664b80 --- /dev/null +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module @@ -0,0 +1,59 @@ +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); + } + } +} diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.services.yml b/modules/helfi_etusivu_entities/helfi_etusivu_entities.services.yml new file mode 100644 index 000000000..98ead7bf4 --- /dev/null +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.services.yml @@ -0,0 +1,6 @@ +services: + logger.channel.helfi_etusivu_entities: + parent: logger.channel_base + arguments: [ 'helfi_etusivu_entities' ] + + diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php new file mode 100644 index 000000000..db8d54966 --- /dev/null +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php @@ -0,0 +1,60 @@ + 'id', + 'fields[status]' => 1, + 'filter[status][value]' => 1, + 'filter[field_publish_externally][value]' => 1, + ]; + + if ($start) { + $query['page[offset]'] = $start; + } + + if ($length) { + $query['page[limit]'] = $length; + } + + // If filter is missing language set it manually. + if (!isset($query['filter[langcode]'])) { + $language = $this->languageManager + ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) + ->getId(); + $query['filter[langcode]'] = $language; + } + + return $this->request("/node/announcement", $query, $query['filter[langcode]']); + } + +} diff --git a/modules/helfi_global_announcement/src/Plugin/ExternalEntities/StorageClient/Announcements.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php similarity index 66% rename from modules/helfi_global_announcement/src/Plugin/ExternalEntities/StorageClient/Announcements.php rename to modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php index 559c6c5a4..4a2cd3c20 100644 --- a/modules/helfi_global_announcement/src/Plugin/ExternalEntities/StorageClient/Announcements.php +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php @@ -2,10 +2,9 @@ declare(strict_types=1); -namespace Drupal\helfi_global_announcement\Plugin\ExternalEntities\StorageClient; +namespace Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient; use Drupal\Core\Entity\EntityStorageException; -use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Utility\Error; use Drupal\external_entities\ExternalEntityInterface; @@ -17,26 +16,23 @@ use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\Query; use GuzzleHttp\Utils; -use Psr\Log\LoggerInterface; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * External entity storage client for News feed entities. - * - * @ExternalEntityStorageClient( - * id = "helfi_announcements", - * label = @Translation("Helfi: Announcements"), - * description = @Translation("Retrieves announcements from helfi") - * ) + * Base class for etusivu external entity storage client. */ -final class Announcements extends ExternalEntityStorageClientBase { +abstract class EtusivuEntityBase extends ExternalEntityStorageClientBase implements LoggerAwareInterface { + + use LoggerAwareTrait; /** - * Custom cache tag for announcements. + * Custom cache tag. * * @var string */ - public static string $customCacheTag = 'helfi_external_entity_announcement'; + public static string $customCacheTag = 'helfi_external_entity'; /** * The active endpoint environment. @@ -45,13 +41,6 @@ final class Announcements extends ExternalEntityStorageClientBase { */ private ?Environment $environment = NULL; - /** - * The current language service. - * - * @var \Drupal\Core\Language\LanguageManagerInterface - */ - private LanguageManagerInterface $languageManager; - /** * The HTTP client service. * @@ -60,11 +49,11 @@ final class Announcements extends ExternalEntityStorageClientBase { private ClientInterface $client; /** - * The logger. + * The current language service. * - * @var \Psr\Log\LoggerInterface + * @var \Drupal\Core\Language\LanguageManagerInterface */ - private LoggerInterface $logger; + protected LanguageManagerInterface $languageManager; /** * {@inheritdoc} @@ -76,8 +65,8 @@ public static function create( $plugin_definition, ) : self { $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); - $instance->languageManager = $container->get('language_manager'); $instance->client = $container->get('http_client'); + $instance->languageManager = $container->get('language_manager'); /** @var \Drupal\helfi_api_base\Environment\EnvironmentResolver $environmentResolver */ $environmentResolver = $container->get('helfi_api_base.environment_resolver'); @@ -88,7 +77,7 @@ public static function create( } catch (\InvalidArgumentException) { } - $instance->logger = $container->get('logger.factory')->get('helfi_announcements'); + $instance->setLogger($container->get('logger.channel.helfi_etusivu_entities')); return $instance; } @@ -132,44 +121,11 @@ public function delete(ExternalEntityInterface $entity) : void { throw new EntityStorageException('::delete() is not supported.'); } - /** - * {@inheritdoc} - */ - public function query( - array $parameters = [], - array $sorts = [], - $start = NULL, - $length = NULL, - ) : array { - $query = [ - 'fields[node--announcements]' => 'id', - 'fields[status]' => 1, - 'filter[status][value]' => 1, - 'filter[field_publish_externally][value]' => 1, - ]; - - if ($start) { - $query['page[offset]'] = $start; - } - - if ($length) { - $query['page[limit]'] = $length; - } - - // If filter is missing language set it manually. - if (!isset($query['filter[langcode]'])) { - $language = $this->languageManager - ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) - ->getId(); - $query['filter[langcode]'] = $language; - } - - return $this->request($query, $query['filter[langcode]']); - } - /** * Creates a request against JSON:API. * + * @param string $endpoint + * The jsonapi endpoint. * @param array $parameters * The query parameters. * @param string $langcode @@ -178,13 +134,14 @@ public function query( * @return array * An array of entities. */ - private function request(array $parameters, string $langcode) : array { + protected function request(string $endpoint, array $parameters, string $langcode) : array { if (!$this->environment) { return []; } try { - $uri = vsprintf('%s/jsonapi/node/announcement?%s', [ + $uri = vsprintf('%s/jsonapi%s?%s', [ $this->environment->getInternalAddress($langcode), + $endpoint, Query::build($parameters), ]); $content = $this->client->request('GET', $uri); diff --git a/modules/helfi_global_announcement/helfi_global_announcement.info.yml b/modules/helfi_global_announcement/helfi_global_announcement.info.yml index 319f87b6c..f1d8b173e 100644 --- a/modules/helfi_global_announcement/helfi_global_announcement.info.yml +++ b/modules/helfi_global_announcement/helfi_global_announcement.info.yml @@ -4,3 +4,4 @@ core_version_requirement: '^9 || ^10' dependencies: - external_entities:external_entities - helfi_node_announcement:helfi_node_announcement + - helfi_platform_config:helfi_etusivu_entities diff --git a/modules/helfi_global_announcement/helfi_global_announcement.install b/modules/helfi_global_announcement/helfi_global_announcement.install index e9657f8b2..a17f0a708 100644 --- a/modules/helfi_global_announcement/helfi_global_announcement.install +++ b/modules/helfi_global_announcement/helfi_global_announcement.install @@ -69,3 +69,17 @@ function helfi_global_announcement_update_9004() : void { \Drupal::service('helfi_platform_config.config_update_helper') ->update('helfi_global_announcement'); } + +/** + * UHF-9708: Enable helfi_node_survey module. + */ +function helfi_global_announcement_update_9309() : void { + \Drupal::configFactory()->getEditable('external_entities.external_entity_type.helfi_announcements')->delete(); + + $module_installer = \Drupal::service('module_installer'); + + // Enable helfi_node_survey module. + if (!\Drupal::moduleHandler()->moduleExists('helfi_etusivu_entities')) { + $module_installer->install(['helfi_etusivu_entities']); + } +} diff --git a/modules/helfi_global_announcement/helfi_global_announcement.module b/modules/helfi_global_announcement/helfi_global_announcement.module index e2257b36d..b8e086eae 100644 --- a/modules/helfi_global_announcement/helfi_global_announcement.module +++ b/modules/helfi_global_announcement/helfi_global_announcement.module @@ -8,10 +8,7 @@ declare(strict_types=1); use Drupal\Core\Cache\Cache; -use Drupal\Core\Entity\EntityTypeInterface; -use Drupal\Core\StringTranslation\TranslatableMarkup; -use Drupal\entity\BundleFieldDefinition; -use Drupal\helfi_global_announcement\Plugin\ExternalEntities\StorageClient\Announcements; +use Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient\Announcements; /** * Gets the block configurations. @@ -62,39 +59,6 @@ function helfi_global_announcement_get_block_configurations(string $theme) : arr ]; } -/** - * Implements hook_entity_bundle_field_info_alter(). - */ -function helfi_global_announcement_entity_bundle_field_info_alter( - &$fields, - EntityTypeInterface $entity_type, -) : void { - if ($entity_type->id() === 'helfi_announcements') { - - // Additional entity info fields. - $entity_info_fields = [ - 'published_at' => new TranslatableMarkup('Published at'), - 'unpublish_on' => new TranslatableMarkup('Unpublish on'), - 'notification' => new TranslatableMarkup('Notification'), - 'langcode' => new TranslatableMarkup('Langcode'), - 'body' => new TranslatableMarkup('Body'), - 'status' => new TranslatableMarkup('Status'), - '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'), - ]; - - 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); - } - } -} - /** * Implements hook_cron(). */ diff --git a/modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php b/modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php index c4d982548..d604b237b 100644 --- a/modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php +++ b/modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php @@ -7,7 +7,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\external_entities\ExternalEntityStorageInterface; -use Drupal\helfi_global_announcement\Plugin\ExternalEntities\StorageClient\Announcements; +use Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient\Announcements; use Drupal\helfi_node_announcement\Plugin\Block\AnnouncementsBlockBase; use Drupal\node\Entity\Node; From 338a38901e9e9224b54125947c631f896a315750 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Thu, 13 Jun 2024 08:57:17 +0300 Subject: [PATCH 13/33] UHF-9708: Add surveys external entity --- ...ies.external_entity_type.helfi_surveys.yml | 39 ++++++++++++ .../StorageClient/Surveys.php | 62 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 modules/helfi_etusivu_entities/config/install/external_entities.external_entity_type.helfi_surveys.yml create mode 100644 modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php diff --git a/modules/helfi_etusivu_entities/config/install/external_entities.external_entity_type.helfi_surveys.yml b/modules/helfi_etusivu_entities/config/install/external_entities.external_entity_type.helfi_surveys.yml new file mode 100644 index 000000000..e3054f3ca --- /dev/null +++ b/modules/helfi_etusivu_entities/config/install/external_entities.external_entity_type.helfi_surveys.yml @@ -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 diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php new file mode 100644 index 000000000..05e0378be --- /dev/null +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php @@ -0,0 +1,62 @@ + 'id,langcode,status,published_at,unpublish_on,title,body,field_survey_link', + 'fields[status]' => 1, + 'filter[status][value]' => 1, + 'filter[field_survey_all_pages][value]' => 1, + ]; + + if ($start) { + $query['page[offset]'] = $start; + } + + if ($length) { + $query['page[limit]'] = $length; + } + + // If filter is missing language set it manually. + if (!isset($query['filter[langcode]'])) { + $language = $this->languageManager + ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) + ->getId(); + $query['filter[langcode]'] = $language; + } + + return $this->request("/node/survey", $query, $query['filter[langcode]']); + } + +} From 41142c5438625b31b203c1ff0734fc28d4a7f4f7 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Fri, 14 Jun 2024 08:43:53 +0300 Subject: [PATCH 14/33] UHF-9708: Use constructor property promotion --- .../Plugin/Block/AnnouncementsBlockBase.php | 48 ++++--------------- 1 file changed, 8 insertions(+), 40 deletions(-) diff --git a/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlockBase.php b/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlockBase.php index 4d7b34bb1..90f3f4617 100644 --- a/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlockBase.php +++ b/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlockBase.php @@ -21,34 +21,6 @@ abstract class AnnouncementsBlockBase extends BlockBase implements ContainerFact public const VISIBILITY_REGION_WEIGHT = 1; public const VISIBILITY_PAGE_WEIGHT = 2; - /** - * The current route match. - * - * @var \Drupal\Core\Routing\RouteMatchInterface - */ - protected RouteMatchInterface $routeMatch; - - /** - * The node storage. - * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface - */ - protected EntityTypeManagerInterface $entityTypeManager; - - /** - * The language manager. - * - * @var \Drupal\Core\Language\LanguageManagerInterface - */ - protected LanguageManagerInterface $languageManager; - - /** - * Default language resolver. - * - * @var \Drupal\helfi_api_base\Language\DefaultLanguageResolver - */ - protected DefaultLanguageResolver $defaultLanguageResolver; - /** * Constructs a new AnnouncementsBlock instance. * @@ -58,29 +30,25 @@ abstract class AnnouncementsBlockBase extends BlockBase implements ContainerFact * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch * The current route match. - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. - * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager * The language manager. - * @param \Drupal\helfi_api_base\Language\DefaultLanguageResolver $default_language_resolver + * @param \Drupal\helfi_api_base\Language\DefaultLanguageResolver $defaultLanguageResolver * Default language resolver. */ public function __construct( array $configuration, $plugin_id, $plugin_definition, - RouteMatchInterface $route_match, - EntityTypeManagerInterface $entity_type_manager, - LanguageManagerInterface $language_manager, - DefaultLanguageResolver $default_language_resolver, + protected RouteMatchInterface $routeMatch, + protected EntityTypeManagerInterface $entityTypeManager, + protected LanguageManagerInterface $languageManager, + protected DefaultLanguageResolver $defaultLanguageResolver, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->routeMatch = $route_match; - $this->entityTypeManager = $entity_type_manager; - $this->languageManager = $language_manager; - $this->defaultLanguageResolver = $default_language_resolver; } /** From b709ce30d6432d6176c0f333e50760632a709cb0 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Fri, 14 Jun 2024 08:43:56 +0300 Subject: [PATCH 15/33] UHF-9708: Add block for surveys --- .../helfi_etusivu_entities.info.yml | 4 +- .../helfi_etusivu_entities.install | 58 +++++ .../helfi_etusivu_entities.module | 105 +++++++++ .../Plugin/Block/EtusivuEntityBlockBase.php | 66 ++++++ .../Plugin/Block/GlobalAnnouncementsBlock.php | 29 ++- .../src/Plugin/Block/GlobalSurveysBlock.php | 80 +++++++ modules/helfi_global_announcement/README.md | 9 - .../helfi_global_announcement.install | 65 +----- .../helfi_global_announcement.module | 80 ------- ...s.jsonapi_resource_config.node--survey.yml | 215 ++++++++++++++++++ 10 files changed, 541 insertions(+), 170 deletions(-) create mode 100644 modules/helfi_etusivu_entities/helfi_etusivu_entities.install create mode 100644 modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php rename modules/{helfi_global_announcement => helfi_etusivu_entities}/src/Plugin/Block/GlobalAnnouncementsBlock.php (73%) create mode 100644 modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php delete mode 100644 modules/helfi_global_announcement/README.md delete mode 100644 modules/helfi_global_announcement/helfi_global_announcement.module create mode 100644 modules/helfi_node_survey/config/optional/jsonapi_extras.jsonapi_resource_config.node--survey.yml diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml b/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml index 5480694c2..b8c69827d 100644 --- a/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml @@ -1,5 +1,7 @@ -name: 'HELfi Remote Entities' +name: 'HELfi Etusivu Entities' type: module core_version_requirement: ^10 dependencies: - external_entities:external_entities + - helfi_api_base:helfi_api_base + - helfi_platform_config:helfi_platform_config diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.install b/modules/helfi_etusivu_entities/helfi_etusivu_entities.install new file mode 100644 index 000000000..fdc12cc6b --- /dev/null +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.install @@ -0,0 +1,58 @@ + $permissions, + 'authenticated' => $permissions, + ]); +} + +/** + * Added external entity for announcements and enable block. + */ +function _helfi_etusivu_entities_create_block() : 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); + } +} diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module index f8a664b80..0e66ed5de 100644 --- a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module @@ -7,9 +7,69 @@ declare(strict_types=1); +use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\entity\BundleFieldDefinition; +use Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient\Announcements; +use Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient\Surveys; + +/** + * Gets the block configurations. + * + * @return array[] + * The block configurations. + */ +function helfi_etusivu_entities_get_block_configurations(string $theme) : array { + return [ + 'global_announcement' => [ + 'block' => [ + 'id' => 'globalannouncement', + 'plugin' => 'global_announcements', + 'settings' => [ + 'id' => 'global_announcements', + 'label' => 'Global announcement', + 'provider' => 'helfi_etusivu_entities', + ], + 'provider' => 'helfi_etusivu_entities', + 'weight' => -14, + ], + 'variations' => [ + [ + 'theme' => $theme, + 'region' => 'before_content', + ], + [ + 'theme' => 'stark', + 'region' => 'content', + ], + ], + ], + 'global_survey' => [ + 'block' => [ + 'id' => 'globalsurvey', + 'plugin' => 'global_surveys', + 'settings' => [ + 'id' => 'global_surveys', + 'label' => 'Global surveys', + 'provider' => 'helfi_etusivu_entities', + ], + 'provider' => 'helfi_etusivu_entities', + 'weight' => -14, + ], + 'variations' => [ + [ + 'theme' => $theme, + 'region' => 'before_content', + ], + [ + 'theme' => 'stark', + 'region' => 'content', + ], + ], + ], + ]; +} /** * Implements hook_entity_bundle_field_info_alter(). @@ -57,3 +117,48 @@ function helfi_etusivu_entities_entity_bundle_field_info_alter( } } } + +/** + * Implements hook_cron(). + */ +function helfi_etusivu_entities_cron(): void { + // @todo Remove this once we've confirmed that PubSub cache invalidation works. + $store = \Drupal::service('tempstore.shared')->get('etusivu_entities'); + $externalEntityTypes = [ + "helfi_announcements" => Announcements::$customCacheTag, + "helfi_surveys" => Surveys::$customCacheTag, + ]; + + foreach ($externalEntityTypes as $entityTypeId => $cacheTag) { + $newHash = _helfi_etusivu_entities_hash($entityTypeId); + $oldHash = $store->get($entityTypeId); + + // External entities have changed if hashes do not match. + if ($oldHash !== $newHash) { + $store->set($entityTypeId, $newHash); + Cache::invalidateTags([Announcements::$customCacheTag]); + } + } +} + +/** + * Gets hash of remote entities. + * + * @param string $entityTypeId + * Entity type id. + * + * @return string + * Sha256 of serialized remote entities. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ +function _helfi_etusivu_entities_hash(string $entityTypeId): string { + $globalEntityStorage = \Drupal::entityTypeManager() + ->getStorage($entityTypeId); + + $externalAnnouncements = $globalEntityStorage + ->loadMultiple(); + + return hash('sha256', serialize($externalAnnouncements)); +} diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php b/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php new file mode 100644 index 000000000..a10f0e018 --- /dev/null +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php @@ -0,0 +1,66 @@ +get('entity_type.manager'), + ); + } + + /** + * Get global entity storage. + * + * @param string $entityTypeId + * External entity type. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + protected function getGlobalEntityStorage(string $entityTypeId): ExternalEntityStorageInterface { + $globalEntityStorage = $this->entityTypeManager->getStorage($entityTypeId); + if ($globalEntityStorage instanceof ExternalEntityStorageInterface) { + return $globalEntityStorage; + } + + throw new \InvalidArgumentException("$entityTypeId is not external entity type"); + } + +} diff --git a/modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php similarity index 73% rename from modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php rename to modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php index d604b237b..52be5f012 100644 --- a/modules/helfi_global_announcement/src/Plugin/Block/GlobalAnnouncementsBlock.php +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php @@ -1,14 +1,14 @@ entityTypeManager->getStorage('helfi_announcements'); - assert($globalEntityStorage instanceof ExternalEntityStorageInterface); - $cacheMaxAge = $globalEntityStorage->getExternalEntityType()->get('persistent_cache_max_age'); - - $externalAnnouncements = $globalEntityStorage - ->loadMultiple(); - + $entityStorage = $this->getGlobalEntityStorage('helfi_announcements'); $announcementNodes = []; + /** @var \Drupal\external_entities\ExternalEntityInterface $announcement */ - foreach ($externalAnnouncements as $announcement) { + foreach ($entityStorage->loadMultiple() as $announcement) { $linkUrl = NULL; $linkText = NULL; if ($announcement->hasField('announcement_link_text')) { @@ -45,21 +40,23 @@ public function build(): array { $announcementNodes[] = Node::create([ 'uuid' => $announcement->get('uuid')->value, 'type' => 'announcement', - 'field_announcement_type' => $announcement->get('announcement_type')->value, - 'field_announcement_link' => ['uri' => $linkUrl, 'title' => $linkText], 'langcode' => $announcement->get('langcode')->value, 'body' => Xss::filter($announcement->get('body')->value), 'title' => Xss::filter($announcement->get('title')->value), 'status' => $announcement->get('status')->value, 'field_announcement_title' => $announcement->get('announcement_assistive_technology_close_button_title')->value, + 'field_announcement_type' => $announcement->get('announcement_type')->value, + 'field_announcement_link' => ['uri' => $linkUrl, 'title' => $linkText], ]); } $viewMode = 'default'; - $renderArray = $this->entityTypeManager->getViewBuilder('node')->viewMultiple($announcementNodes, $viewMode); + $renderArray = $this->entityTypeManager + ->getViewBuilder('node') + ->viewMultiple($announcementNodes, $viewMode); $renderArray['#cache'] = [ - 'max-age' => $cacheMaxAge, + 'max-age' => $entityStorage->getExternalEntityType()->get('persistent_cache_max_age'), 'tags' => [ Announcements::$customCacheTag, ], diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php new file mode 100644 index 000000000..3524fc373 --- /dev/null +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php @@ -0,0 +1,80 @@ +getGlobalEntityStorage('helfi_surveys'); + $nodes = []; + + /** @var \Drupal\external_entities\ExternalEntityInterface $entity */ + foreach ($entityStorage->loadMultiple() as $entity) { + $linkUrl = NULL; + $linkText = NULL; + if ($entity->hasField('survey_link_text')) { + $linkText = $entity->get('survey_link_text')->value; + $linkUrl = $entity->get('survey_link_url')->value; + } + + // Create announcement nodes for the block based on external entity data. + $nodes[] = Node::create([ + 'uuid' => $entity->get('uuid')->value, + 'type' => 'survey', + 'field_survey_link' => ['uri' => $linkUrl, 'title' => $linkText], + 'langcode' => $entity->get('langcode')->value, + 'body' => Xss::filter($entity->get('body')->value), + 'title' => Xss::filter($entity->get('title')->value), + 'status' => $entity->get('status')->value, + ]); + } + + $viewMode = 'default'; + $renderArray = $this->entityTypeManager->getViewBuilder('node')->viewMultiple($nodes, $viewMode); + $renderArray['#cache'] = [ + 'max-age' => $entityStorage->getExternalEntityType()->get('persistent_cache_max_age'), + 'tags' => [ + Surveys::$customCacheTag, + ], + ]; + + return $renderArray; + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts(): array { + return Cache::mergeContexts(parent::getCacheContexts(), [ + 'url.path', + 'languages:language_content', + ]); + } + + /** + * {@inheritdoc} + */ + public function getCacheTags(): array { + return Cache::mergeTags(parent::getCacheTags(), ['node_list:survey']); + } + +} diff --git a/modules/helfi_global_announcement/README.md b/modules/helfi_global_announcement/README.md deleted file mode 100644 index a96ea1f32..000000000 --- a/modules/helfi_global_announcement/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Global announcement - -Global announcement module allows fetching announcements from `Etusivu`-instance. -It utilizes `json-api` and `external_entities`-module to transfer the data between instances. -Global announcements can be fetched to any instance and rendered using blocks. - -## How to set up locally - -Local setup requires Etusivu-instance to be up and running with some relevant data created to it. diff --git a/modules/helfi_global_announcement/helfi_global_announcement.install b/modules/helfi_global_announcement/helfi_global_announcement.install index a17f0a708..296ecbe23 100644 --- a/modules/helfi_global_announcement/helfi_global_announcement.install +++ b/modules/helfi_global_announcement/helfi_global_announcement.install @@ -8,70 +8,7 @@ declare(strict_types=1); /** - * Implements hook_install(). - */ -function helfi_global_announcement_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_global_announcement_grant_permissions(); - _helfi_global_announcement_create_block(); -} - -/** - * Grants required permissions. - */ -function helfi_global_announcement_grant_permissions() : void { - $permissions = [ - 'view helfi_announcements external entity', - ]; - - helfi_platform_config_grant_permissions([ - 'anonymous' => $permissions, - 'authenticated' => $permissions, - ]); -} - -/** - * Added external entity for announcements and enable block. - */ -function _helfi_global_announcement_create_block() : 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(); - $block_config = helfi_global_announcement_get_block_configurations($theme)['global_announcement']; - ['block' => $block, 'variations' => $variations] = $block_config; - $block_installer->install($block, $variations); -} - -/** - * Grant required permissions. - */ -function helfi_global_announcement_update_9001() : void { - helfi_global_announcement_grant_permissions(); -} - -/** - * Re-install configuration to update cache lifetime. - */ -function helfi_global_announcement_update_9004() : void { - \Drupal::service('helfi_platform_config.config_update_helper') - ->update('helfi_global_announcement'); -} - -/** - * UHF-9708: Enable helfi_node_survey module. + * UHF-9708: Enable helfi_etusivu_entities module. */ function helfi_global_announcement_update_9309() : void { \Drupal::configFactory()->getEditable('external_entities.external_entity_type.helfi_announcements')->delete(); diff --git a/modules/helfi_global_announcement/helfi_global_announcement.module b/modules/helfi_global_announcement/helfi_global_announcement.module deleted file mode 100644 index b8e086eae..000000000 --- a/modules/helfi_global_announcement/helfi_global_announcement.module +++ /dev/null @@ -1,80 +0,0 @@ - [ - * 'block' => [ - * ... - * ], - * 'variations' => [ - * ... - * ], - * ], - * ]; - * @endcode - * - * @return array[] - * The block configurations. - */ -function helfi_global_announcement_get_block_configurations(string $theme) : array { - return [ - 'global_announcement' => [ - 'block' => [ - 'id' => 'globalannouncement', - 'plugin' => 'global_announcements', - 'settings' => [ - 'id' => 'global_announcements', - 'label' => 'Global announcement', - 'provider' => 'helfi_global_announcement', - ], - 'provider' => 'helfi_global_announcement', - 'weight' => -14, - ], - 'variations' => [ - [ - 'theme' => $theme, - 'region' => 'before_content', - ], - [ - 'theme' => 'stark', - 'region' => 'content', - ], - ], - ], - ]; -} - -/** - * Implements hook_cron(). - */ -function helfi_global_announcement_cron(): void { - // @todo Remove this once we've confirmed that PubSub cache invalidation works. - $store = \Drupal::service('tempstore.shared')->get('global_announcement'); - $globalEntityStorage = \Drupal::entityTypeManager() - ->getStorage('helfi_announcements'); - - $externalAnnouncements = $globalEntityStorage - ->loadMultiple(); - - $newHash = hash('sha256', serialize($externalAnnouncements)); - $hash = $store->get('announcements'); - if ($hash !== $newHash) { - $store->set('announcements', $newHash); - Cache::invalidateTags([Announcements::$customCacheTag]); - } -} diff --git a/modules/helfi_node_survey/config/optional/jsonapi_extras.jsonapi_resource_config.node--survey.yml b/modules/helfi_node_survey/config/optional/jsonapi_extras.jsonapi_resource_config.node--survey.yml new file mode 100644 index 000000000..f8b410ecc --- /dev/null +++ b/modules/helfi_node_survey/config/optional/jsonapi_extras.jsonapi_resource_config.node--survey.yml @@ -0,0 +1,215 @@ +uuid: 1d49642a-fab8-4c74-ac95-ab4a509893db +langcode: fi +status: true +dependencies: + config: + - node.type.survey +id: node--survey +disabled: false +path: node/survey +resourceType: node--survey +resourceFields: + nid: + disabled: false + fieldName: nid + publicName: nid + enhancer: + id: '' + uuid: + disabled: false + fieldName: uuid + publicName: uuid + enhancer: + id: '' + vid: + disabled: false + fieldName: vid + publicName: vid + enhancer: + id: '' + langcode: + disabled: false + fieldName: langcode + publicName: langcode + enhancer: + id: '' + type: + disabled: false + fieldName: type + publicName: type + enhancer: + id: '' + revision_timestamp: + disabled: false + fieldName: revision_timestamp + publicName: revision_timestamp + enhancer: + id: '' + revision_uid: + disabled: false + fieldName: revision_uid + publicName: revision_uid + enhancer: + id: '' + revision_log: + disabled: false + fieldName: revision_log + publicName: revision_log + enhancer: + id: '' + status: + disabled: false + fieldName: status + publicName: status + enhancer: + id: '' + uid: + disabled: false + fieldName: uid + publicName: uid + enhancer: + id: '' + title: + disabled: false + fieldName: title + publicName: title + enhancer: + id: '' + created: + disabled: false + fieldName: created + publicName: created + enhancer: + id: '' + changed: + disabled: false + fieldName: changed + publicName: changed + enhancer: + id: '' + promote: + disabled: false + fieldName: promote + publicName: promote + enhancer: + id: '' + sticky: + disabled: false + fieldName: sticky + publicName: sticky + enhancer: + id: '' + default_langcode: + disabled: false + fieldName: default_langcode + publicName: default_langcode + enhancer: + id: '' + revision_default: + disabled: false + fieldName: revision_default + publicName: revision_default + enhancer: + id: '' + revision_translation_affected: + disabled: false + fieldName: revision_translation_affected + publicName: revision_translation_affected + enhancer: + id: '' + color_palette: + disabled: false + fieldName: color_palette + publicName: color_palette + enhancer: + id: '' + hide_sidebar_navigation: + disabled: false + fieldName: hide_sidebar_navigation + publicName: hide_sidebar_navigation + enhancer: + id: '' + toc_enabled: + disabled: false + fieldName: toc_enabled + publicName: toc_enabled + enhancer: + id: '' + toc_title: + disabled: false + fieldName: toc_title + publicName: toc_title + enhancer: + id: '' + metatag: + disabled: false + fieldName: metatag + publicName: metatag + enhancer: + id: '' + path: + disabled: false + fieldName: path + publicName: path + enhancer: + id: '' + publish_on: + disabled: false + fieldName: publish_on + publicName: publish_on + enhancer: + id: '' + unpublish_on: + disabled: false + fieldName: unpublish_on + publicName: unpublish_on + enhancer: + id: '' + menu_link: + disabled: false + fieldName: menu_link + publicName: menu_link + enhancer: + id: '' + content_translation_source: + disabled: false + fieldName: content_translation_source + publicName: content_translation_source + enhancer: + id: '' + content_translation_outdated: + disabled: false + fieldName: content_translation_outdated + publicName: content_translation_outdated + enhancer: + id: '' + published_at: + disabled: false + fieldName: published_at + publicName: published_at + enhancer: + id: '' + body: + disabled: false + fieldName: body + publicName: body + enhancer: + id: '' + field_survey_all_pages: + disabled: false + fieldName: field_survey_all_pages + publicName: field_survey_all_pages + enhancer: + id: '' + field_survey_content_pages: + disabled: false + fieldName: field_survey_content_pages + publicName: field_survey_content_pages + enhancer: + id: '' + field_survey_link: + disabled: false + fieldName: field_survey_link + publicName: field_survey_link + enhancer: + id: '' From 8edfda394924763f005c6f88c3807376f5894783 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Fri, 14 Jun 2024 08:43:57 +0300 Subject: [PATCH 16/33] UHF-9708: Invalidate cache tags with pubsub service --- .../helfi_etusivu_entities.module | 83 +++++++++++-------- .../Plugin/Block/GlobalAnnouncementsBlock.php | 7 -- .../src/Plugin/Block/GlobalSurveysBlock.php | 7 -- 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module index 0e66ed5de..ffd322128 100644 --- a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module @@ -7,10 +7,13 @@ declare(strict_types=1); -use Drupal\Core\Cache\Cache; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\entity\BundleFieldDefinition; +use Drupal\helfi_api_base\Cache\CacheTagInvalidatorInterface; +use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface; +use Drupal\helfi_api_base\Environment\Project; use Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient\Announcements; use Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient\Surveys; @@ -119,46 +122,60 @@ function helfi_etusivu_entities_entity_bundle_field_info_alter( } /** - * Implements hook_cron(). + * Implements hook_node_insert(). */ -function helfi_etusivu_entities_cron(): void { - // @todo Remove this once we've confirmed that PubSub cache invalidation works. - $store = \Drupal::service('tempstore.shared')->get('etusivu_entities'); - $externalEntityTypes = [ - "helfi_announcements" => Announcements::$customCacheTag, - "helfi_surveys" => Surveys::$customCacheTag, - ]; +function helfi_etusivu_entities_node_insert(EntityInterface $entity): void { + _helfi_etusivu_entities_invalidate_cache($entity); +} - foreach ($externalEntityTypes as $entityTypeId => $cacheTag) { - $newHash = _helfi_etusivu_entities_hash($entityTypeId); - $oldHash = $store->get($entityTypeId); +/** + * Implements hook_node_update(). + */ +function helfi_etusivu_entities_node_update(EntityInterface $entity): void { + _helfi_etusivu_entities_invalidate_cache($entity); +} - // External entities have changed if hashes do not match. - if ($oldHash !== $newHash) { - $store->set($entityTypeId, $newHash); - Cache::invalidateTags([Announcements::$customCacheTag]); - } - } +/** + * Implements hook_node_delete(). + */ +function helfi_etusivu_entities_node_delete(EntityInterface $entity): void { + _helfi_etusivu_entities_invalidate_cache($entity); } /** - * Gets hash of remote entities. - * - * @param string $entityTypeId - * Entity type id. + * Invalidate cache tags using pubsub. * - * @return string - * Sha256 of serialized remote entities. - * - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException - * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * @param EntityInterface $entity + * The entity. */ -function _helfi_etusivu_entities_hash(string $entityTypeId): string { - $globalEntityStorage = \Drupal::entityTypeManager() - ->getStorage($entityTypeId); +function _helfi_etusivu_entities_invalidate_cache(EntityInterface $entity): void { + $cacheTags = [ + 'survey' => Surveys::$customCacheTag, + 'announcement' => Announcements::$customCacheTag, + ]; + + if ($entity->getEntityTypeId() !== 'node' || !array_key_exists($entity->bundle(), $cacheTags)) { + return; + } + + try { + /** @var EnvironmentResolverInterface $environmentResolver */ + $environmentResolver = \Drupal::service(EnvironmentResolverInterface::class); - $externalAnnouncements = $globalEntityStorage - ->loadMultiple(); + // Only run on etusivu. + if ($environmentResolver->getActiveProject()->getName() == Project::ETUSIVU) { + $cacheTag = $cacheTags[$entity->bundle()]; + + /** @var CacheTagInvalidatorInterface $cacheTagInvalidator */ + $cacheTagInvalidator = \Drupal::service(CacheTagInvalidatorInterface::class); + + // Invalidate external entities from all instances. + $cacheTagInvalidator->invalidateTags([ + $cacheTag + ]); + } + } + catch (\InvalidArgumentException) { + } - return hash('sha256', serialize($externalAnnouncements)); } diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php index 52be5f012..fb80a74db 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php @@ -75,11 +75,4 @@ public function getCacheContexts(): array { ]); } - /** - * {@inheritdoc} - */ - public function getCacheTags(): array { - return Cache::mergeTags(parent::getCacheTags(), ['node_list:announcement']); - } - } diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php index 3524fc373..c4ef287e9 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php @@ -70,11 +70,4 @@ public function getCacheContexts(): array { ]); } - /** - * {@inheritdoc} - */ - public function getCacheTags(): array { - return Cache::mergeTags(parent::getCacheTags(), ['node_list:survey']); - } - } From 125f77a07778a27475229054cefd92335a6f946c Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Fri, 14 Jun 2024 08:56:05 +0300 Subject: [PATCH 17/33] UHF-9708: Remove duplicated code --- .../StorageClient/Announcements.php | 19 ++-------- .../StorageClient/EtusivuEntityBase.php | 36 +++++++++++++++++++ .../StorageClient/Surveys.php | 19 ++-------- 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php index db8d54966..dd0fc5226 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php @@ -4,8 +4,6 @@ namespace Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient; -use Drupal\Core\Language\LanguageInterface; - /** * External entity storage client for News feed entities. * @@ -38,21 +36,8 @@ public function query( 'filter[field_publish_externally][value]' => 1, ]; - if ($start) { - $query['page[offset]'] = $start; - } - - if ($length) { - $query['page[limit]'] = $length; - } - - // If filter is missing language set it manually. - if (!isset($query['filter[langcode]'])) { - $language = $this->languageManager - ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) - ->getId(); - $query['filter[langcode]'] = $language; - } + $query += $this->queryLimits($start, $length); + $query += $this->queryDefaultLangcode(); return $this->request("/node/announcement", $query, $query['filter[langcode]']); } diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php index 4a2cd3c20..406d22725 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php @@ -5,6 +5,7 @@ namespace Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient; use Drupal\Core\Entity\EntityStorageException; +use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Utility\Error; use Drupal\external_entities\ExternalEntityInterface; @@ -154,4 +155,39 @@ protected function request(string $endpoint, array $parameters, string $langcode return []; } + /** + * Get limits query. + * + * @param int|null $start + * The first item to return. + * @param int|null $length + * The number of items to return. + */ + protected function queryLimits(?int $start, ?int $length): array { + $query = []; + + if ($start) { + $query['page[offset]'] = $start; + } + + if ($length) { + $query['page[limit]'] = $length; + } + + return $query; + } + + /** + * Get default langcode query. + */ + protected function queryDefaultLangcode(): array { + $language = $this->languageManager + ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) + ->getId(); + + return [ + 'filter[langcode]' => $language, + ]; + } + } diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php index 05e0378be..7f8e0f446 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php @@ -4,8 +4,6 @@ namespace Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient; -use Drupal\Core\Language\LanguageInterface; - /** * External entity storage client for News feed entities. * @@ -40,21 +38,8 @@ public function query( 'filter[field_survey_all_pages][value]' => 1, ]; - if ($start) { - $query['page[offset]'] = $start; - } - - if ($length) { - $query['page[limit]'] = $length; - } - - // If filter is missing language set it manually. - if (!isset($query['filter[langcode]'])) { - $language = $this->languageManager - ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) - ->getId(); - $query['filter[langcode]'] = $language; - } + $query += $this->queryLimits($start, $length); + $query += $this->queryDefaultLangcode(); return $this->request("/node/survey", $query, $query['filter[langcode]']); } From 7fed9b9476f2b042c15e940b631c570293cc4033 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Fri, 14 Jun 2024 08:57:45 +0300 Subject: [PATCH 18/33] UHF-9708: PHPCS fixes --- .../helfi_etusivu_entities.module | 10 ++++------ .../StorageClient/EtusivuEntityBase.php | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module index ffd322128..f21f316a3 100644 --- a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module @@ -145,7 +145,7 @@ function helfi_etusivu_entities_node_delete(EntityInterface $entity): void { /** * Invalidate cache tags using pubsub. * - * @param EntityInterface $entity + * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. */ function _helfi_etusivu_entities_invalidate_cache(EntityInterface $entity): void { @@ -159,20 +159,18 @@ function _helfi_etusivu_entities_invalidate_cache(EntityInterface $entity): void } try { - /** @var EnvironmentResolverInterface $environmentResolver */ + /** @var \Drupal\helfi_api_base\Environment\EnvironmentResolverInterface $environmentResolver */ $environmentResolver = \Drupal::service(EnvironmentResolverInterface::class); // Only run on etusivu. if ($environmentResolver->getActiveProject()->getName() == Project::ETUSIVU) { $cacheTag = $cacheTags[$entity->bundle()]; - /** @var CacheTagInvalidatorInterface $cacheTagInvalidator */ + /** @var \Drupal\helfi_api_base\Cache\CacheTagInvalidatorInterface $cacheTagInvalidator */ $cacheTagInvalidator = \Drupal::service(CacheTagInvalidatorInterface::class); // Invalidate external entities from all instances. - $cacheTagInvalidator->invalidateTags([ - $cacheTag - ]); + $cacheTagInvalidator->invalidateTags([$cacheTag]); } } catch (\InvalidArgumentException) { diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php index 406d22725..1dd5883d9 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php @@ -161,7 +161,7 @@ protected function request(string $endpoint, array $parameters, string $langcode * @param int|null $start * The first item to return. * @param int|null $length - * The number of items to return. + * The number of items to return. */ protected function queryLimits(?int $start, ?int $length): array { $query = []; From 9edecd942d06e9365feef204026ade1defbe69b6 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Fri, 14 Jun 2024 09:46:12 +0300 Subject: [PATCH 19/33] UHF-9708: Remove jsonapi_extras configuration This is only needed by etusivu --- ...s.jsonapi_resource_config.node--survey.yml | 215 ------------------ 1 file changed, 215 deletions(-) delete mode 100644 modules/helfi_node_survey/config/optional/jsonapi_extras.jsonapi_resource_config.node--survey.yml diff --git a/modules/helfi_node_survey/config/optional/jsonapi_extras.jsonapi_resource_config.node--survey.yml b/modules/helfi_node_survey/config/optional/jsonapi_extras.jsonapi_resource_config.node--survey.yml deleted file mode 100644 index f8b410ecc..000000000 --- a/modules/helfi_node_survey/config/optional/jsonapi_extras.jsonapi_resource_config.node--survey.yml +++ /dev/null @@ -1,215 +0,0 @@ -uuid: 1d49642a-fab8-4c74-ac95-ab4a509893db -langcode: fi -status: true -dependencies: - config: - - node.type.survey -id: node--survey -disabled: false -path: node/survey -resourceType: node--survey -resourceFields: - nid: - disabled: false - fieldName: nid - publicName: nid - enhancer: - id: '' - uuid: - disabled: false - fieldName: uuid - publicName: uuid - enhancer: - id: '' - vid: - disabled: false - fieldName: vid - publicName: vid - enhancer: - id: '' - langcode: - disabled: false - fieldName: langcode - publicName: langcode - enhancer: - id: '' - type: - disabled: false - fieldName: type - publicName: type - enhancer: - id: '' - revision_timestamp: - disabled: false - fieldName: revision_timestamp - publicName: revision_timestamp - enhancer: - id: '' - revision_uid: - disabled: false - fieldName: revision_uid - publicName: revision_uid - enhancer: - id: '' - revision_log: - disabled: false - fieldName: revision_log - publicName: revision_log - enhancer: - id: '' - status: - disabled: false - fieldName: status - publicName: status - enhancer: - id: '' - uid: - disabled: false - fieldName: uid - publicName: uid - enhancer: - id: '' - title: - disabled: false - fieldName: title - publicName: title - enhancer: - id: '' - created: - disabled: false - fieldName: created - publicName: created - enhancer: - id: '' - changed: - disabled: false - fieldName: changed - publicName: changed - enhancer: - id: '' - promote: - disabled: false - fieldName: promote - publicName: promote - enhancer: - id: '' - sticky: - disabled: false - fieldName: sticky - publicName: sticky - enhancer: - id: '' - default_langcode: - disabled: false - fieldName: default_langcode - publicName: default_langcode - enhancer: - id: '' - revision_default: - disabled: false - fieldName: revision_default - publicName: revision_default - enhancer: - id: '' - revision_translation_affected: - disabled: false - fieldName: revision_translation_affected - publicName: revision_translation_affected - enhancer: - id: '' - color_palette: - disabled: false - fieldName: color_palette - publicName: color_palette - enhancer: - id: '' - hide_sidebar_navigation: - disabled: false - fieldName: hide_sidebar_navigation - publicName: hide_sidebar_navigation - enhancer: - id: '' - toc_enabled: - disabled: false - fieldName: toc_enabled - publicName: toc_enabled - enhancer: - id: '' - toc_title: - disabled: false - fieldName: toc_title - publicName: toc_title - enhancer: - id: '' - metatag: - disabled: false - fieldName: metatag - publicName: metatag - enhancer: - id: '' - path: - disabled: false - fieldName: path - publicName: path - enhancer: - id: '' - publish_on: - disabled: false - fieldName: publish_on - publicName: publish_on - enhancer: - id: '' - unpublish_on: - disabled: false - fieldName: unpublish_on - publicName: unpublish_on - enhancer: - id: '' - menu_link: - disabled: false - fieldName: menu_link - publicName: menu_link - enhancer: - id: '' - content_translation_source: - disabled: false - fieldName: content_translation_source - publicName: content_translation_source - enhancer: - id: '' - content_translation_outdated: - disabled: false - fieldName: content_translation_outdated - publicName: content_translation_outdated - enhancer: - id: '' - published_at: - disabled: false - fieldName: published_at - publicName: published_at - enhancer: - id: '' - body: - disabled: false - fieldName: body - publicName: body - enhancer: - id: '' - field_survey_all_pages: - disabled: false - fieldName: field_survey_all_pages - publicName: field_survey_all_pages - enhancer: - id: '' - field_survey_content_pages: - disabled: false - fieldName: field_survey_content_pages - publicName: field_survey_content_pages - enhancer: - id: '' - field_survey_link: - disabled: false - fieldName: field_survey_link - publicName: field_survey_link - enhancer: - id: '' From bf8dcb18e4c116761e5e303f5b36bb921badc1d4 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Thu, 20 Jun 2024 07:39:08 +0300 Subject: [PATCH 20/33] UHF-9708: Remove cache & add cache section to readme This was implemented in helfi_etusivu_invalidate_external_caches --- modules/helfi_etusivu_entities/README.md | 4 ++ .../helfi_etusivu_entities.module | 57 ------------------- 2 files changed, 4 insertions(+), 57 deletions(-) diff --git a/modules/helfi_etusivu_entities/README.md b/modules/helfi_etusivu_entities/README.md index 61c566275..ff0009477 100644 --- a/modules/helfi_etusivu_entities/README.md +++ b/modules/helfi_etusivu_entities/README.md @@ -6,3 +6,7 @@ It utilizes `json-api` and `external_entities`-module to transfer the data betwe ## 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). diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module index f21f316a3..e10e0ae19 100644 --- a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module @@ -120,60 +120,3 @@ function helfi_etusivu_entities_entity_bundle_field_info_alter( } } } - -/** - * Implements hook_node_insert(). - */ -function helfi_etusivu_entities_node_insert(EntityInterface $entity): void { - _helfi_etusivu_entities_invalidate_cache($entity); -} - -/** - * Implements hook_node_update(). - */ -function helfi_etusivu_entities_node_update(EntityInterface $entity): void { - _helfi_etusivu_entities_invalidate_cache($entity); -} - -/** - * Implements hook_node_delete(). - */ -function helfi_etusivu_entities_node_delete(EntityInterface $entity): void { - _helfi_etusivu_entities_invalidate_cache($entity); -} - -/** - * Invalidate cache tags using pubsub. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity. - */ -function _helfi_etusivu_entities_invalidate_cache(EntityInterface $entity): void { - $cacheTags = [ - 'survey' => Surveys::$customCacheTag, - 'announcement' => Announcements::$customCacheTag, - ]; - - if ($entity->getEntityTypeId() !== 'node' || !array_key_exists($entity->bundle(), $cacheTags)) { - return; - } - - try { - /** @var \Drupal\helfi_api_base\Environment\EnvironmentResolverInterface $environmentResolver */ - $environmentResolver = \Drupal::service(EnvironmentResolverInterface::class); - - // Only run on etusivu. - if ($environmentResolver->getActiveProject()->getName() == Project::ETUSIVU) { - $cacheTag = $cacheTags[$entity->bundle()]; - - /** @var \Drupal\helfi_api_base\Cache\CacheTagInvalidatorInterface $cacheTagInvalidator */ - $cacheTagInvalidator = \Drupal::service(CacheTagInvalidatorInterface::class); - - // Invalidate external entities from all instances. - $cacheTagInvalidator->invalidateTags([$cacheTag]); - } - } - catch (\InvalidArgumentException) { - } - -} From b7d03af15b8a1a5d0e13404598986d723234b736 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Thu, 20 Jun 2024 08:04:32 +0300 Subject: [PATCH 21/33] UHF-9708: Remove field_survey_all_pages, use field_publish_externally only on etusivu --- ....announcement.field_publish_externally.yml | 23 +++++++++++++++++++ ...ntity_form_display.node.survey.default.yml | 8 ------- ...ntity_view_display.node.survey.default.yml | 3 --- ...entity_view_display.node.survey.teaser.yml | 2 -- ...ld.storage.node.field_survey_all_pages.yml | 18 --------------- ....node.survey.field_publish_externally.yml} | 12 +++++----- ....announcement.field_publish_externally.yml | 4 ++++ ...eld.node.survey.field_survey_all_pages.yml | 4 ---- ...announcement.field_publish_externally.yml} | 0 9 files changed, 33 insertions(+), 41 deletions(-) create mode 100644 modules/helfi_node_announcement/config/optional/field.field.node.announcement.field_publish_externally.yml delete mode 100644 modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml rename modules/helfi_node_survey/config/{install/field.field.node.survey.field_survey_all_pages.yml => optional/field.field.node.survey.field_publish_externally.yml} (55%) create mode 100644 modules/helfi_node_survey/config/optional/language/fi/field.field.node.announcement.field_publish_externally.yml delete mode 100644 modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_all_pages.yml rename modules/helfi_node_survey/config/optional/language/sv/{field.field.node.survey.field_survey_all_pages.yml => field.field.node.announcement.field_publish_externally.yml} (100%) diff --git a/modules/helfi_node_announcement/config/optional/field.field.node.announcement.field_publish_externally.yml b/modules/helfi_node_announcement/config/optional/field.field.node.announcement.field_publish_externally.yml new file mode 100644 index 000000000..4ce87bf72 --- /dev/null +++ b/modules/helfi_node_announcement/config/optional/field.field.node.announcement.field_publish_externally.yml @@ -0,0 +1,23 @@ +uuid: 80cab237-aac0-4195-bd3c-d86ee2ef6aeb +langcode: en +status: true +dependencies: + config: + - field.storage.node.field_publish_externally + - node.type.announcement +id: node.announcement.field_publish_externally +field_name: field_publish_externally +entity_type: node +bundle: announcement +label: 'Publish on external site' +description: '' +required: false +translatable: false +default_value: + - + value: 0 +default_value_callback: '' +settings: + on_label: 'On' + off_label: 'Off' +field_type: boolean diff --git a/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml b/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml index f6389f42f..a973639a9 100755 --- a/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml +++ b/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml @@ -4,7 +4,6 @@ status: true dependencies: config: - field.field.node.survey.body - - field.field.node.survey.field_survey_all_pages - field.field.node.survey.field_survey_content_pages - field.field.node.survey.field_survey_link - node.type.survey @@ -37,13 +36,6 @@ content: region: content settings: { } third_party_settings: { } - field_survey_all_pages: - type: boolean_checkbox - weight: 13 - region: content - settings: - display_label: true - third_party_settings: { } field_survey_content_pages: type: select2_entity_reference weight: 14 diff --git a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml index 040ec94b3..403f124f9 100644 --- a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml +++ b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml @@ -4,13 +4,11 @@ status: true dependencies: config: - field.field.node.survey.body - - field.field.node.survey.field_survey_all_pages - field.field.node.survey.field_survey_content_pages - field.field.node.survey.field_survey_link - node.type.survey module: - link - - options - text - user id: node.survey.default @@ -43,7 +41,6 @@ content: weight: 0 region: content hidden: - field_survey_all_pages: true field_survey_content_pages: true field_survey_service_pages: true field_survey_unit_pages: true diff --git a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml index 54e6acb78..647f73609 100644 --- a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml +++ b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml @@ -5,7 +5,6 @@ dependencies: config: - core.entity_view_mode.node.teaser - field.field.node.survey.body - - field.field.node.survey.field_survey_all_pages - field.field.node.survey.field_survey_content_pages - field.field.node.survey.field_survey_link - node.type.survey @@ -31,7 +30,6 @@ content: weight: 100 region: content hidden: - field_survey_all_pages: true field_survey_content_pages: true field_survey_link: true field_survey_service_pages: true diff --git a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml b/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml deleted file mode 100644 index 009c86145..000000000 --- a/modules/helfi_node_survey/config/install/field.storage.node.field_survey_all_pages.yml +++ /dev/null @@ -1,18 +0,0 @@ -uuid: 700a8e47-2a1a-4736-94de-5306cfe5148a -langcode: en -status: true -dependencies: - module: - - node -id: node.field_survey_all_pages -field_name: field_survey_all_pages -entity_type: node -type: boolean -settings: { } -module: core -locked: false -cardinality: 1 -translatable: true -indexes: { } -persist_with_no_fields: false -custom_storage: false diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml b/modules/helfi_node_survey/config/optional/field.field.node.survey.field_publish_externally.yml similarity index 55% rename from modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml rename to modules/helfi_node_survey/config/optional/field.field.node.survey.field_publish_externally.yml index 7590e04aa..31f89093c 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_all_pages.yml +++ b/modules/helfi_node_survey/config/optional/field.field.node.survey.field_publish_externally.yml @@ -1,21 +1,21 @@ -uuid: b7008a02-dd8e-42b1-b6fb-2cb4f642d9e5 +uuid: fd052f25-5c1d-400d-b22c-53960606800e langcode: en status: true dependencies: config: - - field.storage.node.field_survey_all_pages + - field.storage.node.field_publish_externally - node.type.survey -id: node.survey.field_survey_all_pages -field_name: field_survey_all_pages +id: node.survey.field_publish_externally +field_name: field_publish_externally entity_type: node bundle: survey -label: 'Show on all pages' +label: 'Publish on external site' description: '' required: false translatable: false default_value: - - value: 1 + value: 0 default_value_callback: '' settings: on_label: 'On' diff --git a/modules/helfi_node_survey/config/optional/language/fi/field.field.node.announcement.field_publish_externally.yml b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.announcement.field_publish_externally.yml new file mode 100644 index 000000000..050d38a51 --- /dev/null +++ b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.announcement.field_publish_externally.yml @@ -0,0 +1,4 @@ +label: 'Julkaise ulkoisella sivustolla ' +settings: + on_label: Päällä + off_label: 'Pois päältä' diff --git a/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_all_pages.yml b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_all_pages.yml deleted file mode 100644 index b053189d9..000000000 --- a/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_all_pages.yml +++ /dev/null @@ -1,4 +0,0 @@ -label: 'Näytä kaikilla sivuilla' -settings: - on_label: Kyllä - off_label: Ei diff --git a/modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.field_survey_all_pages.yml b/modules/helfi_node_survey/config/optional/language/sv/field.field.node.announcement.field_publish_externally.yml similarity index 100% rename from modules/helfi_node_survey/config/optional/language/sv/field.field.node.survey.field_survey_all_pages.yml rename to modules/helfi_node_survey/config/optional/language/sv/field.field.node.announcement.field_publish_externally.yml From 99a8c054130473b62f23bf96f43924a21d5534c5 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Thu, 20 Jun 2024 09:01:50 +0300 Subject: [PATCH 22/33] UHF-9708: Unify global and local blocks --- .../helfi_etusivu_entities.info.yml | 2 + .../helfi_etusivu_entities.install | 53 ++-- .../helfi_etusivu_entities.module | 57 ++-- .../src/Plugin/Block/AnnouncementsBlock.php | 287 ++++++++++++++++++ .../Plugin/Block/EtusivuEntityBlockBase.php | 104 ++++++- .../Plugin/Block/GlobalAnnouncementsBlock.php | 78 ----- .../src/Plugin/Block/GlobalSurveysBlock.php | 73 ----- .../src/Plugin/Block/SurveyBlock.php | 187 ++++++++++++ .../StorageClient/Surveys.php | 6 +- .../helfi_global_announcement.install | 14 + .../helfi_node_announcement.install | 12 +- .../helfi_node_announcement.module | 83 ++--- .../src/Plugin/Block/AnnouncementsBlock.php | 123 -------- .../Plugin/Block/AnnouncementsBlockBase.php | 193 ------------ ...node.survey.field_survey_content_pages.yml | 2 +- ...node.survey.field_survey_content_pages.yml | 1 + .../helfi_node_survey.install | 26 +- .../helfi_node_survey.module | 26 +- .../helfi_platform_config_base.info.yml | 1 + phpstan.neon | 3 - 20 files changed, 726 insertions(+), 605 deletions(-) create mode 100644 modules/helfi_etusivu_entities/src/Plugin/Block/AnnouncementsBlock.php delete mode 100644 modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php delete mode 100644 modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php create mode 100644 modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php delete mode 100644 modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlock.php delete mode 100644 modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlockBase.php diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml b/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml index b8c69827d..30f2ab963 100644 --- a/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml @@ -5,3 +5,5 @@ dependencies: - external_entities:external_entities - helfi_api_base:helfi_api_base - helfi_platform_config:helfi_platform_config + - helfi_platform_config:helfi_node_announcement + - helfi_platform_config:helfi_node_survey diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.install b/modules/helfi_etusivu_entities/helfi_etusivu_entities.install index fdc12cc6b..b3067d0e0 100644 --- a/modules/helfi_etusivu_entities/helfi_etusivu_entities.install +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.install @@ -18,28 +18,13 @@ function helfi_etusivu_entities_install($is_syncing) : void { } helfi_etusivu_entities_grant_permissions(); - _helfi_etusivu_entities_create_block(); + helfi_etusivu_entities_create_blocks(); } /** - * Grants required permissions. + * Create required blocks. */ -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, - ]); -} - -/** - * Added external entity for announcements and enable block. - */ -function _helfi_etusivu_entities_create_block() : void { +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'); @@ -56,3 +41,35 @@ function _helfi_etusivu_entities_create_block() : void { $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, + ]); +} diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module index e10e0ae19..3087c0d0e 100644 --- a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module @@ -7,35 +7,46 @@ declare(strict_types=1); -use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\entity\BundleFieldDefinition; -use Drupal\helfi_api_base\Cache\CacheTagInvalidatorInterface; -use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface; -use Drupal\helfi_api_base\Environment\Project; -use Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient\Announcements; -use Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient\Surveys; /** * 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 [ - 'global_announcement' => [ + // External entity version of this block is + // enabled by helfi_etusivu_entities module. + 'announcements' => [ 'block' => [ - 'id' => 'globalannouncement', - 'plugin' => 'global_announcements', + 'id' => 'announcements', + 'plugin' => 'announcements', 'settings' => [ - 'id' => 'global_announcements', - 'label' => 'Global announcement', - 'provider' => 'helfi_etusivu_entities', + 'id' => 'announcements', + 'label' => 'Announcements', + 'provider' => 'helfi_node_announcement', ], - 'provider' => 'helfi_etusivu_entities', - 'weight' => -14, + 'provider' => 'helfi_node_announcement', + 'weight' => -15, ], 'variations' => [ [ @@ -48,17 +59,19 @@ function helfi_etusivu_entities_get_block_configurations(string $theme) : array ], ], ], - 'global_survey' => [ + // External entity version of this block is + // enabled by helfi_etusivu_entities module. + 'surveys' => [ 'block' => [ - 'id' => 'globalsurvey', - 'plugin' => 'global_surveys', + 'id' => 'surveys', + 'plugin' => 'surveys', 'settings' => [ - 'id' => 'global_surveys', - 'label' => 'Global surveys', - 'provider' => 'helfi_etusivu_entities', + 'id' => 'surveys', + 'label' => 'Surveys', + 'provider' => 'helfi_node_survey', ], - 'provider' => 'helfi_etusivu_entities', - 'weight' => -14, + 'provider' => 'helfi_node_survey', + 'weight' => -15, ], 'variations' => [ [ diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/AnnouncementsBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/AnnouncementsBlock.php new file mode 100644 index 000000000..02f2b9352 --- /dev/null +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/AnnouncementsBlock.php @@ -0,0 +1,287 @@ + 'field_announcement_content_pages', + 'tpr_unit' => 'field_announcement_unit_pages', + 'tpr_service' => 'field_announcement_service_pages', + ]; + + /** + * {@inheritdoc} + */ + public function build(): array { + try { + $announcementNodes = $this->getLocalAnnouncements(); + $globalAnnouncements = $this->getExternalAnnouncements(); + $localAnnouncements = []; + } + catch (\Exception $e) { + Error::logException($this->logger, $e); + return []; + } + + $currentEntity = $this->getCurrentPageEntity(array_keys(self::ENTITY_TYPE_FIELDS)); + $referenceField = self::ENTITY_TYPE_FIELDS[$currentEntity?->getEntityTypeId()] ?? NULL; + + foreach ($announcementNodes as $announcementNode) { + assert($announcementNode instanceof FieldableEntityInterface); + + // Check if the announcement should be shown at all pages. + // Global announcements should be shown on top of all pages. + if ( + $announcementNode->hasField('field_publish_externally') && + $announcementNode->get('field_publish_externally')->value + ) { + $globalAnnouncements[] = $announcementNode; + continue; + } + + if ($announcementNode->get('field_announcement_all_pages')->value === "1") { + $localAnnouncements[] = $announcementNode; + continue; + } + + if (!empty($referenceField) && $this->hasReference($referenceField, $announcementNode, $currentEntity)) { + // Add announcement to showed announcements if current page's entity + // is found from the list of referenced entities. + $localAnnouncements[] = $announcementNode; + } + } + + $this->sortAnnouncements($localAnnouncements); + + return $this + ->entityTypeManager + ->getViewBuilder('node') + ->viewMultiple(array_merge($globalAnnouncements, $localAnnouncements), 'default'); + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts(): array { + return Cache::mergeContexts(parent::getCacheContexts(), [ + 'url.path', + 'languages:language_content', + ]); + } + + /** + * {@inheritdoc} + */ + public function getCacheTags(): array { + return Cache::mergeTags(parent::getCacheTags(), [ + 'node_list:announcement', + Announcements::$customCacheTag, + ]); + } + + /** + * Loads external announcement nodes. + * + * @return array + * External surveys. + * + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + private function getExternalAnnouncements(): array { + $entityStorage = $this->getExternalEntityStorage('helfi_announcements'); + $nodes = []; + + /** @var \Drupal\external_entities\ExternalEntityInterface $announcement */ + foreach ($entityStorage->loadMultiple() as $announcement) { + $linkUrl = NULL; + $linkText = NULL; + if ($announcement->hasField('announcement_link_text')) { + $linkText = $announcement->get('announcement_link_text')->value; + $linkUrl = $announcement->get('announcement_link_url')->value; + } + + // Create announcement nodes for the block based on external entity data. + $nodes[] = Node::create([ + 'uuid' => $announcement->get('uuid')->value, + 'type' => 'announcement', + 'langcode' => $announcement->get('langcode')->value, + 'body' => Xss::filter($announcement->get('body')->value), + 'title' => Xss::filter($announcement->get('title')->value), + 'status' => $announcement->get('status')->value, + 'field_announcement_title' => $announcement->get('announcement_assistive_technology_close_button_title')->value, + 'field_announcement_type' => $announcement->get('announcement_type')->value, + 'field_announcement_link' => ['uri' => $linkUrl, 'title' => $linkText], + ]); + } + + return $nodes; + } + + /** + * Loads local announcement nodes. + * + * @return array + * Local surveys. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + private function getLocalAnnouncements(): array { + $langcodes = $this->getContentLangcodes(); + $storage = $this->entityTypeManager->getStorage('node'); + + // Get all published announcement nodes. + $query = $storage->getQuery() + ->accessCheck(TRUE) + ->condition('type', 'announcement') + ->condition('status', NodeInterface::PUBLISHED) + ->condition('langcode', $langcodes, 'IN'); + + $fields = $this->entityFieldManager->getFieldDefinitions('node', 'survey'); + + // Query only local nodes. + if (isset($fields['field_publish_externally'])) { + $query->condition('field_publish_externally', FALSE); + } + + return $storage->loadMultiple($query->execute()); + } + + /** + * Sort announcements by type/severity and by visibility. + * + * @param \Drupal\node\NodeInterface[] $announcements + * Array of nodes. + */ + private function sortAnnouncements(array &$announcements): void { + if (empty($announcements)) { + return; + } + + // Get all possible values for the announcement types. + $announcementTypeDefinition = $announcements[0]->getFieldDefinitions()['field_announcement_type']; + $types = options_allowed_values( + $announcementTypeDefinition->getFieldStorageDefinition(), + ); + + // Map select-list values with numeric weight value. + $announcementTypeWeights = $this->createAnnouncementWeightMap($types); + + $this->doSort($announcements, $announcementTypeWeights); + } + + /** + * Create the map which is used to order the announcements by severity. + * + * @param array $announcementTypes + * Should return ['notification' => 0, 'attention' => 1, 'alert' => 2]. + * + * @return int[]|string[] + * Array of announcement type keys and weights. + */ + private function createAnnouncementWeightMap(array $announcementTypes): array { + return array_flip(array_keys($announcementTypes)); + } + + /** + * Execute sorting. + * + * @param \Drupal\node\NodeInterface[] $announcements + * Announcement entities. + * @param array $announcementTypeWeights + * Announcement types ordered by severity. + */ + private function doSort(array &$announcements, array $announcementTypeWeights): void { + // Sort by type/severity. + usort($announcements, function ( + EntityInterface $a, + EntityInterface $b, + ) use ($announcementTypeWeights) { + $weightA = $announcementTypeWeights[$a->get('field_announcement_type')->value]; + $weightB = $announcementTypeWeights[$b->get('field_announcement_type')->value]; + if ($weightA === $weightB) { + return 0; + } + // More urgent announcements render first. + return $weightA < $weightB ? 1 : -1; + }); + + // Sort by visibility. + usort($announcements, function (EntityInterface $a, EntityInterface $b) { + $visibilityA = $this->resolveVisibilityWeight($a); + $visibilityB = $this->resolveVisibilityWeight($b); + // Sort visibility only within same type. + if ( + $a->get('field_announcement_type')->value !== $b->get('field_announcement_type')->value || + $visibilityA === $visibilityB + ) { + return 0; + } + // Page-specific renders before global announcement. + return $visibilityA < $visibilityB ? 1 : -1; + }); + } + + /** + * Return weight for announcement visibility. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $announcement + * Announcement entity. + * + * @return int + * Visibility weight. + */ + private function resolveVisibilityWeight(ContentEntityInterface $announcement): int { + if ($announcement->get('field_announcement_all_pages')->value) { + return self::VISIBILITY_ALL_WEIGHT; + } + + if ( + ($announcement->hasField('field_announcement_unit_pages') && + !$announcement->get('field_announcement_unit_pages')->isEmpty()) || + ($announcement->hasField('field_announcement_service_pages') && + !$announcement->get('field_announcement_service_pages')->isEmpty()) + ) { + return self::VISIBILITY_REGION_WEIGHT; + } + + if ( + $announcement->hasField('field_announcement_content_pages') && + !$announcement->get('field_announcement_content_pages')->isEmpty() + ) { + return self::VISIBILITY_PAGE_WEIGHT; + } + + return self::VISIBILITY_ALL_WEIGHT; + } + +} diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php b/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php index a10f0e018..ff1410046 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php @@ -5,9 +5,18 @@ namespace Drupal\helfi_etusivu_entities\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Field\EntityReferenceFieldItemListInterface; +use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\external_entities\ExternalEntityStorageInterface; +use Drupal\helfi_api_base\Language\DefaultLanguageResolver; +use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -24,14 +33,29 @@ abstract class EtusivuEntityBlockBase extends BlockBase implements ContainerFact * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. + * @param \Psr\Log\LoggerInterface $logger + * The logger. + * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch + * The current route match. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager + * The entity field manager. + * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager + * The language manager. + * @param \Drupal\helfi_api_base\Language\DefaultLanguageResolver $defaultLanguageResolver + * Default language resolver. */ final public function __construct( array $configuration, $plugin_id, $plugin_definition, + protected readonly LoggerInterface $logger, + protected readonly RouteMatchInterface $routeMatch, protected readonly EntityTypeManagerInterface $entityTypeManager, + protected readonly EntityFieldManagerInterface $entityFieldManager, + protected readonly LanguageManagerInterface $languageManager, + protected readonly DefaultLanguageResolver $defaultLanguageResolver, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); } @@ -41,7 +65,12 @@ final public function __construct( */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { return new static($configuration, $plugin_id, $plugin_definition, - $container->get('entity_type.manager'), + $container->get('logger.channel.helfi_etusivu_entities'), + $container->get(RouteMatchInterface::class), + $container->get(EntityTypeManagerInterface::class), + $container->get(EntityFieldManagerInterface::class), + $container->get(LanguageManagerInterface::class), + $container->get(DefaultLanguageResolver::class), ); } @@ -54,7 +83,7 @@ public static function create(ContainerInterface $container, array $configuratio * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ - protected function getGlobalEntityStorage(string $entityTypeId): ExternalEntityStorageInterface { + protected function getExternalEntityStorage(string $entityTypeId): ExternalEntityStorageInterface { $globalEntityStorage = $this->entityTypeManager->getStorage($entityTypeId); if ($globalEntityStorage instanceof ExternalEntityStorageInterface) { return $globalEntityStorage; @@ -63,4 +92,75 @@ protected function getGlobalEntityStorage(string $entityTypeId): ExternalEntityS throw new \InvalidArgumentException("$entityTypeId is not external entity type"); } + /** + * Gets content langcodes. + */ + protected function getContentLangcodes(): array { + // Also fetch english announcements for languages with non-standard support. + $langcodes[] = $this->defaultLanguageResolver->getCurrentOrFallbackLanguage(LanguageInterface::TYPE_CONTENT); + $currentLangcode = $this->languageManager + ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) + ->getId(); + + if (reset($langcodes) !== $currentLangcode) { + $langcodes[] = $currentLangcode; + } + + return $langcodes; + } + + /** + * Get current page's entity from given possibilities. + * + * @param array $entityTypes + * Entity names to be used to check the current page. + * + * @return \Drupal\Core\Entity\EntityInterface|null + * Current page's entity, if any. + */ + protected function getCurrentPageEntity(array $entityTypes): ?EntityInterface { + foreach ($entityTypes as $entityType) { + $pageEntity = $this->routeMatch->getParameter($entityType); + if (!empty($pageEntity) && $pageEntity instanceof EntityInterface) { + return $pageEntity; + } + } + return NULL; + } + + /** + * Checks if entity has reference to another entity. + * + * @param string $fieldName + * Entity reference field name. + * @param \Drupal\Core\Entity\FieldableEntityInterface $entity + * The entity. + * @param \Drupal\Core\Entity\EntityInterface|null $target + * Target entity. + * + * @return bool + * True if $entity has reference to $target. + */ + protected function hasReference(string $fieldName, FieldableEntityInterface $entity, ?EntityInterface $target): bool { + // Get announcement's referenced entities from the appropriate field, + // depending on the current page's entity. + $referencedEntities = []; + + if ($entity->hasField($fieldName)) { + $field = $entity->get($fieldName); + assert($field instanceof EntityReferenceFieldItemListInterface); + $referencedEntities = $field->referencedEntities(); + } + + if ($target) { + foreach ($referencedEntities as $referencedEntity) { + if ($referencedEntity->id() === $target->id()) { + return TRUE; + } + } + } + + return FALSE; + } + } diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php deleted file mode 100644 index fb80a74db..000000000 --- a/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalAnnouncementsBlock.php +++ /dev/null @@ -1,78 +0,0 @@ -getGlobalEntityStorage('helfi_announcements'); - $announcementNodes = []; - - /** @var \Drupal\external_entities\ExternalEntityInterface $announcement */ - foreach ($entityStorage->loadMultiple() as $announcement) { - $linkUrl = NULL; - $linkText = NULL; - if ($announcement->hasField('announcement_link_text')) { - $linkText = $announcement->get('announcement_link_text')->value; - $linkUrl = $announcement->get('announcement_link_url')->value; - } - - // Create announcement nodes for the block based on external entity data. - $announcementNodes[] = Node::create([ - 'uuid' => $announcement->get('uuid')->value, - 'type' => 'announcement', - 'langcode' => $announcement->get('langcode')->value, - 'body' => Xss::filter($announcement->get('body')->value), - 'title' => Xss::filter($announcement->get('title')->value), - 'status' => $announcement->get('status')->value, - 'field_announcement_title' => $announcement->get('announcement_assistive_technology_close_button_title')->value, - 'field_announcement_type' => $announcement->get('announcement_type')->value, - 'field_announcement_link' => ['uri' => $linkUrl, 'title' => $linkText], - ]); - } - - $viewMode = 'default'; - $renderArray = $this->entityTypeManager - ->getViewBuilder('node') - ->viewMultiple($announcementNodes, $viewMode); - - $renderArray['#cache'] = [ - 'max-age' => $entityStorage->getExternalEntityType()->get('persistent_cache_max_age'), - 'tags' => [ - Announcements::$customCacheTag, - ], - ]; - - return $renderArray; - } - - /** - * {@inheritdoc} - */ - public function getCacheContexts(): array { - return Cache::mergeContexts(parent::getCacheContexts(), [ - 'url.path', - 'languages:language_content', - ]); - } - -} diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php deleted file mode 100644 index c4ef287e9..000000000 --- a/modules/helfi_etusivu_entities/src/Plugin/Block/GlobalSurveysBlock.php +++ /dev/null @@ -1,73 +0,0 @@ -getGlobalEntityStorage('helfi_surveys'); - $nodes = []; - - /** @var \Drupal\external_entities\ExternalEntityInterface $entity */ - foreach ($entityStorage->loadMultiple() as $entity) { - $linkUrl = NULL; - $linkText = NULL; - if ($entity->hasField('survey_link_text')) { - $linkText = $entity->get('survey_link_text')->value; - $linkUrl = $entity->get('survey_link_url')->value; - } - - // Create announcement nodes for the block based on external entity data. - $nodes[] = Node::create([ - 'uuid' => $entity->get('uuid')->value, - 'type' => 'survey', - 'field_survey_link' => ['uri' => $linkUrl, 'title' => $linkText], - 'langcode' => $entity->get('langcode')->value, - 'body' => Xss::filter($entity->get('body')->value), - 'title' => Xss::filter($entity->get('title')->value), - 'status' => $entity->get('status')->value, - ]); - } - - $viewMode = 'default'; - $renderArray = $this->entityTypeManager->getViewBuilder('node')->viewMultiple($nodes, $viewMode); - $renderArray['#cache'] = [ - 'max-age' => $entityStorage->getExternalEntityType()->get('persistent_cache_max_age'), - 'tags' => [ - Surveys::$customCacheTag, - ], - ]; - - return $renderArray; - } - - /** - * {@inheritdoc} - */ - public function getCacheContexts(): array { - return Cache::mergeContexts(parent::getCacheContexts(), [ - 'url.path', - 'languages:language_content', - ]); - } - -} diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php new file mode 100644 index 000000000..12b7b03b3 --- /dev/null +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php @@ -0,0 +1,187 @@ + 'field_survey_content_pages', + ]; + + /** + * {@inheritdoc} + */ + public function build(): array { + $currentEntity = $this->getCurrentPageEntity(array_keys(self::ENTITY_TYPE_FIELDS)); + + if ($survey = $this->getSurvey($currentEntity)) { + return $this + ->entityTypeManager + ->getViewBuilder('node') + ->view($survey, 'default'); + } + + return []; + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts(): array { + return Cache::mergeContexts(parent::getCacheContexts(), [ + 'url.path', + 'languages:language_content', + ]); + } + + /** + * {@inheritdoc} + */ + public function getCacheTags(): array { + return Cache::mergeTags(parent::getCacheTags(), [ + Surveys::$customCacheTag, + 'node_list:survey', + ]); + } + + /** + * Loads most resent survey node that is valid for this page. + * + * @param \Drupal\Core\Entity\EntityInterface|null $currentEntity + * Entity on current page. + * + * @returns NodeInterface|null + * Most recent survey for this page or NULL if none exists. + */ + public function getSurvey(?EntityInterface $currentEntity) : ?NodeInterface { + try { + // Since the arrays contain numeric keys, the + // later values will be appended. + $surveys = array_merge($this->getExternalSurveys(), $this->getLocalSurveys()); + } + catch (\Exception $e) { + Error::logException($this->logger, $e); + return NULL; + } + + // Sort by publised_at time. + usort($surveys, static function (NodeInterface $a, NodeInterface $b) { + $weightA = $a->get('published_at')->value; + $weightB = $b->get('published_at')->value; + if ($weightA === $weightB) { + return 0; + } + // More urgent announcements render first. + return $weightA < $weightB ? 1 : -1; + }); + + $referenceField = self::ENTITY_TYPE_FIELDS[$currentEntity?->getEntityTypeId()] ?? NULL; + + foreach ($surveys as $node) { + // Check if the announcement should be shown at all pages. + if ($node->get('field_survey_content_pages')->isEmpty()) { + return $node; + } + + // Show survey if current page's entity is found + // from the list of referenced entities. + if (!empty($referenceField) && $this->hasReference($referenceField, $node, $currentEntity)) { + return $node; + } + } + + return NULL; + } + + /** + * Loads external survey nodes. + * + * @return array + * External surveys. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + private function getExternalSurveys(): array { + $entityStorage = $this->getExternalEntityStorage('helfi_surveys'); + $nodes = []; + + /** @var \Drupal\external_entities\ExternalEntityInterface $entity */ + foreach ($entityStorage->loadMultiple() as $entity) { + $linkUrl = NULL; + $linkText = NULL; + if ($entity->hasField('survey_link_text')) { + $linkText = $entity->get('survey_link_text')->value; + $linkUrl = $entity->get('survey_link_url')->value; + } + + // Create announcement nodes for the block based on external entity data. + $nodes[] = Node::create([ + 'uuid' => $entity->get('uuid')->value, + 'type' => 'survey', + 'field_survey_link' => ['uri' => $linkUrl, 'title' => $linkText], + 'langcode' => $entity->get('langcode')->value, + 'body' => Xss::filter($entity->get('body')->value), + 'title' => Xss::filter($entity->get('title')->value), + 'status' => $entity->get('status')->value, + ]); + } + + return $nodes; + } + + /** + * Loads local surveys. + * + * @return array + * Local surveys. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + private function getLocalSurveys() : array { + $langcodes = $this->getContentLangcodes(); + + $storage = $this->entityTypeManager->getStorage('node'); + + // Get all published survey nodes. + $query = $storage->getQuery() + ->accessCheck(TRUE) + ->condition('type', 'survey') + ->condition('status', NodeInterface::PUBLISHED) + ->condition('langcode', $langcodes, 'IN') + ->sort('published_at', 'DESC'); + + $fields = $this->entityFieldManager->getFieldDefinitions('node', 'survey'); + + // Query only local nodes. + if (isset($fields['field_publish_externally'])) { + $query->condition('field_publish_externally', FALSE); + } + + return array_values($storage->loadMultiple($query->execute())); + } + +} diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php index 7f8e0f446..cb702c01b 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php @@ -35,10 +35,12 @@ public function query( 'fields[node--survey]' => 'id,langcode,status,published_at,unpublish_on,title,body,field_survey_link', 'fields[status]' => 1, 'filter[status][value]' => 1, - 'filter[field_survey_all_pages][value]' => 1, + 'filter[field_publish_externally][value]' => 1, + 'sort' => '-published_at', ]; - $query += $this->queryLimits($start, $length); + // This only ever returns the latest survey. + $query += $this->queryLimits($start, 1); $query += $this->queryDefaultLangcode(); return $this->request("/node/survey", $query, $query['filter[langcode]']); diff --git a/modules/helfi_global_announcement/helfi_global_announcement.install b/modules/helfi_global_announcement/helfi_global_announcement.install index 296ecbe23..b458836f6 100644 --- a/modules/helfi_global_announcement/helfi_global_announcement.install +++ b/modules/helfi_global_announcement/helfi_global_announcement.install @@ -7,6 +7,8 @@ declare(strict_types=1); +use Drupal\block\Entity\Block; + /** * UHF-9708: Enable helfi_etusivu_entities module. */ @@ -20,3 +22,15 @@ function helfi_global_announcement_update_9309() : void { $module_installer->install(['helfi_etusivu_entities']); } } + +/** + * UHF-9708: Uninstall helfi_global_announcements module. + */ +function helfi_global_announcement_update_9310() : void { + if ($block = Block::load('globalannouncement')) { + $block->delete(); + } + + // Functionality moved to helfi_etusivu_entities. + \Drupal::service('module_installer')->uninstall(['helfi_global_announcement']); +} diff --git a/modules/helfi_node_announcement/helfi_node_announcement.install b/modules/helfi_node_announcement/helfi_node_announcement.install index 67ded4a1a..be7c0d0f0 100644 --- a/modules/helfi_node_announcement/helfi_node_announcement.install +++ b/modules/helfi_node_announcement/helfi_node_announcement.install @@ -63,13 +63,12 @@ function helfi_node_announcement_install($is_syncing) : void { } helfi_node_announcement_grant_permissions(); - _helfi_global_announcement_enable(); } /** - * Enable global announcement. + * Implements hook_update(). */ -function _helfi_global_announcement_enable() { +function helfi_node_announcement_update_9001() : void { if ( Drupal::moduleHandler()->moduleExists('external_entities') && Drupal::moduleHandler()->moduleExists('helfi_node_announcement') @@ -80,13 +79,6 @@ function _helfi_global_announcement_enable() { } } -/** - * Implements hook_update(). - */ -function helfi_node_announcement_update_9001() : void { - _helfi_global_announcement_enable(); -} - /** * UHF-10159 Convert the announcement title field to a translatable field. */ diff --git a/modules/helfi_node_announcement/helfi_node_announcement.module b/modules/helfi_node_announcement/helfi_node_announcement.module index 02f699c89..b49f94c56 100644 --- a/modules/helfi_node_announcement/helfi_node_announcement.module +++ b/modules/helfi_node_announcement/helfi_node_announcement.module @@ -14,78 +14,37 @@ function helfi_node_announcement_form_node_form_alter(&$form, &$form_state, $for switch ($form_id) { case 'node_announcement_edit_form': case 'node_announcement_form': - $form['field_announcement_content_pages']['#states'] = [ + // Don't show "menu link translation" published checkbox, as + // announcements cannot be added to any menu. + if (isset($form['menu']['content_translation_status'])) { + $form['menu']['content_translation_status']['#access'] = FALSE; + } + + $contentPageStates = [ 'visible' => [ ':input[name="field_announcement_all_pages[value]"]' => ['checked' => FALSE], ], ]; - break; - } -} -/** - * Implements hook_themes_installed(). - */ -function helfi_node_announcement_themes_installed($theme_list) : void { + if (isset($form['field_publish_externally'])) { + $contentPageStates['visible'][':input[name="field_publish_externally[value]"]'] = + ['checked' => FALSE]; - /** @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_node_announcement_get_block_configurations($theme) as $block_config) { - ['block' => $block, 'variations' => $variations] = $block_config; - $block_installer->install($block, $variations); + $form['field_announcement_all_pages']['#states'] = [ + 'visible' => [ + ':input[name="field_publish_externally[value]"]' => ['checked' => FALSE], + ], + ]; } - } + + $form['field_announcement_content_pages']['#states'] = $contentPageStates; + break; } } /** - * Gets the block configurations. - * - * Example block: - * - * @code - * [ - * 'breadbrumbs' => [ - * 'block' => [ - * ... - * ], - * 'variations' => [ - * ... - * ], - * ], - * ]; - * @endcode - * - * @return array[] - * The block configurations. + * Implements hook_content_translation_status_access(). */ -function helfi_node_announcement_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' => -12, - ], - 'variations' => [ - [ - 'theme' => $theme, - 'region' => 'before_content', - ], - [ - 'theme' => 'stark', - 'region' => 'content', - ], - ], - ], - ]; +function helfi_node_announcement_content_translation_status_access($form_id) : bool { + return !in_array($form_id, ['node_announcement_edit_form', 'node_announcement_form']); } diff --git a/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlock.php b/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlock.php deleted file mode 100644 index d829175b6..000000000 --- a/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlock.php +++ /dev/null @@ -1,123 +0,0 @@ - 'field_announcement_content_pages', - 'tpr_unit' => 'field_announcement_unit_pages', - 'tpr_service' => 'field_announcement_service_pages', - ]; - - $currentEntity = $this->getCurrentPageEntity(array_keys($entityTypeFields)); - - // Also fetch english announcements for languages with non standard support. - $currentLangcode = $this->languageManager - ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) - ->getId(); - - if ($this->defaultLanguageResolver->isAltLanguage($currentLangcode)) { - $langcode = [ - $this->defaultLanguageResolver->getFallbackLanguage(), - $currentLangcode, - ]; - } - else { - $langcode = [$currentLangcode]; - } - - // Get all published announcement nodes. - $nids = $this->entityTypeManager->getStorage('node')->getQuery() - ->accessCheck(TRUE) - ->condition('type', 'announcement') - ->condition('status', NodeInterface::PUBLISHED) - ->condition('langcode', $langcode, 'IN') - ->execute(); - $announcementNodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids); - - $localAnnouncements = []; - $globalAnnouncements = []; - - foreach ($announcementNodes as $announcementNode) { - // Check if the announcement should be shown at all pages. - // Global announcements should be shown on top of all pages. - if ( - $announcementNode->hasField('field_publish_externally') - && $announcementNode->get('field_publish_externally')->value - ) { - $globalAnnouncements[] = $announcementNode; - continue; - } - - if ($announcementNode->get('field_announcement_all_pages')->value === "1") { - $localAnnouncements[] = $announcementNode; - continue; - } - - if (empty($currentEntity)) { - continue; - } - - // Get announcement's referenced entities from the appropriate field, - // depending on the current page's entity. - $referencedEntities = []; - if ($announcementNode->hasField($entityTypeFields[$currentEntity->getEntityType()->id()])) { - $referencedEntities = $announcementNode->get($entityTypeFields[$currentEntity->getEntityType()->id()])->referencedEntities(); - } - - // Add announcement to showed announcements if current page's entity - // is found from the list of referenced entities. - foreach ($referencedEntities as $referencedEntity) { - if ($referencedEntity->id() === $currentEntity->id()) { - $localAnnouncements[] = $announcementNode; - } - } - } - - $this->sortAnnouncements($localAnnouncements); - - $viewMode = 'default'; - $renderArray = $this->entityTypeManager->getViewBuilder('node') - ->viewMultiple(array_merge($globalAnnouncements, $localAnnouncements), $viewMode); - - return $renderArray; - } - - /** - * {@inheritdoc} - */ - public function getCacheContexts(): array { - return Cache::mergeContexts(parent::getCacheContexts(), [ - 'url.path', - 'languages:language_content', - ]); - } - - /** - * {@inheritdoc} - */ - public function getCacheTags(): array { - return Cache::mergeTags(parent::getCacheTags(), ['node_list:announcement']); - } - -} diff --git a/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlockBase.php b/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlockBase.php deleted file mode 100644 index 90f3f4617..000000000 --- a/modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlockBase.php +++ /dev/null @@ -1,193 +0,0 @@ -get('current_route_match'), - $container->get('entity_type.manager'), - $container->get('language_manager'), - $container->get('helfi_api_base.default_language_resolver'), - ); - } - - /** - * Get current page's entity from given possibilities. - * - * @param array $entityTypes - * Entity names to be used to check the current page. - * - * @return \Drupal\Core\Entity\EntityInterface|null - * Current page's entity, if any. - */ - protected function getCurrentPageEntity(array $entityTypes): ?EntityInterface { - foreach ($entityTypes as $entityType) { - $pageEntity = $this->routeMatch->getParameter($entityType); - if (!empty($pageEntity) && $pageEntity instanceof EntityInterface) { - return $pageEntity; - } - } - return NULL; - } - - /** - * Sort announcements by type/severity and by visibility. - * - * @param \Drupal\node\NodeInterface[] $announcements - * Array of nodes. - */ - protected function sortAnnouncements(array &$announcements): void { - if (empty($announcements)) { - return; - } - - // Get all possible values for the announcement types. - $announcementTypeDefinition = $announcements[0]->getFieldDefinitions()['field_announcement_type']; - $types = options_allowed_values( - $announcementTypeDefinition->getFieldStorageDefinition(), - ); - - // Map select-list values with numeric weight value. - $announcementTypeWeights = $this->createAnnouncementWeightMap($types); - - $this->doSort($announcements, $announcementTypeWeights); - } - - /** - * Execute sorting. - * - * @param \Drupal\node\NodeInterface[] $announcements - * Announcement entities. - * @param array $announcementTypeWeights - * Announcement types ordered by severity. - */ - protected function doSort(array &$announcements, array $announcementTypeWeights): void { - // Sort by type/severity. - usort($announcements, function ( - EntityInterface $a, - EntityInterface $b, - ) use ($announcementTypeWeights) { - $weightA = $announcementTypeWeights[$a->get('field_announcement_type')->value]; - $weightB = $announcementTypeWeights[$b->get('field_announcement_type')->value]; - if ($weightA === $weightB) { - return 0; - } - // More urgent announcements render first. - return $weightA < $weightB ? 1 : -1; - }); - - // Sort by visibility. - usort($announcements, function (EntityInterface $a, EntityInterface $b) { - $visibilityA = $this->resolveVisibilityWeight($a); - $visibilityB = $this->resolveVisibilityWeight($b); - // Sort visibility only within same type. - if ( - $a->get('field_announcement_type')->value !== $b->get('field_announcement_type')->value || - $visibilityA === $visibilityB - ) { - return 0; - } - // Page-specific renders before global announcement. - return $visibilityA < $visibilityB ? 1 : -1; - }); - } - - /** - * Create the map which is used to order the announcements by severity. - * - * @param array $announcementTypes - * Should return ['notification' => 0, 'attention' => 1, 'alert' => 2]. - * - * @return int[]|string[] - * Array of announcement type keys and weights. - */ - protected function createAnnouncementWeightMap(array $announcementTypes): array { - return array_flip(array_keys($announcementTypes)); - } - - /** - * Return weight for announcement visibility. - * - * @param \Drupal\Core\Entity\ContentEntityInterface $announcement - * Announcement entity. - * - * @return int - * Visibility weight. - */ - protected function resolveVisibilityWeight(ContentEntityInterface $announcement): int { - if ($announcement->get('field_announcement_all_pages')->value == TRUE) { - return self::VISIBILITY_ALL_WEIGHT; - } - - if ( - ($announcement->hasField('field_announcement_unit_pages') && - !$announcement->get('field_announcement_unit_pages')->isEmpty()) || - ($announcement->hasField('field_announcement_service_pages') && - !$announcement->get('field_announcement_service_pages')->isEmpty()) - ) { - return self::VISIBILITY_REGION_WEIGHT; - } - - if ( - ($announcement->hasField('field_announcement_content_pages') && - !$announcement->get('field_announcement_content_pages')->isEmpty()) - ) { - return self::VISIBILITY_PAGE_WEIGHT; - } - - return self::VISIBILITY_ALL_WEIGHT; - } - -} diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml index a6eb80c80..b7e8e2f22 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_content_pages.yml @@ -12,7 +12,7 @@ field_name: field_survey_content_pages entity_type: node bundle: survey label: 'Show on content pages' -description: '' +description: 'Shown on all pages if left empty.' required: false translatable: false default_value: { } diff --git a/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_content_pages.yml b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_content_pages.yml index 1abe9ed71..6b1adffa4 100644 --- a/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_content_pages.yml +++ b/modules/helfi_node_survey/config/optional/language/fi/field.field.node.survey.field_survey_content_pages.yml @@ -1 +1,2 @@ label: 'Näytä sisältösivuilla' +description: 'Näytetään kaikilla sivuilla jos jätetään tyhjäksi.' diff --git a/modules/helfi_node_survey/helfi_node_survey.install b/modules/helfi_node_survey/helfi_node_survey.install index d05392496..14ad5f71c 100644 --- a/modules/helfi_node_survey/helfi_node_survey.install +++ b/modules/helfi_node_survey/helfi_node_survey.install @@ -7,6 +7,19 @@ declare(strict_types=1); +/** + * Implements hook_install(). + */ +function helfi_node_survey_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_node_survey_grant_permissions(); +} + /** * Grants required permissions. */ @@ -27,16 +40,3 @@ function helfi_node_survey_grant_permissions() : void { ]; helfi_platform_config_grant_permissions($permissions); } - -/** - * Implements hook_install(). - */ -function helfi_node_survey_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_node_survey_grant_permissions(); -} diff --git a/modules/helfi_node_survey/helfi_node_survey.module b/modules/helfi_node_survey/helfi_node_survey.module index 4cbeefd66..c8a46f47d 100644 --- a/modules/helfi_node_survey/helfi_node_survey.module +++ b/modules/helfi_node_survey/helfi_node_survey.module @@ -14,11 +14,27 @@ function helfi_node_survey_form_node_form_alter(&$form, &$form_state, $form_id) switch ($form_id) { case 'node_survey_edit_form': case 'node_survey_form': - $form['field_survey_content_pages']['#states'] = [ - 'visible' => [ - ':input[name="field_survey_all_pages[value]"]' => ['checked' => FALSE], - ], - ]; + // Don't show "menu link translation" published checkbox, as + // surveys cannot be added to any menu. + if (isset($form['menu']['content_translation_status'])) { + $form['menu']['content_translation_status']['#access'] = FALSE; + } + + if (isset($form['field_publish_externally'])) { + $form['field_survey_content_pages']['#states'] = [ + 'visible' => [ + ':input[name="field_publish_externally[value]"]' => ['checked' => FALSE], + ], + ]; + } + break; } } + +/** + * Implements hook_content_translation_status_access(). + */ +function helfi_node_survey_content_translation_status_access($form_id) : bool { + return !in_array($form_id, ['node_survey_edit_form', 'node_survey_form']); +} diff --git a/modules/helfi_platform_config_base/helfi_platform_config_base.info.yml b/modules/helfi_platform_config_base/helfi_platform_config_base.info.yml index 6ae30d4d9..b0edd8352 100644 --- a/modules/helfi_platform_config_base/helfi_platform_config_base.info.yml +++ b/modules/helfi_platform_config_base/helfi_platform_config_base.info.yml @@ -12,6 +12,7 @@ dependencies: - helfi_media_map:helfi_media_map - helfi_media_remote_video:helfi_media_remote_video - helfi_node_announcement:helfi_node_announcement + - helfi_node_survey:helfi_node_survey - helfi_node_landing_page:helfi_node_landing_page - helfi_node_page:helfi_node_page - helfi_paragraphs_accordion:helfi_paragraphs_accordion diff --git a/phpstan.neon b/phpstan.neon index 079fbf9f5..afb484f89 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -28,9 +28,6 @@ parameters: - message: '#^Access to an undefined property Drupal\\Core\\Field\\FieldItemInterface::\$alt.#' path: modules/helfi_paragraphs_contact_card_listing/src/Entity/ContactCard.php - - - message: '#^Unsafe usage of new static#' - path: modules/helfi_node_announcement/src/Plugin/Block/AnnouncementsBlockBase.php - message: '#^Access to an undefined property Drupal\\Core\\Field\\FieldItemListInterface::\$iframe_title.#' path: modules/helfi_media_remote_video/src/Entity/RemoteVideo.php From f1cefe9c6b422b87de4b076c1a1170b19ee86f95 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Thu, 27 Jun 2024 07:48:41 +0300 Subject: [PATCH 23/33] UHF-9708: Fix comments --- .../helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php index 12b7b03b3..447c22cd1 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php @@ -93,14 +93,13 @@ public function getSurvey(?EntityInterface $currentEntity) : ?NodeInterface { if ($weightA === $weightB) { return 0; } - // More urgent announcements render first. return $weightA < $weightB ? 1 : -1; }); $referenceField = self::ENTITY_TYPE_FIELDS[$currentEntity?->getEntityTypeId()] ?? NULL; foreach ($surveys as $node) { - // Check if the announcement should be shown at all pages. + // Check if the node should be shown at all pages. if ($node->get('field_survey_content_pages')->isEmpty()) { return $node; } @@ -137,7 +136,7 @@ private function getExternalSurveys(): array { $linkUrl = $entity->get('survey_link_url')->value; } - // Create announcement nodes for the block based on external entity data. + // Create nodes for the block based on external entity data. $nodes[] = Node::create([ 'uuid' => $entity->get('uuid')->value, 'type' => 'survey', From 0878dacdf3251c4a2fc25cb9d0f5625725d6cc1c Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Thu, 27 Jun 2024 07:48:46 +0300 Subject: [PATCH 24/33] UHF-9708: Remove missing tpr fields --- ...ntity_form_display.node.survey.default.yml | 20 ------------------- ...ntity_view_display.node.survey.default.yml | 2 -- ...entity_view_display.node.survey.teaser.yml | 2 -- 3 files changed, 24 deletions(-) diff --git a/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml b/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml index a973639a9..de1b70c6b 100755 --- a/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml +++ b/modules/helfi_node_survey/config/install/core.entity_form_display.node.survey.default.yml @@ -54,26 +54,6 @@ content: placeholder_url: '' placeholder_title: '' third_party_settings: { } - field_survey_service_pages: - type: select2_entity_reference - weight: 16 - region: content - settings: - width: 100% - autocomplete: true - match_operator: CONTAINS - match_limit: 20 - third_party_settings: { } - field_survey_unit_pages: - type: select2_entity_reference - weight: 15 - region: content - settings: - width: 100% - autocomplete: true - match_operator: CONTAINS - match_limit: 20 - third_party_settings: { } langcode: type: language_select weight: 1 diff --git a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml index 403f124f9..082ffc57b 100644 --- a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml +++ b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.default.yml @@ -42,8 +42,6 @@ content: region: content hidden: field_survey_content_pages: true - field_survey_service_pages: true - field_survey_unit_pages: true langcode: true published_at: true toc_enabled: true diff --git a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml index 647f73609..75bbc631b 100644 --- a/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml +++ b/modules/helfi_node_survey/config/install/core.entity_view_display.node.survey.teaser.yml @@ -32,8 +32,6 @@ content: hidden: field_survey_content_pages: true field_survey_link: true - field_survey_service_pages: true - field_survey_unit_pages: true langcode: true published_at: true toc_enabled: true From cd6683933c87acc415d25b4692829e7972e5cb95 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Thu, 27 Jun 2024 08:50:13 +0300 Subject: [PATCH 25/33] UHF-9708: Allow optional config in test --- tests/src/ExistingSite/ConfigurationTest.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/src/ExistingSite/ConfigurationTest.php b/tests/src/ExistingSite/ConfigurationTest.php index 54eb943e9..3187f1327 100644 --- a/tests/src/ExistingSite/ConfigurationTest.php +++ b/tests/src/ExistingSite/ConfigurationTest.php @@ -86,9 +86,21 @@ private function assertModuleUuids(string $module) : void { if ($file->getExtension() !== 'yml') { continue; } - $configFileName = str_replace('.yml', '', $file->getFilename()); - $definition = $this->typedConfigManager->get($configFileName) - ->getDataDefinition(); + + try { + $configFileName = str_replace('.yml', '', $file->getFilename()); + $definition = $this->typedConfigManager->get($configFileName) + ->getDataDefinition(); + } + catch (\InvalidArgumentException $e) { + // Support optional configuration that are not installed on every + // instance. This test does not catch missing uuids on these configs. + if ($type === 'optional') { + continue; + } + + throw $e; + } // Skip configuration that doesn't require UUID. if (!isset($definition['mapping']['uuid'])) { From f917baf16c67a3d3cf3fb03bd926c2ecfb1227e5 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Tue, 30 Jul 2024 08:38:50 +0300 Subject: [PATCH 26/33] UHF-9708: Fix "Mismatched entity and/or field definitions" for published_at field --- helfi_platform_config.install | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/helfi_platform_config.install b/helfi_platform_config.install index e9f72d15a..72cc3ac42 100644 --- a/helfi_platform_config.install +++ b/helfi_platform_config.install @@ -145,3 +145,26 @@ function helfi_platform_config_update_9310() : void { $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); + } + } +} From 81e5b6b7cd4a5df4ccc7626901fb6c7aefd5ca6c Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Tue, 30 Jul 2024 12:40:29 +0300 Subject: [PATCH 27/33] Add option to only use local content Some non-core instances might not want to pull content from etusivu --- .../src/Plugin/Block/AnnouncementsBlock.php | 154 ++++++++---------- .../Plugin/Block/EtusivuEntityBlockBase.php | 95 +++++++++++ .../src/Plugin/Block/SurveyBlock.php | 141 ++++++---------- 3 files changed, 211 insertions(+), 179 deletions(-) diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/AnnouncementsBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/AnnouncementsBlock.php index 02f2b9352..6a56e006f 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/Block/AnnouncementsBlock.php +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/AnnouncementsBlock.php @@ -11,7 +11,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; -use Drupal\Core\Utility\Error; use Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient\Announcements; use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; @@ -41,82 +40,31 @@ final class AnnouncementsBlock extends EtusivuEntityBlockBase { /** * {@inheritdoc} */ - public function build(): array { - try { - $announcementNodes = $this->getLocalAnnouncements(); - $globalAnnouncements = $this->getExternalAnnouncements(); - $localAnnouncements = []; - } - catch (\Exception $e) { - Error::logException($this->logger, $e); - return []; - } - - $currentEntity = $this->getCurrentPageEntity(array_keys(self::ENTITY_TYPE_FIELDS)); - $referenceField = self::ENTITY_TYPE_FIELDS[$currentEntity?->getEntityTypeId()] ?? NULL; - - foreach ($announcementNodes as $announcementNode) { - assert($announcementNode instanceof FieldableEntityInterface); + protected function getLocalEntities(): array { + $langcodes = $this->getContentLangcodes(); + $storage = $this->entityTypeManager->getStorage('node'); - // Check if the announcement should be shown at all pages. - // Global announcements should be shown on top of all pages. - if ( - $announcementNode->hasField('field_publish_externally') && - $announcementNode->get('field_publish_externally')->value - ) { - $globalAnnouncements[] = $announcementNode; - continue; - } + // Get all published announcement nodes. + $query = $storage->getQuery() + ->accessCheck(TRUE) + ->condition('type', 'announcement') + ->condition('status', NodeInterface::PUBLISHED) + ->condition('langcode', $langcodes, 'IN'); - if ($announcementNode->get('field_announcement_all_pages')->value === "1") { - $localAnnouncements[] = $announcementNode; - continue; - } + $fields = $this->entityFieldManager->getFieldDefinitions('node', 'survey'); - if (!empty($referenceField) && $this->hasReference($referenceField, $announcementNode, $currentEntity)) { - // Add announcement to showed announcements if current page's entity - // is found from the list of referenced entities. - $localAnnouncements[] = $announcementNode; - } + // Query only local nodes. + if (isset($fields['field_publish_externally'])) { + $query->condition('field_publish_externally', FALSE); } - $this->sortAnnouncements($localAnnouncements); - - return $this - ->entityTypeManager - ->getViewBuilder('node') - ->viewMultiple(array_merge($globalAnnouncements, $localAnnouncements), 'default'); - } - - /** - * {@inheritdoc} - */ - public function getCacheContexts(): array { - return Cache::mergeContexts(parent::getCacheContexts(), [ - 'url.path', - 'languages:language_content', - ]); + return $storage->loadMultiple($query->execute()); } /** * {@inheritdoc} */ - public function getCacheTags(): array { - return Cache::mergeTags(parent::getCacheTags(), [ - 'node_list:announcement', - Announcements::$customCacheTag, - ]); - } - - /** - * Loads external announcement nodes. - * - * @return array - * External surveys. - * - * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException - */ - private function getExternalAnnouncements(): array { + protected function getRemoteEntities(): array { $entityStorage = $this->getExternalEntityStorage('helfi_announcements'); $nodes = []; @@ -147,33 +95,43 @@ private function getExternalAnnouncements(): array { } /** - * Loads local announcement nodes. - * - * @return array - * Local surveys. - * - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException - * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * {@inheritdoc} */ - private function getLocalAnnouncements(): array { - $langcodes = $this->getContentLangcodes(); - $storage = $this->entityTypeManager->getStorage('node'); + protected function sortEntities(array $local, array $remote): array { + $currentEntity = $this->getCurrentPageEntity(array_keys(self::ENTITY_TYPE_FIELDS)); + $referenceField = self::ENTITY_TYPE_FIELDS[$currentEntity?->getEntityTypeId()] ?? NULL; - // Get all published announcement nodes. - $query = $storage->getQuery() - ->accessCheck(TRUE) - ->condition('type', 'announcement') - ->condition('status', NodeInterface::PUBLISHED) - ->condition('langcode', $langcodes, 'IN'); + $localAnnouncements = []; + $globalAnnouncements = $remote; - $fields = $this->entityFieldManager->getFieldDefinitions('node', 'survey'); + foreach ($local as $announcementNode) { + assert($announcementNode instanceof FieldableEntityInterface); - // Query only local nodes. - if (isset($fields['field_publish_externally'])) { - $query->condition('field_publish_externally', FALSE); + // Check if the announcement should be shown at all pages. + // Global announcements should be shown on top of all pages. + if ( + $announcementNode->hasField('field_publish_externally') && + $announcementNode->get('field_publish_externally')->value + ) { + $globalAnnouncements[] = $announcementNode; + continue; + } + + if ($announcementNode->get('field_announcement_all_pages')->value === "1") { + $localAnnouncements[] = $announcementNode; + continue; + } + + if (!empty($referenceField) && $this->hasReference($referenceField, $announcementNode, $currentEntity)) { + // Add announcement to showed announcements if current page's entity + // is found from the list of referenced entities. + $localAnnouncements[] = $announcementNode; + } } - return $storage->loadMultiple($query->execute()); + $this->sortAnnouncements($localAnnouncements); + + return array_merge($globalAnnouncements, $localAnnouncements); } /** @@ -284,4 +242,24 @@ private function resolveVisibilityWeight(ContentEntityInterface $announcement): return self::VISIBILITY_ALL_WEIGHT; } + /** + * {@inheritdoc} + */ + public function getCacheContexts(): array { + return Cache::mergeContexts(parent::getCacheContexts(), [ + 'url.path', + 'languages:language_content', + ]); + } + + /** + * {@inheritdoc} + */ + public function getCacheTags(): array { + return Cache::mergeTags(parent::getCacheTags(), [ + 'node_list:announcement', + Announcements::$customCacheTag, + ]); + } + } diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php b/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php index ff1410046..ba4bf4616 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php @@ -10,10 +10,12 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Field\EntityReferenceFieldItemListInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\Core\Utility\Error; use Drupal\external_entities\ExternalEntityStorageInterface; use Drupal\helfi_api_base\Language\DefaultLanguageResolver; use Psr\Log\LoggerInterface; @@ -74,6 +76,99 @@ public static function create(ContainerInterface $container, array $configuratio ); } + /** + * {@inheritdoc} + */ + public function defaultConfiguration(): array { + return [ + 'use_remote_entities' => TRUE, + ]; + } + + /** + * {@inheritdoc} + */ + public function blockForm($form, FormStateInterface $form_state): array { + $form['use_remote_entities'] = [ + '#type' => 'boolean', + '#title' => $this->t('Fetch remote entities'), + '#description' => $this->t('This options should be disabled for non-core sites that do not want to pull remote content.'), + '#default_value' => $this->configuration['use_remote_entities'], + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function blockSubmit($form, FormStateInterface $form_state): void { + $this->configuration['use_remote_entities'] = $form_state->getValues()['use_remote_entities'] ?: FALSE; + } + + /** + * Checks if this block is configured to pull content from Etusivu. + * + * @return bool + * If content should be pulled from Etusivu. + * The block only shows local items when FALSE. + */ + public function useRemoteEntities(): bool { + return $this->configuration['use_remote_entities']; + } + + /** + * Gets a list of local entities the block should render. + * + * @return \Drupal\Core\Entity\EntityInterface[] + * Items the block should render. + * + * @throws \Exception + */ + abstract protected function getLocalEntities(): array; + + /** + * Gets a list of remote entities the block should render. + * + * @return \Drupal\Core\Entity\EntityInterface[] + * Items the block should render. + * + * @throws \Exception + */ + abstract protected function getRemoteEntities(): array; + + /** + * Sorts items the block should render. + * + * @return \Drupal\Core\Entity\EntityInterface[] + * Sorted entity list. + */ + abstract protected function sortEntities(array $local, array $remote): array; + + /** + * {@inheritdoc} + */ + public function build(): array { + try { + $local = $this->getLocalEntities(); + + // Some non-core instances might want to only show local entities. + // The provided configuration allows disabling the remote fetch. + $remote = $this->useRemoteEntities() ? $this->getRemoteEntities() : []; + } + catch (\Exception $e) { + Error::logException($this->logger, $e); + return []; + } + + $sorted = $this->sortEntities($local, $remote); + + return $this + ->entityTypeManager + ->getViewBuilder('node') + ->viewMultiple($sorted, 'default'); + } + /** * Get global entity storage. * diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php b/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php index 447c22cd1..e5d645c72 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/SurveyBlock.php @@ -7,9 +7,7 @@ use Drupal\Component\Utility\Xss; use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Cache\Cache; -use Drupal\Core\Entity\EntityInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; -use Drupal\Core\Utility\Error; use Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient\Surveys; use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; @@ -31,60 +29,12 @@ final class SurveyBlock extends EtusivuEntityBlockBase { ]; /** - * {@inheritdoc} + * {@inheritDoc} */ - public function build(): array { + protected function sortEntities($local, $remote) : array { $currentEntity = $this->getCurrentPageEntity(array_keys(self::ENTITY_TYPE_FIELDS)); - if ($survey = $this->getSurvey($currentEntity)) { - return $this - ->entityTypeManager - ->getViewBuilder('node') - ->view($survey, 'default'); - } - - return []; - } - - /** - * {@inheritdoc} - */ - public function getCacheContexts(): array { - return Cache::mergeContexts(parent::getCacheContexts(), [ - 'url.path', - 'languages:language_content', - ]); - } - - /** - * {@inheritdoc} - */ - public function getCacheTags(): array { - return Cache::mergeTags(parent::getCacheTags(), [ - Surveys::$customCacheTag, - 'node_list:survey', - ]); - } - - /** - * Loads most resent survey node that is valid for this page. - * - * @param \Drupal\Core\Entity\EntityInterface|null $currentEntity - * Entity on current page. - * - * @returns NodeInterface|null - * Most recent survey for this page or NULL if none exists. - */ - public function getSurvey(?EntityInterface $currentEntity) : ?NodeInterface { - try { - // Since the arrays contain numeric keys, the - // later values will be appended. - $surveys = array_merge($this->getExternalSurveys(), $this->getLocalSurveys()); - } - catch (\Exception $e) { - Error::logException($this->logger, $e); - return NULL; - } + $surveys = array_merge($remote, $local); // Sort by publised_at time. usort($surveys, static function (NodeInterface $a, NodeInterface $b) { @@ -98,32 +48,53 @@ public function getSurvey(?EntityInterface $currentEntity) : ?NodeInterface { $referenceField = self::ENTITY_TYPE_FIELDS[$currentEntity?->getEntityTypeId()] ?? NULL; + // Pick which survey to show. foreach ($surveys as $node) { // Check if the node should be shown at all pages. if ($node->get('field_survey_content_pages')->isEmpty()) { - return $node; + return [$node]; } // Show survey if current page's entity is found // from the list of referenced entities. if (!empty($referenceField) && $this->hasReference($referenceField, $node, $currentEntity)) { - return $node; + return [$node]; } } - return NULL; + return []; } /** - * Loads external survey nodes. - * - * @return array - * External surveys. - * - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException - * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * {@inheritDoc} */ - private function getExternalSurveys(): array { + protected function getLocalEntities() : array { + $langcodes = $this->getContentLangcodes(); + + $storage = $this->entityTypeManager->getStorage('node'); + + // Get all published survey nodes. + $query = $storage->getQuery() + ->accessCheck(TRUE) + ->condition('type', 'survey') + ->condition('status', NodeInterface::PUBLISHED) + ->condition('langcode', $langcodes, 'IN') + ->sort('published_at', 'DESC'); + + $fields = $this->entityFieldManager->getFieldDefinitions('node', 'survey'); + + // Query only local nodes. + if (isset($fields['field_publish_externally'])) { + $query->condition('field_publish_externally', FALSE); + } + + return array_values($storage->loadMultiple($query->execute())); + } + + /** + * {@inheritDoc} + */ + protected function getRemoteEntities(): array { $entityStorage = $this->getExternalEntityStorage('helfi_surveys'); $nodes = []; @@ -152,35 +123,23 @@ private function getExternalSurveys(): array { } /** - * Loads local surveys. - * - * @return array - * Local surveys. - * - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException - * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * {@inheritdoc} */ - private function getLocalSurveys() : array { - $langcodes = $this->getContentLangcodes(); - - $storage = $this->entityTypeManager->getStorage('node'); - - // Get all published survey nodes. - $query = $storage->getQuery() - ->accessCheck(TRUE) - ->condition('type', 'survey') - ->condition('status', NodeInterface::PUBLISHED) - ->condition('langcode', $langcodes, 'IN') - ->sort('published_at', 'DESC'); - - $fields = $this->entityFieldManager->getFieldDefinitions('node', 'survey'); - - // Query only local nodes. - if (isset($fields['field_publish_externally'])) { - $query->condition('field_publish_externally', FALSE); - } + public function getCacheContexts(): array { + return Cache::mergeContexts(parent::getCacheContexts(), [ + 'url.path', + 'languages:language_content', + ]); + } - return array_values($storage->loadMultiple($query->execute())); + /** + * {@inheritdoc} + */ + public function getCacheTags(): array { + return Cache::mergeTags(parent::getCacheTags(), [ + Surveys::$customCacheTag, + 'node_list:survey', + ]); } } From 0ca6e001849fb35b634a1f6063a1d4ccb90ad53e Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Tue, 30 Jul 2024 14:57:25 +0300 Subject: [PATCH 28/33] UHF-9708: Make survey link required Template crashes if survey is NULL --- .../install/field.field.node.survey.field_survey_link.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml index 48aaccc12..3c9bea366 100644 --- a/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml +++ b/modules/helfi_node_survey/config/install/field.field.node.survey.field_survey_link.yml @@ -13,7 +13,7 @@ entity_type: node bundle: survey label: Link description: '' -required: false +required: true translatable: true default_value: { } default_value_callback: '' From 1968deb36e52c776a0cb31872aed4cf239dbe2ac Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Tue, 30 Jul 2024 18:28:32 +0300 Subject: [PATCH 29/33] UHF-9708: Add test --- .../schema/helfi_etusivu_entities.schema.yml | 13 ++ .../helfi_etusivu_entities.info.yml | 1 + .../Plugin/Block/EtusivuEntityBlockBase.php | 4 +- .../src/Functional/LocalEntitiesTest.php | 118 ++++++++++++++++++ 4 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 modules/helfi_etusivu_entities/config/schema/helfi_etusivu_entities.schema.yml create mode 100644 modules/helfi_etusivu_entities/tests/src/Functional/LocalEntitiesTest.php diff --git a/modules/helfi_etusivu_entities/config/schema/helfi_etusivu_entities.schema.yml b/modules/helfi_etusivu_entities/config/schema/helfi_etusivu_entities.schema.yml new file mode 100644 index 000000000..b16bca1d6 --- /dev/null +++ b/modules/helfi_etusivu_entities/config/schema/helfi_etusivu_entities.schema.yml @@ -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 diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml b/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml index 30f2ab963..b9af58aaa 100644 --- a/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.info.yml @@ -3,6 +3,7 @@ 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 diff --git a/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php b/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php index ba4bf4616..776842856 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php +++ b/modules/helfi_etusivu_entities/src/Plugin/Block/EtusivuEntityBlockBase.php @@ -152,8 +152,8 @@ public function build(): array { try { $local = $this->getLocalEntities(); - // Some non-core instances might want to only show local entities. - // The provided configuration allows disabling the remote fetch. + // Some non-core instances might want to show only local entities. + // Block configuration allows disabling the remote entities. $remote = $this->useRemoteEntities() ? $this->getRemoteEntities() : []; } catch (\Exception $e) { diff --git a/modules/helfi_etusivu_entities/tests/src/Functional/LocalEntitiesTest.php b/modules/helfi_etusivu_entities/tests/src/Functional/LocalEntitiesTest.php new file mode 100644 index 000000000..716ed9777 --- /dev/null +++ b/modules/helfi_etusivu_entities/tests/src/Functional/LocalEntitiesTest.php @@ -0,0 +1,118 @@ +testNode = $this->createNode([ + 'status' => NodeInterface::PUBLISHED, + 'type' => 'page', + 'title' => 'Test node', + 'langcode' => 'en', + ]); + + $this->placeBlock('surveys', [ + 'use_remote_entities' => FALSE, + ]); + + $this->placeBlock('announcements', [ + 'use_remote_entities' => FALSE, + ]); + + $this->grantPermissions(Role::load('anonymous'), ['access content']); + } + + /** + * Tests blocks. + */ + public function testBlocks(): void { + $this->createNode([ + 'status' => NodeInterface::PUBLISHED, + 'type' => 'survey', + 'title' => 'Old test survey', + 'body' => 'Old survey content', + 'langcode' => 'en', + // Hide this if there is a survey published after 2000-01-01. + 'published_at' => 946677600, + 'field_survey_link' => 'https://example.com', + ]); + + // Only the newest survey is shown. + $this->createNode([ + 'status' => NodeInterface::PUBLISHED, + 'type' => 'survey', + 'title' => 'New test survey', + 'body' => 'New survey content', + 'langcode' => 'en', + 'field_survey_link' => 'https://example.com', + // Only show this on test node. + 'field_survey_content_pages' => [ + [ + 'target_id' => $this->testNode->id(), + ], + ], + ]); + + $this->createNode([ + 'status' => NodeInterface::PUBLISHED, + 'type' => 'announcement', + 'title' => 'Test announcement', + 'body' => 'Announcement content', + 'langcode' => 'en', + ]); + + $this->drupalGet('/'); + + $this->assertSession()->pageTextContains('Old test survey'); + $this->assertSession()->pageTextNotContains('New test survey'); + $this->assertSession()->pageTextContains('Test announcement'); + + $this->drupalGet($this->testNode->toUrl()); + + $this->assertSession()->pageTextContains('New test survey'); + $this->assertSession()->pageTextNotContains('Old test survey'); + $this->assertSession()->pageTextContains('Test announcement'); + } + +} From c6095c6581b81cca1a30cc349c7cdda083be2311 Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Sun, 11 Aug 2024 15:07:13 +0300 Subject: [PATCH 30/33] Rename base class --- .../src/Plugin/ExternalEntities/StorageClient/Announcements.php | 2 +- .../{EtusivuEntityBase.php => EtusivuJsonApiEntityBase.php} | 2 +- .../src/Plugin/ExternalEntities/StorageClient/Surveys.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/{EtusivuEntityBase.php => EtusivuJsonApiEntityBase.php} (97%) diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php index dd0fc5226..22afae760 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Announcements.php @@ -13,7 +13,7 @@ * description = @Translation("Retrieves announcements from helfi") * ) */ -final class Announcements extends EtusivuEntityBase { +final class Announcements extends EtusivuJsonApiEntityBase { /** * {@inheritdoc} diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuJsonApiEntityBase.php similarity index 97% rename from modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php rename to modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuJsonApiEntityBase.php index 1dd5883d9..db77a9887 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuEntityBase.php +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuJsonApiEntityBase.php @@ -24,7 +24,7 @@ /** * Base class for etusivu external entity storage client. */ -abstract class EtusivuEntityBase extends ExternalEntityStorageClientBase implements LoggerAwareInterface { +abstract class EtusivuJsonApiEntityBase extends ExternalEntityStorageClientBase implements LoggerAwareInterface { use LoggerAwareTrait; diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php index cb702c01b..b5c329988 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/Surveys.php @@ -13,7 +13,7 @@ * description = @Translation("Retrieves surveys from helfi") * ) */ -final class Surveys extends EtusivuEntityBase { +final class Surveys extends EtusivuJsonApiEntityBase { /** * Custom cache tag for announcements. From fc44f1e91b8478b4ba221a1639c56d65a2173eff Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Sun, 11 Aug 2024 16:09:14 +0300 Subject: [PATCH 31/33] Cache json:api requests External entities were tied to main request so each etusivu entity block caused at least one json:api requests. This adds separate cache for JSON:API responses so that they are cached separately from block caching rules. These caches are invalidated by etusivu using pubsub service. --- .../EtusivuJsonApiEntityBase.php | 36 +++++++-- .../tests/fixtures/survey.json | 55 +++++++++++++ .../src/Kernel/EtusivuJsonApiEntityTest.php | 77 +++++++++++++++++++ 3 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 modules/helfi_etusivu_entities/tests/fixtures/survey.json create mode 100644 modules/helfi_etusivu_entities/tests/src/Kernel/EtusivuJsonApiEntityTest.php diff --git a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuJsonApiEntityBase.php b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuJsonApiEntityBase.php index db77a9887..c7c03eb88 100644 --- a/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuJsonApiEntityBase.php +++ b/modules/helfi_etusivu_entities/src/Plugin/ExternalEntities/StorageClient/EtusivuJsonApiEntityBase.php @@ -4,6 +4,7 @@ namespace Drupal\helfi_etusivu_entities\Plugin\ExternalEntities\StorageClient; +use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageManagerInterface; @@ -56,6 +57,11 @@ abstract class EtusivuJsonApiEntityBase extends ExternalEntityStorageClientBase */ protected LanguageManagerInterface $languageManager; + /** + * Cache service. + */ + protected CacheBackendInterface $cache; + /** * {@inheritdoc} */ @@ -68,6 +74,7 @@ public static function create( $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); $instance->client = $container->get('http_client'); $instance->languageManager = $container->get('language_manager'); + $instance->cache = $container->get('cache.default'); /** @var \Drupal\helfi_api_base\Environment\EnvironmentResolver $environmentResolver */ $environmentResolver = $container->get('helfi_api_base.environment_resolver'); @@ -125,6 +132,10 @@ public function delete(ExternalEntityInterface $entity) : void { /** * Creates a request against JSON:API. * + * JSON:API responses are cached with custom cache tags to increase main + * request performance. These caches are invalidated with pubsub service + * whenever remote entities in etusivu instance are changed. + * * @param string $endpoint * The jsonapi endpoint. * @param array $parameters @@ -134,24 +145,37 @@ public function delete(ExternalEntityInterface $entity) : void { * * @return array * An array of entities. + * + * @see \helfi_etusivu_invalidate_external_caches() */ protected function request(string $endpoint, array $parameters, string $langcode) : array { if (!$this->environment) { return []; } + + $uri = vsprintf('%s/jsonapi%s?%s', [ + $this->environment->getInternalAddress($langcode), + $endpoint, + Query::build($parameters), + ]); + + if ($cache = $this->cache->get($uri)) { + return $cache->data; + } + try { - $uri = vsprintf('%s/jsonapi%s?%s', [ - $this->environment->getInternalAddress($langcode), - $endpoint, - Query::build($parameters), - ]); $content = $this->client->request('GET', $uri); $json = Utils::jsonDecode($content->getBody()->getContents(), TRUE); - return $json['data']; + $data = $json['data']; + + $this->cache->set($uri, $data, tags: [static::$customCacheTag]); + + return $data; } catch (RequestException | GuzzleException $e) { Error::logException($this->logger, $e); } + return []; } diff --git a/modules/helfi_etusivu_entities/tests/fixtures/survey.json b/modules/helfi_etusivu_entities/tests/fixtures/survey.json new file mode 100644 index 000000000..83cc3e8a1 --- /dev/null +++ b/modules/helfi_etusivu_entities/tests/fixtures/survey.json @@ -0,0 +1,55 @@ +{ + "jsonapi": { + "version": "1.0", + "meta": { + "links": { + "self": { + "href": "http://jsonapi.org/format/1.0/" + } + } + } + }, + "data": [ + { + "type": "node--survey", + "id": "f218e9a7-3ae1-4858-afb4-bf7e773d9ab6", + "links": { + "self": { + "href": "https://helfi-etusivu.docker.so/fi/jsonapi/node/survey/f218e9a7-3ae1-4858-afb4-bf7e773d9ab6?resourceVersion=id%3A27797" + } + }, + "attributes": { + "langcode": "en", + "status": true, + "title": "Test survey", + "unpublish_on": null, + "published_at": "2024-07-30T11:58:29+00:00", + "field_survey_link": { + "uri": "https://example.com", + "title": "", + "options": [] + }, + "body": { + "value": "

Survey content

", + "format": "minimal", + "processed": "

Survey content

\n", + "summary": "" + } + } + } + ], + "meta": { + "count": 2 + }, + "links": { + "last": { + "href": "https://helfi-etusivu.docker.so/fi/jsonapi/node/survey?fields%5Bnode--survey%5D=id%2Clangcode%2Cstatus%2Cpublished_at%2Cunpublish_on%2Ctitle%2Cbody%2Cfield_survey_link&fields%5Bstatus%5D=1&filter%5Bstatus%5D%5Bvalue%5D=1&filter%5Bfield_publish_externally%5D%5Bvalue%5D=1&filter%5Blangcode%5D=fi&sort=-published_at&page%5Boffset%5D=1&page%5Blimit%5D=1" + }, + "next": { + "href": "https://helfi-etusivu.docker.so/fi/jsonapi/node/survey?fields%5Bnode--survey%5D=id%2Clangcode%2Cstatus%2Cpublished_at%2Cunpublish_on%2Ctitle%2Cbody%2Cfield_survey_link&fields%5Bstatus%5D=1&filter%5Bstatus%5D%5Bvalue%5D=1&filter%5Bfield_publish_externally%5D%5Bvalue%5D=1&filter%5Blangcode%5D=fi&sort=-published_at&page%5Boffset%5D=1&page%5Blimit%5D=1" + }, + "self": { + "href": "https://helfi-etusivu.docker.so/fi/jsonapi/node/survey?fields%5Bnode--survey%5D=id%2Clangcode%2Cstatus%2Cpublished_at%2Cunpublish_on%2Ctitle%2Cbody%2Cfield_survey_link&fields%5Bstatus%5D=1&filter%5Bstatus%5D%5Bvalue%5D=1&filter%5Bfield_publish_externally%5D%5Bvalue%5D=1&filter%5Blangcode%5D=fi&page%5Blimit%5D=1&sort=-published_at" + } + } +} diff --git a/modules/helfi_etusivu_entities/tests/src/Kernel/EtusivuJsonApiEntityTest.php b/modules/helfi_etusivu_entities/tests/src/Kernel/EtusivuJsonApiEntityTest.php new file mode 100644 index 000000000..76696b255 --- /dev/null +++ b/modules/helfi_etusivu_entities/tests/src/Kernel/EtusivuJsonApiEntityTest.php @@ -0,0 +1,77 @@ +installConfig(['helfi_etusivu_entities']); + $this->setActiveProject(Project::ETUSIVU, $this->environment); + } + + /** + * Make sure that cache is used. + */ + public function testRequestCache(): void { + $this->setupMockHttpClient([ + new Response(body: file_get_contents(__DIR__ . "/../../fixtures/survey.json")), + new Response(body: json_encode(['data' => []])), + ]); + + $entityTypeManager = $this->container->get(EntityTypeManagerInterface::class); + $storage = $entityTypeManager->getStorage('helfi_announcements'); + + // Cache should be used on seconds request. + $this->assertNotEmpty($storage->loadMultiple()); + $this->assertNotEmpty($storage->loadMultiple()); + + /** @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cacheTagInvalidator */ + $cacheTagInvalidator = $this->container->get(CacheTagsInvalidatorInterface::class); + $cacheTagInvalidator->invalidateTags([Announcements::$customCacheTag]); + + // Cache should not be used. + $this->assertEmpty($storage->loadMultiple()); + } + +} From 3ded9f914ae952f41cfce3315cf16b9af4279abd Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Mon, 12 Aug 2024 10:44:11 +0300 Subject: [PATCH 32/33] Remove old comments --- modules/helfi_etusivu_entities/helfi_etusivu_entities.module | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module index 3087c0d0e..da1120ff2 100644 --- a/modules/helfi_etusivu_entities/helfi_etusivu_entities.module +++ b/modules/helfi_etusivu_entities/helfi_etusivu_entities.module @@ -34,8 +34,6 @@ use Drupal\entity\BundleFieldDefinition; */ function helfi_etusivu_entities_get_block_configurations(string $theme) : array { return [ - // External entity version of this block is - // enabled by helfi_etusivu_entities module. 'announcements' => [ 'block' => [ 'id' => 'announcements', @@ -59,8 +57,6 @@ function helfi_etusivu_entities_get_block_configurations(string $theme) : array ], ], ], - // External entity version of this block is - // enabled by helfi_etusivu_entities module. 'surveys' => [ 'block' => [ 'id' => 'surveys', From c2c5619493b007fb8e62047fbfbd0364f404618b Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Mon, 12 Aug 2024 11:55:35 +0300 Subject: [PATCH 33/33] Add link to module README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d1b1a07f9..4649a7c28 100644 --- a/README.md +++ b/README.md @@ -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