From 1cbea43371cb781560971bb1601a7d4c1c98109c Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Wed, 16 Oct 2024 15:02:22 +0300 Subject: [PATCH 01/18] UHF-10259: Curated event list - Add new paragraph: curated_event_list - Add new external entity for Linked Events events - Add new storageclient for retrieving events - Add custom constraint and validator - Add translations --- ...y.paragraph.curated_event_list.default.yml | 36 ++++ ...vents_event.linkedevents_event.default.yml | 73 ++++++++ ...y.paragraph.curated_event_list.default.yml | 38 ++++ ...xternal_entity_type.linkedevents_event.yml | 42 +++++ ...agraph.curated_event_list.field_events.yml | 32 ++++ .../field.storage.paragraph.field_events.yml | 20 ++ ...phs.paragraphs_type.curated_event_list.yml | 10 + ...rated_event_list.field_all_events_link.yml | 5 + ...agraph.curated_event_list.field_events.yml | 2 + ...phs.paragraphs_type.curated_event_list.yml | 2 + .../helfi_curated_event_list.info.yml | 11 ++ .../helfi_curated_event_list.module | 109 +++++++++++ .../ExternalEntities/StorageClient/Events.php | 176 ++++++++++++++++++ .../Constraint/CuratedEventConstraint.php | 24 +++ .../CuratedEventConstraintValidator.php | 35 ++++ .../translations/fi.po | 5 + .../translations/sv.po | 5 + 17 files changed, 625 insertions(+) create mode 100644 modules/helfi_paragraphs_curated_event_list/config/install/core.entity_form_display.paragraph.curated_event_list.default.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.linkedevents_event.linkedevents_event.default.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.paragraph.curated_event_list.default.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/config/install/field.storage.paragraph.field_events.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/config/install/paragraphs.paragraphs_type.curated_event_list.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_all_events_link.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_events.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/config/optional/paragraphs.paragraphs_type.curated_event_list.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.info.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.module create mode 100644 modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php create mode 100644 modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraint.php create mode 100644 modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraintValidator.php create mode 100644 modules/helfi_paragraphs_curated_event_list/translations/fi.po create mode 100644 modules/helfi_paragraphs_curated_event_list/translations/sv.po diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_form_display.paragraph.curated_event_list.default.yml b/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_form_display.paragraph.curated_event_list.default.yml new file mode 100644 index 000000000..fc339086a --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_form_display.paragraph.curated_event_list.default.yml @@ -0,0 +1,36 @@ +uuid: b5c5ed81-d651-4db2-8d72-a67e7b66390e +langcode: en +status: true +dependencies: + config: + - field.field.paragraph.curated_event_list.field_all_events_link + - field.field.paragraph.curated_event_list.field_events + - paragraphs.paragraphs_type.curated_event_list + module: + - link +id: paragraph.curated_event_list.default +targetEntityType: paragraph +bundle: curated_event_list +mode: default +content: + field_all_events_link: + type: link_default + weight: 1 + region: content + settings: + placeholder_url: '' + placeholder_title: '' + third_party_settings: { } + field_events: + type: entity_reference_autocomplete + weight: 0 + region: content + settings: + match_operator: CONTAINS + match_limit: 5 + size: 60 + placeholder: '' + third_party_settings: { } +hidden: + created: true + status: true diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.linkedevents_event.linkedevents_event.default.yml b/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.linkedevents_event.linkedevents_event.default.yml new file mode 100644 index 000000000..7b3c6260b --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.linkedevents_event.linkedevents_event.default.yml @@ -0,0 +1,73 @@ +uuid: 22363443-8bc6-4b97-980f-6bfb225edb1b +langcode: fi +status: true +dependencies: + config: + - responsive_image.styles.card_teaser + module: + - datetime + - external_entities + - imagecache_external + - linkit +id: linkedevents_event.linkedevents_event.default +targetEntityType: linkedevents_event +bundle: linkedevents_event +mode: default +content: + end_time: + type: datetime_default + label: hidden + settings: + timezone_override: '' + format_type: medium + third_party_settings: { } + weight: 3 + region: content + external_link: + type: linkit + label: hidden + settings: + trim_length: 80 + url_only: false + url_plain: false + rel: '' + target: '' + linkit_profile: default + third_party_settings: { } + weight: 4 + region: content + langcode: + type: string_default + label: hidden + settings: { } + third_party_settings: { } + weight: 5 + region: content + main_image: + type: imagecache_external_responsive_image + label: hidden + settings: + imagecache_external_link: '' + imagecache_external_responsive_style: card_teaser + third_party_settings: { } + weight: 1 + region: content + start_time: + type: datetime_default + label: hidden + settings: + timezone_override: '' + format_type: medium + third_party_settings: { } + weight: 2 + region: content + title: + type: string + label: hidden + settings: + link_to_entity: false + third_party_settings: { } + weight: 0 + region: content +hidden: + search_api_excerpt: true diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.paragraph.curated_event_list.default.yml b/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.paragraph.curated_event_list.default.yml new file mode 100644 index 000000000..0fea77b3c --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.paragraph.curated_event_list.default.yml @@ -0,0 +1,38 @@ +uuid: 313ec60d-d84a-4e82-9ae3-ee3221c0e631 +langcode: en +status: true +dependencies: + config: + - field.field.paragraph.curated_event_list.field_all_events_link + - field.field.paragraph.curated_event_list.field_events + - paragraphs.paragraphs_type.curated_event_list + module: + - link +id: paragraph.curated_event_list.default +targetEntityType: paragraph +bundle: curated_event_list +mode: default +content: + field_all_events_link: + type: link + label: hidden + settings: + trim_length: 80 + url_only: false + url_plain: false + rel: '' + target: '' + third_party_settings: { } + weight: 1 + region: content + field_events: + type: entity_reference_entity_view + label: hidden + settings: + view_mode: default + link: false + third_party_settings: { } + weight: 0 + region: content +hidden: + search_api_excerpt: true diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml b/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml new file mode 100644 index 000000000..3488a41ba --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml @@ -0,0 +1,42 @@ +uuid: 66fe8cbc-203c-40aa-ac7b-6e0e89470312 +langcode: en +status: true +dependencies: { } +id: linkedevents_event +label: 'LinkedEvents: Event' +label_plural: 'LinkedEvents: Events' +description: 'Event entities from https://api.hel.fi/linkedevents.' +read_only: true +generate_aliases: false +field_mapper_id: jsonpath +field_mapper_config: + field_mappings: + id: + value: $.id + uuid: + value: $.id + title: + value: $.title + main_image: + uri: '$.images[0].url' + title: '$.images[0].name' + start_time: + value: $.start_time + end_time: + value: $.end_time + external_link: + uri: $.external_link + title: $.clean_title + main_image_title: + value: '$.images[0].name' + main_image_alt: + value: '$.images[0].alt_text' + photographer_name: + value: '$.images[0].photographer_name' +storage_client_id: linkedevents_events +storage_client_config: null +persistent_cache_max_age: 10800 +annotation_entity_type_id: null +annotation_bundle_id: null +annotation_field_name: null +inherits_annotation_fields: false diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml b/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml new file mode 100644 index 000000000..d0075879a --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml @@ -0,0 +1,32 @@ +uuid: 41acbd4d-63ba-4cb7-919c-d6435553d4a9 +langcode: en +status: true +dependencies: + config: + - field.storage.paragraph.field_events + - paragraphs.paragraphs_type.curated_event_list + module: + - disable_field +third_party_settings: + disable_field: + add_disable: none + edit_disable: none +id: paragraph.curated_event_list.field_events +field_name: field_events +entity_type: paragraph +bundle: curated_event_list +label: Events +description: "Pick events to display in the list. You may eihter search event by their name or enter their ID. \r\n

\r\nYou can find the ID of an event on tapahtumat.hel.fi site from the URL of the page. For example, in the url https://tapahtumat.hel.fi/fi/events/helsinki:agiy4m4ida the helsinki:agiy4m4ida part is the ID of the event.\r\n

\r\nThe page will display the top three events. You can arrange the events by dragging. Expired events will be automatically removed from the page." +required: false +translatable: false +default_value: { } +default_value_callback: '' +settings: + handler: 'default:linkedevents_event' + handler_settings: + target_bundles: null + sort: + field: _none + direction: ASC + auto_create: false +field_type: entity_reference diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/field.storage.paragraph.field_events.yml b/modules/helfi_paragraphs_curated_event_list/config/install/field.storage.paragraph.field_events.yml new file mode 100644 index 000000000..5c1940b34 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/install/field.storage.paragraph.field_events.yml @@ -0,0 +1,20 @@ +uuid: a381685c-627a-4ba4-8be3-5bb2d2a705bd +langcode: en +status: true +dependencies: + module: + - external_entities + - paragraphs +id: paragraph.field_events +field_name: field_events +entity_type: paragraph +type: entity_reference +settings: + target_type: linkedevents_event +module: core +locked: false +cardinality: -1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/paragraphs.paragraphs_type.curated_event_list.yml b/modules/helfi_paragraphs_curated_event_list/config/install/paragraphs.paragraphs_type.curated_event_list.yml new file mode 100644 index 000000000..7613b6f23 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/install/paragraphs.paragraphs_type.curated_event_list.yml @@ -0,0 +1,10 @@ +uuid: 5b0c689e-1733-47ff-ba7c-63923e4f2116 +langcode: en +status: true +dependencies: { } +id: curated_event_list +label: 'Curated event list' +icon_uuid: null +icon_default: null +description: 'Event list with curated event picks loaded from Linked Events API.' +behavior_plugins: { } diff --git a/modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_all_events_link.yml b/modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_all_events_link.yml new file mode 100644 index 000000000..baa262793 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_all_events_link.yml @@ -0,0 +1,5 @@ +label: 'Kaikki tapahtumat -linkki' +description: "Lisää likki tapahtumat.hel.fi -sivulle. Voit halutessasi tehdä tapahtumakalenterissa haluamasi tapahtumien rajauksen ja kopioida sivun URL-osoitteen.\r\nLisää myös teksti linkille, esimerkiksi. Katso kaikki tapahtumat" +default_value: + - + title: 'Katso kaikki tapahtumat' diff --git a/modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_events.yml b/modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_events.yml new file mode 100644 index 000000000..65cac6a6e --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_events.yml @@ -0,0 +1,2 @@ +label: Tapahtumat +description: "Valitse tapahtumat, jotka näytetään listassa. Voit hakea tapahtumaa sen nimellä tai tapahtuman ID:llä.\r\n

\r\nLöydät tapahtuman ID:n tapahtumat.hel.fi-sivun osoitteesta: esimerkiksi osoitteessa https://tapahtumat.hel.fi/fi/events/helsinki:agiy4m4ida kohta helsinki:agiy4m4ida on tapahtuman ID.\r\n

\r\nSivulla näytetään kolme ylintä tapahtumaa. Voit järjestää tapahtumia raahaamalla. Vanhentuneet tapahtumat poistuvat sivulta automaattisesti." diff --git a/modules/helfi_paragraphs_curated_event_list/config/optional/paragraphs.paragraphs_type.curated_event_list.yml b/modules/helfi_paragraphs_curated_event_list/config/optional/paragraphs.paragraphs_type.curated_event_list.yml new file mode 100644 index 000000000..7d8f759d8 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/optional/paragraphs.paragraphs_type.curated_event_list.yml @@ -0,0 +1,2 @@ +label: 'Kuratoitu tapahtumalista' +description: 'Manuaalisesti ylläpidettävä tapahtumalista tapahtumille Linked Events -rajapinnasta.' diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.info.yml b/modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.info.yml new file mode 100644 index 000000000..fc3065ff4 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.info.yml @@ -0,0 +1,11 @@ +name: 'HELfi Curated Event List' +type: module +core_version_requirement: '^9 || ^10' +dependencies: + - external_entities:external_entities + - drupal:link + - helfi_api_base:helfi_api_base + - helfi_platform_config:helfi_platform_config + - paragraphs:paragraphs +'interface translation project': helfi_paragraphs_curated_event_list +'interface translation server pattern': modules/contrib/helfi_platform_config/modules/helfi_paragraphs_curated_event_list/translations/%language.po diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.module new file mode 100644 index 000000000..0911878b8 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.module @@ -0,0 +1,109 @@ +end_time->date; + if ( + $end->getTimeStamp() < $time + ) { + $past_events[] = $item; + } + else { + $future_or_ongoing[] = $item; + } + } + + $variables['past_events'] = $past_events; + // Always show a maximum if 3 events. + $variables['future_or_ongoing'] = array_slice($future_or_ongoing, 0, 3); +} + +/** + * Implements hook_preprocess_HOOK(). + */ +function helfi_paragraphs_curated_event_list_preprocess_external_entity__linkedevents_event( + array &$variables +): void { + $event = $variables['external_entity']; + $variables['start_timestamp'] = $event->start_time->date->getTimeStamp(); + $variables['end_timestamp'] = $event->end_time->date->getTimeStamp(); +} + +/** + * Implements hook_entity_bundle_field_info_alter(). + */ +function helfi_paragraphs_curated_event_list_entity_bundle_field_info_alter( + &$fields, + EntityTypeInterface $entity_type, + $bundle, +): void { + if ($entity_type->id() === 'linkedevents_event') { + $fields['main_image'] = BundleFieldDefinition::create('link') + ->setName('main_image') + ->setLabel('Main image') + ->setTargetEntityTypeId($entity_type->id()) + ->setTargetBundle($bundle) + ->setSettings([ + 'max_length' => 1024, + ]) + ->setDisplayConfigurable('view', TRUE); + $fields['start_time'] = BundleFieldDefinition::create('datetime') + ->setName('start_time') + ->setLabel('Event start time') + ->setDefaultValue('') + ->setDisplayOptions('view', [ + 'label' => 'hidden', + 'type' => 'datetime_default', + ]) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayConfigurable('form', TRUE); + $fields['end_time'] = BundleFieldDefinition::create('datetime') + ->setName('end_time') + ->setLabel('Event end time') + ->setDefaultValue('') + ->setDisplayOptions('view', [ + 'label' => 'hidden', + 'type' => 'datetime_default', + ]) + ->setDisplayConfigurable('view', TRUE) + ->setDisplayConfigurable('form', TRUE); + $fields['external_link'] = BundleFieldDefinition::create('link') + ->setName('external_link') + ->setLabel('External link') + ->setDefaultValue('') + ->setDisplayOptions('view', [ + 'label' => 'hidden', + 'type' => 'link_default', + ]) + ->setTranslatable(TRUE) + ->setDisplayConfigurable('view', TRUE); + } + + if ( + $entity_type->id() === 'paragraph' && + $bundle === 'curated_event_list' && + array_key_exists('field_events', $fields) + ) { + $fields['field_events']->addConstraint('CuratedEvent', []); + } +} diff --git a/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php b/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php new file mode 100644 index 000000000..0fe2797b9 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php @@ -0,0 +1,176 @@ +languageManager = $container->get('language_manager'); + $instance->client = $container->get('http_client'); + $instance->logger = $container->get('logger.factory')->get('helfi_external_entity'); + + return $instance; + } + + /** + * {@inheritdoc} + */ + public function save(ExternalEntityInterface $entity) : int { + return 1; + } + + /** + * {@inheritdoc} + */ + public function delete(ExternalEntityInterface $entity) : void { + } + + /** + * {@inheritdoc} + */ + public function loadMultiple(?array $ids = NULL) : array { + return $this->query(['ids' => $ids]); + } + + /** + * {@inheritdoc} + */ + public function query( + array $parameters = [], + array $sorts = [], + $start = NULL, + $length = NULL, + ) : array { + $langcode = $this->languageManager + ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) + ->getId(); + + // Run when loading a list of entities eg. when creating autocomplete optionlist + if (isset($parameters['ids'])) { + $endpoint = 'event'; + $query = http_build_query([ + 'ids' => implode(',', $parameters['ids']), + 'language' => $langcode + ]); + } + else if ( + isset($parameters[0]['field']) && + $parameters[0]['field'] === 'id' + ) { + $endpoint = 'event'; + $query = http_build_query([ + 'ids' => implode(',', $parameters[0]['value']), + 'language' => $langcode + ]); + } + else if (preg_match('/.{0,15}:.{0,40}/i', $parameters[0]['value'])) { + $endpoint = 'event'; + $query = http_build_query( + [ + 'ids' => $parameters[0]['value'], + 'language' => $langcode + ] + ); + } + // Run when receiving input from autocomplete field + else { + $endpoint = 'search'; + $query = http_build_query([ + 'input' => $parameters[0]['value'], + 'language' => $langcode, + 'start' => date('Y-m-d'), + 'type' => 'event', + ]); + } + + $uri = sprintf('%s/%s?%s', self::API_URL, $endpoint, $query); + + try { + $content = $this->client->request('GET', $uri); + $json = Utils::jsonDecode($content->getBody()->getContents(), TRUE); + + if (empty($json['data'])) { + return []; + } + } + catch(\Throwable $t) { + $this->logger->error( + 'Linked Events external entity request failed with error: ' . + $t->getMessage() + ); + + return []; + } + + $prepared = []; + foreach($json['data'] as $event) { + if (!isset($event['name'][$langcode])) { + $this->logger->error( + 'Event with id: ' . $event['id'] . ' has no name in language: ' . $langcode + ); + + continue; + } + + $event['clean_title'] = $event['name'][$langcode]; + $start = new \DateTime($event['start_time']); + $event['title'] = $event['clean_title'] . ' (' . $start->format('d.m.Y H:i') . ')'; + $event['external_link'] = self::EVENTS_BASE_URL . $event['id']; + $prepared[$event['id']] = $event; + } + + return $prepared; + } +} diff --git a/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraint.php b/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraint.php new file mode 100644 index 000000000..031454906 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraint.php @@ -0,0 +1,24 @@ +referencedEntities(); + + // Early return if no events are selected. + if (empty($events)) { + return; + } + + foreach($events as $key => $event) { + if ($event->end_time->date->getTimeStamp() < time()) { + $this->context->buildViolation($constraint->containsEndedEvents, ['%title' => $event->title->value]) + ->atPath($key) + ->addViolation(); + } + } + } +} diff --git a/modules/helfi_paragraphs_curated_event_list/translations/fi.po b/modules/helfi_paragraphs_curated_event_list/translations/fi.po new file mode 100644 index 000000000..9e6624533 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/translations/fi.po @@ -0,0 +1,5 @@ +msgid "" +msgstr "" + +msgid "Event %title has ended. Only upcoming or ongoing events are allowed." +msgstr "Tapahtuma %title on päättynyt. Listaan voi lisätä vain tulevia tai käynnissä olevia tapahtumia." diff --git a/modules/helfi_paragraphs_curated_event_list/translations/sv.po b/modules/helfi_paragraphs_curated_event_list/translations/sv.po new file mode 100644 index 000000000..0f25454eb --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/translations/sv.po @@ -0,0 +1,5 @@ +msgid "" +msgstr "" + +msgid "Event %title has ended. Only upcoming or ongoing events are allowed." +msgstr "Evenemanget %title har avslutats. Endast kommande eller pågående evenemang är tillåtna." From eeabe62d665e1704b6ecbce294e2f386d956cb2b Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Fri, 18 Oct 2024 15:43:12 +0300 Subject: [PATCH 02/18] UHF-10259: Enable curated event list module via update hook --- helfi_platform_config.install | 12 +++++++ ...vents_event.linkedevents_event.default.yml | 2 +- ...rated_event_list.field_all_events_link.yml | 33 +++++++++++++++++++ ...torage.paragraph.field_all_events_link.yml | 19 +++++++++++ ...rated_event_list.field_all_events_link.yml | 0 ...agraph.curated_event_list.field_events.yml | 0 ...phs.paragraphs_type.curated_event_list.yml | 0 ...fi_paragraphs_curated_event_list.info.yml} | 0 ...elfi_paragraphs_curated_event_list.module} | 20 +++++++++++ 9 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_all_events_link.yml create mode 100644 modules/helfi_paragraphs_curated_event_list/config/install/field.storage.paragraph.field_all_events_link.yml rename modules/helfi_paragraphs_curated_event_list/config/optional/{ => languages/fi}/field.field.paragraph.curated_event_list.field_all_events_link.yml (100%) rename modules/helfi_paragraphs_curated_event_list/config/optional/{ => languages/fi}/field.field.paragraph.curated_event_list.field_events.yml (100%) rename modules/helfi_paragraphs_curated_event_list/config/optional/{ => languages/fi}/paragraphs.paragraphs_type.curated_event_list.yml (100%) rename modules/helfi_paragraphs_curated_event_list/{helfi_curated_event_list.info.yml => helfi_paragraphs_curated_event_list.info.yml} (100%) rename modules/helfi_paragraphs_curated_event_list/{helfi_curated_event_list.module => helfi_paragraphs_curated_event_list.module} (85%) diff --git a/helfi_platform_config.install b/helfi_platform_config.install index 7a7adeeb1..df8585bfc 100644 --- a/helfi_platform_config.install +++ b/helfi_platform_config.install @@ -181,3 +181,15 @@ function helfi_platform_config_update_9312() : void { \Drupal::service('module_installer')->install(['helfi_users']); } } + +/** + * UHF-10259: Enable helfi_paragraphs_curated_event_list module + */ +function helfi_platform_config_update_9313() : void { + $module_installer = \Drupal::service('module_installer'); + + // Enable helfi_paragraphs_curated_event_list. + if (!\Drupal::moduleHandler()->moduleExists('helfi_paragraphs_curated_event_list')) { + $module_installer->install(['helfi_paragraphs_curated_event_list']); + } +} diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.linkedevents_event.linkedevents_event.default.yml b/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.linkedevents_event.linkedevents_event.default.yml index 7b3c6260b..61c72afaf 100644 --- a/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.linkedevents_event.linkedevents_event.default.yml +++ b/modules/helfi_paragraphs_curated_event_list/config/install/core.entity_view_display.linkedevents_event.linkedevents_event.default.yml @@ -1,5 +1,5 @@ uuid: 22363443-8bc6-4b97-980f-6bfb225edb1b -langcode: fi +langcode: en status: true dependencies: config: diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_all_events_link.yml b/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_all_events_link.yml new file mode 100644 index 000000000..4900fc666 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_all_events_link.yml @@ -0,0 +1,33 @@ +uuid: b144b934-3c5a-4c5b-ae89-b477bfb8367d +langcode: en +status: true +dependencies: + config: + - field.storage.paragraph.field_all_events_link + - paragraphs.paragraphs_type.curated_event_list + module: + - disable_field + - link +third_party_settings: + disable_field: + add_disable: none + edit_disable: none +id: paragraph.curated_event_list.field_all_events_link +field_name: field_all_events_link +entity_type: paragraph +bundle: curated_event_list +label: 'All events link' +description: "Add a link to the tapahtumat.hel.fi website. You may also filter the events in the event calendar as desired and copy the page's URL.\r\n

\r\nAlso provide a text for the link, eg. View all events." +required: false +translatable: false +default_value: + - + attributes: { } + uri: 'https://tapahtumat.hel.fi' + title: 'View all events' + options: { } +default_value_callback: '' +settings: + title: 2 + link_type: 16 +field_type: link diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/field.storage.paragraph.field_all_events_link.yml b/modules/helfi_paragraphs_curated_event_list/config/install/field.storage.paragraph.field_all_events_link.yml new file mode 100644 index 000000000..1211aa9d9 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/config/install/field.storage.paragraph.field_all_events_link.yml @@ -0,0 +1,19 @@ +uuid: 1dd4d7e6-e3be-490e-9309-e9380e0c33b4 +langcode: en +status: true +dependencies: + module: + - link + - paragraphs +id: paragraph.field_all_events_link +field_name: field_all_events_link +entity_type: paragraph +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_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_all_events_link.yml b/modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/field.field.paragraph.curated_event_list.field_all_events_link.yml similarity index 100% rename from modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_all_events_link.yml rename to modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/field.field.paragraph.curated_event_list.field_all_events_link.yml diff --git a/modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_events.yml b/modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/field.field.paragraph.curated_event_list.field_events.yml similarity index 100% rename from modules/helfi_paragraphs_curated_event_list/config/optional/field.field.paragraph.curated_event_list.field_events.yml rename to modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/field.field.paragraph.curated_event_list.field_events.yml diff --git a/modules/helfi_paragraphs_curated_event_list/config/optional/paragraphs.paragraphs_type.curated_event_list.yml b/modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/paragraphs.paragraphs_type.curated_event_list.yml similarity index 100% rename from modules/helfi_paragraphs_curated_event_list/config/optional/paragraphs.paragraphs_type.curated_event_list.yml rename to modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/paragraphs.paragraphs_type.curated_event_list.yml diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.info.yml b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.info.yml similarity index 100% rename from modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.info.yml rename to modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.info.yml diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module similarity index 85% rename from modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.module rename to modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module index 0911878b8..6193ee3ed 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_curated_event_list.module +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module @@ -9,6 +9,7 @@ declare(strict_types=1); use Drupal\Core\Entity\EntityTypeInterface; use Drupal\entity\BundleFieldDefinition; +use Drupal\helfi_platform_config\DTO\ParagraphTypeCollection; /** * Implements hook_preprocess_HOOK(). @@ -107,3 +108,22 @@ function helfi_paragraphs_curated_event_list_entity_bundle_field_info_alter( $fields['field_events']->addConstraint('CuratedEvent', []); } } + +/** + * Implements hook_helfi_paragraph_types(). + */ +function helfi_paragraphs_curated_event_list_helfi_paragraph_types() : array { + $types = [ + 'field_content' => [ + 'curated_event_list' => 15, + ], + ]; + + $enabled = []; + foreach ($types as $field => $paragraphTypes) { + foreach ($paragraphTypes as $paragraphType => $weight) { + $enabled[] = new ParagraphTypeCollection('node', 'landing_page', $field, $paragraphType, $weight); + } + } + return $enabled; +} From f56e926e8d055165eb02ab5088713f7331d748f3 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Fri, 18 Oct 2024 17:34:55 +0300 Subject: [PATCH 03/18] UHF-10259: Fix phpcs --- helfi_platform_config.install | 2 +- ...helfi_paragraphs_curated_event_list.module | 4 ++-- .../ExternalEntities/StorageClient/Events.php | 22 ++++++++++--------- .../Constraint/CuratedEventConstraint.php | 6 ++--- .../CuratedEventConstraintValidator.php | 6 +++-- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/helfi_platform_config.install b/helfi_platform_config.install index df8585bfc..76e9a7c3e 100644 --- a/helfi_platform_config.install +++ b/helfi_platform_config.install @@ -183,7 +183,7 @@ function helfi_platform_config_update_9312() : void { } /** - * UHF-10259: Enable helfi_paragraphs_curated_event_list module + * UHF-10259: Enable helfi_paragraphs_curated_event_list module. */ function helfi_platform_config_update_9313() : void { $module_installer = \Drupal::service('module_installer'); diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module index 6193ee3ed..81cb525ee 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module @@ -21,7 +21,7 @@ function helfi_paragraphs_curated_event_list_preprocess_field__paragraph__field_ $future_or_ongoing = []; $time = time(); - foreach($variables['items'] as $item) { + foreach ($variables['items'] as $item) { $event = $item['content']['#linkedevents_event']; $end = $event->end_time->date; if ( @@ -43,7 +43,7 @@ function helfi_paragraphs_curated_event_list_preprocess_field__paragraph__field_ * Implements hook_preprocess_HOOK(). */ function helfi_paragraphs_curated_event_list_preprocess_external_entity__linkedevents_event( - array &$variables + array &$variables, ): void { $event = $variables['external_entity']; $variables['start_timestamp'] = $event->start_time->date->getTimeStamp(); diff --git a/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php b/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php index 0fe2797b9..a8199f337 100644 --- a/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php +++ b/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php @@ -1,6 +1,6 @@ getCurrentLanguage(LanguageInterface::TYPE_CONTENT) ->getId(); - // Run when loading a list of entities eg. when creating autocomplete optionlist + // Run when loading a list of entities + // eg. when creating autocomplete optionlist. if (isset($parameters['ids'])) { $endpoint = 'event'; $query = http_build_query([ 'ids' => implode(',', $parameters['ids']), - 'language' => $langcode + 'language' => $langcode, ]); } - else if ( + elseif ( isset($parameters[0]['field']) && $parameters[0]['field'] === 'id' ) { $endpoint = 'event'; $query = http_build_query([ 'ids' => implode(',', $parameters[0]['value']), - 'language' => $langcode + 'language' => $langcode, ]); } - else if (preg_match('/.{0,15}:.{0,40}/i', $parameters[0]['value'])) { + elseif (preg_match('/.{0,15}:.{0,40}/i', $parameters[0]['value'])) { $endpoint = 'event'; $query = http_build_query( [ 'ids' => $parameters[0]['value'], - 'language' => $langcode + 'language' => $langcode, ] ); } - // Run when receiving input from autocomplete field + // Run when receiving input from autocomplete field. else { $endpoint = 'search'; $query = http_build_query([ @@ -145,7 +146,7 @@ public function query( return []; } } - catch(\Throwable $t) { + catch (\Throwable $t) { $this->logger->error( 'Linked Events external entity request failed with error: ' . $t->getMessage() @@ -155,7 +156,7 @@ public function query( } $prepared = []; - foreach($json['data'] as $event) { + foreach ($json['data'] as $event) { if (!isset($event['name'][$langcode])) { $this->logger->error( 'Event with id: ' . $event['id'] . ' has no name in language: ' . $langcode @@ -173,4 +174,5 @@ public function query( return $prepared; } + } diff --git a/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraint.php b/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraint.php index 031454906..84df6af61 100644 --- a/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraint.php +++ b/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraint.php @@ -1,6 +1,6 @@ $event) { + foreach ($events as $key => $event) { if ($event->end_time->date->getTimeStamp() < time()) { $this->context->buildViolation($constraint->containsEndedEvents, ['%title' => $event->title->value]) ->atPath($key) @@ -32,4 +33,5 @@ public function validate(mixed $value, Constraint $constraint) { } } } + } From d7e74cbbf0b715dcafdd591f9fb52879381aece9 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Mon, 21 Oct 2024 07:15:54 +0300 Subject: [PATCH 04/18] UHF-10259: Change module name to more uniform one --- .../helfi_paragraphs_curated_event_list.info.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.info.yml b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.info.yml index fc3065ff4..ec2a1b92b 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.info.yml +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.info.yml @@ -1,4 +1,4 @@ -name: 'HELfi Curated Event List' +name: 'HELfi Paragraphs: Curated Event List' type: module core_version_requirement: '^9 || ^10' dependencies: From 0d5f22bab5617c0d1fbfc8c702f339c0a5be5cdf Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Mon, 21 Oct 2024 07:20:48 +0300 Subject: [PATCH 05/18] UHF-10259: Correct dir for languages --- ...d.field.paragraph.curated_event_list.field_all_events_link.yml | 0 .../fi/field.field.paragraph.curated_event_list.field_events.yml | 0 .../fi/paragraphs.paragraphs_type.curated_event_list.yml | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename modules/helfi_paragraphs_curated_event_list/config/optional/{languages => language}/fi/field.field.paragraph.curated_event_list.field_all_events_link.yml (100%) rename modules/helfi_paragraphs_curated_event_list/config/optional/{languages => language}/fi/field.field.paragraph.curated_event_list.field_events.yml (100%) rename modules/helfi_paragraphs_curated_event_list/config/optional/{languages => language}/fi/paragraphs.paragraphs_type.curated_event_list.yml (100%) diff --git a/modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/field.field.paragraph.curated_event_list.field_all_events_link.yml b/modules/helfi_paragraphs_curated_event_list/config/optional/language/fi/field.field.paragraph.curated_event_list.field_all_events_link.yml similarity index 100% rename from modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/field.field.paragraph.curated_event_list.field_all_events_link.yml rename to modules/helfi_paragraphs_curated_event_list/config/optional/language/fi/field.field.paragraph.curated_event_list.field_all_events_link.yml diff --git a/modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/field.field.paragraph.curated_event_list.field_events.yml b/modules/helfi_paragraphs_curated_event_list/config/optional/language/fi/field.field.paragraph.curated_event_list.field_events.yml similarity index 100% rename from modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/field.field.paragraph.curated_event_list.field_events.yml rename to modules/helfi_paragraphs_curated_event_list/config/optional/language/fi/field.field.paragraph.curated_event_list.field_events.yml diff --git a/modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/paragraphs.paragraphs_type.curated_event_list.yml b/modules/helfi_paragraphs_curated_event_list/config/optional/language/fi/paragraphs.paragraphs_type.curated_event_list.yml similarity index 100% rename from modules/helfi_paragraphs_curated_event_list/config/optional/languages/fi/paragraphs.paragraphs_type.curated_event_list.yml rename to modules/helfi_paragraphs_curated_event_list/config/optional/language/fi/paragraphs.paragraphs_type.curated_event_list.yml From d37fd5c4e2f71ce036d375c5ae57b9fcba899c42 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Mon, 21 Oct 2024 08:20:39 +0300 Subject: [PATCH 06/18] UHF-10259: Set persistent cache age to 0 --- ...xternal_entities.external_entity_type.linkedevents_event.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml b/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml index 3488a41ba..2e52c9f8b 100644 --- a/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml +++ b/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml @@ -35,7 +35,7 @@ field_mapper_config: value: '$.images[0].photographer_name' storage_client_id: linkedevents_events storage_client_config: null -persistent_cache_max_age: 10800 +persistent_cache_max_age: 0 annotation_entity_type_id: null annotation_bundle_id: null annotation_field_name: null From a2387f65697643e6eb522a9ef41d1d3ca4f36957 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Mon, 21 Oct 2024 09:56:01 +0300 Subject: [PATCH 07/18] UHF-10259: Move list generation logic to entity view hook --- ...helfi_paragraphs_curated_event_list.module | 59 +++++++++++++------ .../ExternalEntities/StorageClient/Events.php | 1 + 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module index 81cb525ee..d5eaa27b3 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module @@ -7,36 +7,59 @@ declare(strict_types=1); +use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\entity\BundleFieldDefinition; +use Drupal\external_entities\Entity\ExternalEntity; use Drupal\helfi_platform_config\DTO\ParagraphTypeCollection; +use Drupal\paragraphs\ParagraphInterface; /** - * Implements hook_preprocess_HOOK(). + * Implements hook hook_ENTITY_TYPE_view(). */ -function helfi_paragraphs_curated_event_list_preprocess_field__paragraph__field_events__curated_event_list( - array &$variables, +function helfi_paragraphs_curated_event_list_paragraph_view( + array &$build, + ParagraphInterface $entity, + EntityViewDisplayInterface $display, + string $view_mode, ): void { - $past_events = []; - $future_or_ongoing = []; - $time = time(); + if ($entity->bundle() !== 'curated_event_list') { + return; + } + + $selections = $entity->field_events->referencedEntities(); + + if ( + $display->getComponent('field_events') && + !empty($selections) + ) { + $storage = Drupal::entityTypeManager() + ->getStorage('linkedevents_event'); + + $ids = array_map(function (ExternalEntity $event) { + return $event->id(); + }, $selections); + + $entities = $storage->loadMultiple($ids); + $time = time(); + + foreach ($entities as $item) { + $item->addCacheableDependency($entity); + $end_timestamp = $item->end_time->date->getTimeStamp(); + $has_ended = $end_timestamp < $time; + + $build[$has_ended ? 'past_events' : 'future_or_ongoing'][] = Drupal::entityTypeManager() + ->getViewBuilder('linkedevents_event') + ->view($item); + } - foreach ($variables['items'] as $item) { - $event = $item['content']['#linkedevents_event']; - $end = $event->end_time->date; if ( - $end->getTimeStamp() < $time + isset($build['future_or_ongoing']) && + count($build['future_or_ongoing']) > 3 ) { - $past_events[] = $item; - } - else { - $future_or_ongoing[] = $item; + $build['future_or_ongoing'] = array_slice($build['future_or_ongoing'], 0, 3); } } - - $variables['past_events'] = $past_events; - // Always show a maximum if 3 events. - $variables['future_or_ongoing'] = array_slice($future_or_ongoing, 0, 3); } /** diff --git a/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php b/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php index a8199f337..529271a34 100644 --- a/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php +++ b/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php @@ -116,6 +116,7 @@ public function query( 'language' => $langcode, ]); } + // Enable searching directly with an event id elseif (preg_match('/.{0,15}:.{0,40}/i', $parameters[0]['value'])) { $endpoint = 'event'; $query = http_build_query( From 5abe75ab586d16fd2dba31923121baecc5a9bebd Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Mon, 21 Oct 2024 10:45:45 +0300 Subject: [PATCH 08/18] UHF-10259: Fix phpcs --- .../src/Plugin/ExternalEntities/StorageClient/Events.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php b/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php index 529271a34..e257a9ccf 100644 --- a/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php +++ b/modules/helfi_paragraphs_curated_event_list/src/Plugin/ExternalEntities/StorageClient/Events.php @@ -116,7 +116,7 @@ public function query( 'language' => $langcode, ]); } - // Enable searching directly with an event id + // Enable searching directly with an event id. elseif (preg_match('/.{0,15}:.{0,40}/i', $parameters[0]['value'])) { $endpoint = 'event'; $query = http_build_query( From 0f49ce8892bf3e87a72c05297e1ea19d5cfec15b Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Mon, 21 Oct 2024 10:59:51 +0300 Subject: [PATCH 09/18] UHF-10259: Remove unused field mappings --- ...l_entities.external_entity_type.linkedevents_event.yml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml b/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml index 2e52c9f8b..d0f57e1c3 100644 --- a/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml +++ b/modules/helfi_paragraphs_curated_event_list/config/install/external_entities.external_entity_type.linkedevents_event.yml @@ -19,7 +19,7 @@ field_mapper_config: value: $.title main_image: uri: '$.images[0].url' - title: '$.images[0].name' + title: '$.images[0].alt_text' start_time: value: $.start_time end_time: @@ -27,12 +27,6 @@ field_mapper_config: external_link: uri: $.external_link title: $.clean_title - main_image_title: - value: '$.images[0].name' - main_image_alt: - value: '$.images[0].alt_text' - photographer_name: - value: '$.images[0].photographer_name' storage_client_id: linkedevents_events storage_client_config: null persistent_cache_max_age: 0 From 55638673a87a26485bf3e3e4b0ac6b5c76796156 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Mon, 21 Oct 2024 11:04:02 +0300 Subject: [PATCH 10/18] UHF-10259: Fix module file comment --- .../helfi_paragraphs_curated_event_list.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module index d5eaa27b3..e8e79aa61 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module @@ -2,7 +2,7 @@ /** * @file - * Contains hooks for 'helfi_node_landing_page' module. + * Contains helfi_paragraphs_curated_event_list' module. */ declare(strict_types=1); From 1cb12d3f3fa8ce94da23525f247952bb6c1191c9 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Mon, 21 Oct 2024 11:13:49 +0300 Subject: [PATCH 11/18] UHF-10259: Phpstan compliance --- .../helfi_paragraphs_curated_event_list.module | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module index e8e79aa61..990b09f0b 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module @@ -40,6 +40,8 @@ function helfi_paragraphs_curated_event_list_paragraph_view( return $event->id(); }, $selections); + + /** @var \Drupal\external_entities\Entity\ExternalEntity[] */ $entities = $storage->loadMultiple($ids); $time = time(); From 7603be8c11c1c7ab3cca25dd6af07aab888e93f9 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Mon, 21 Oct 2024 11:20:37 +0300 Subject: [PATCH 12/18] UHF-10259: Fix phpcs --- .../helfi_paragraphs_curated_event_list.module | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module index 990b09f0b..534ba6e75 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module @@ -40,7 +40,6 @@ function helfi_paragraphs_curated_event_list_paragraph_view( return $event->id(); }, $selections); - /** @var \Drupal\external_entities\Entity\ExternalEntity[] */ $entities = $storage->loadMultiple($ids); $time = time(); From 9c14e55c8b69c89e40c4ef0e009250535b9f0c4e Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Mon, 21 Oct 2024 13:17:36 +0300 Subject: [PATCH 13/18] UHF-10259: Remove disable_field dependencies --- ...d.paragraph.curated_event_list.field_all_events_link.yml | 5 ----- ...ield.field.paragraph.curated_event_list.field_events.yml | 6 ------ .../helfi_paragraphs_curated_event_list.module | 4 +++- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_all_events_link.yml b/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_all_events_link.yml index 4900fc666..ee5a8ef6c 100644 --- a/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_all_events_link.yml +++ b/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_all_events_link.yml @@ -6,12 +6,7 @@ dependencies: - field.storage.paragraph.field_all_events_link - paragraphs.paragraphs_type.curated_event_list module: - - disable_field - link -third_party_settings: - disable_field: - add_disable: none - edit_disable: none id: paragraph.curated_event_list.field_all_events_link field_name: field_all_events_link entity_type: paragraph diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml b/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml index d0075879a..365f62ffb 100644 --- a/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml +++ b/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml @@ -5,12 +5,6 @@ dependencies: config: - field.storage.paragraph.field_events - paragraphs.paragraphs_type.curated_event_list - module: - - disable_field -third_party_settings: - disable_field: - add_disable: none - edit_disable: none id: paragraph.curated_event_list.field_events field_name: field_events entity_type: paragraph diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module index 534ba6e75..ee24567f8 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module @@ -46,7 +46,9 @@ function helfi_paragraphs_curated_event_list_paragraph_view( foreach ($entities as $item) { $item->addCacheableDependency($entity); - $end_timestamp = $item->end_time->date->getTimeStamp(); + /** @var \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem */ + $end_time = $item->get('end_time')->first(); + $end_timestamp = $end_time->date->getTimeStamp(); $has_ended = $end_timestamp < $time; $build[$has_ended ? 'past_events' : 'future_or_ongoing'][] = Drupal::entityTypeManager() From 6747ac8eaddeef05ed859277579d8162ff63b0e8 Mon Sep 17 00:00:00 2001 From: Tero Elonen Date: Mon, 21 Oct 2024 18:02:31 +0300 Subject: [PATCH 14/18] UHF-10259: Fix typo --- .../field.field.paragraph.curated_event_list.field_events.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml b/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml index 365f62ffb..5bac522d4 100644 --- a/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml +++ b/modules/helfi_paragraphs_curated_event_list/config/install/field.field.paragraph.curated_event_list.field_events.yml @@ -10,7 +10,7 @@ field_name: field_events entity_type: paragraph bundle: curated_event_list label: Events -description: "Pick events to display in the list. You may eihter search event by their name or enter their ID. \r\n

\r\nYou can find the ID of an event on tapahtumat.hel.fi site from the URL of the page. For example, in the url https://tapahtumat.hel.fi/fi/events/helsinki:agiy4m4ida the helsinki:agiy4m4ida part is the ID of the event.\r\n

\r\nThe page will display the top three events. You can arrange the events by dragging. Expired events will be automatically removed from the page." +description: "Pick events to display in the list. You may either search event by their name or enter their ID. \r\n

\r\nYou can find the ID of an event on tapahtumat.hel.fi site from the URL of the page. For example, in the url https://tapahtumat.hel.fi/fi/events/helsinki:agiy4m4ida the helsinki:agiy4m4ida part is the ID of the event.\r\n

\r\nThe page will display the top three events. You can arrange the events by dragging. Expired events will be automatically removed from the page." required: false translatable: false default_value: { } From 216bec96ed952ebdbbcb8a1bfd28ad3bda509bfe Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Tue, 22 Oct 2024 10:14:50 +0300 Subject: [PATCH 15/18] UHF-10259: Code style fixes --- ...lfi_paragraphs_curated_event_list.info.yml | 3 +- ...helfi_paragraphs_curated_event_list.module | 72 ++++++++++--------- .../src/Entity/LinkedEventsEvent.php | 32 +++++++++ 3 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.info.yml b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.info.yml index ec2a1b92b..08c00acd4 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.info.yml +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.info.yml @@ -2,8 +2,9 @@ name: 'HELfi Paragraphs: Curated Event List' type: module core_version_requirement: '^9 || ^10' dependencies: - - external_entities:external_entities - drupal:link + - drupal:field + - external_entities:external_entities - helfi_api_base:helfi_api_base - helfi_platform_config:helfi_platform_config - paragraphs:paragraphs diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module index ee24567f8..24a4f097f 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module @@ -2,7 +2,7 @@ /** * @file - * Contains helfi_paragraphs_curated_event_list' module. + * Contains helfi_paragraphs_curated_event_list module. */ declare(strict_types=1); @@ -10,7 +10,7 @@ declare(strict_types=1); use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\entity\BundleFieldDefinition; -use Drupal\external_entities\Entity\ExternalEntity; +use Drupal\helfi_paragraphs_curated_event_list\Entity\LinkedEventsEvent; use Drupal\helfi_platform_config\DTO\ParagraphTypeCollection; use Drupal\paragraphs\ParagraphInterface; @@ -27,41 +27,38 @@ function helfi_paragraphs_curated_event_list_paragraph_view( return; } - $selections = $entity->field_events->referencedEntities(); + $selections = $entity->get('field_events')->referencedEntities(); if ( - $display->getComponent('field_events') && - !empty($selections) + !$display->getComponent('field_events') || + empty($selections) ) { - $storage = Drupal::entityTypeManager() - ->getStorage('linkedevents_event'); - - $ids = array_map(function (ExternalEntity $event) { - return $event->id(); - }, $selections); - - /** @var \Drupal\external_entities\Entity\ExternalEntity[] */ - $entities = $storage->loadMultiple($ids); - $time = time(); - - foreach ($entities as $item) { - $item->addCacheableDependency($entity); - /** @var \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem */ - $end_time = $item->get('end_time')->first(); - $end_timestamp = $end_time->date->getTimeStamp(); - $has_ended = $end_timestamp < $time; - - $build[$has_ended ? 'past_events' : 'future_or_ongoing'][] = Drupal::entityTypeManager() - ->getViewBuilder('linkedevents_event') - ->view($item); - } + return; + } - if ( - isset($build['future_or_ongoing']) && - count($build['future_or_ongoing']) > 3 - ) { - $build['future_or_ongoing'] = array_slice($build['future_or_ongoing'], 0, 3); - } + $storage = Drupal::entityTypeManager() + ->getStorage('linkedevents_event'); + + $ids = array_map(function (LinkedEventsEvent $event) { + return $event->id(); + }, $selections); + + /** @var LinkedEventsEvent[] */ + $entities = $storage->loadMultiple($ids); + + foreach ($entities as $item) { + $item->addCacheableDependency($entity); + + $build[$item->hasEnded() ? 'past_events' : 'future_or_ongoing'][] = Drupal::entityTypeManager() + ->getViewBuilder('linkedevents_event') + ->view($item); + } + + if ( + isset($build['future_or_ongoing']) && + count($build['future_or_ongoing']) > 3 + ) { + $build['future_or_ongoing'] = array_slice($build['future_or_ongoing'], 0, 3); } } @@ -135,6 +132,15 @@ function helfi_paragraphs_curated_event_list_entity_bundle_field_info_alter( } } +/** + * Implements hook_entity_bundle_info_alter(). + */ +function helfi_paragraphs_curated_event_list_entity_bundle_info_alter(array &$bundles) : void { + if (isset($bundles['linkedevents_event']['linkedevents_event'])) { + $bundles['linkedevents_event']['linkedevents_event']['class'] = LinkedEventsEvent::class; + } +} + /** * Implements hook_helfi_paragraph_types(). */ diff --git a/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php b/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php new file mode 100644 index 000000000..3666ea3b7 --- /dev/null +++ b/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php @@ -0,0 +1,32 @@ +get('end_time')?->value; + + if (!$end_time) { + return false; + } + + $datetime = new DrupalDateTime($end_time); + return $datetime->format('U') < time(); + } + +} From e1de1a496dc43462dd4a28c99b0bd396fa77bb83 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Tue, 22 Oct 2024 11:01:25 +0300 Subject: [PATCH 16/18] UHF-10259: Use bundle class method to assert event has ended --- .../helfi_paragraphs_curated_event_list.module | 2 +- .../src/Entity/LinkedEventsEvent.php | 2 +- .../Validation/Constraint/CuratedEventConstraintValidator.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module index 24a4f097f..829cb39c7 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module @@ -43,7 +43,7 @@ function helfi_paragraphs_curated_event_list_paragraph_view( return $event->id(); }, $selections); - /** @var LinkedEventsEvent[] */ + /** @var \Drupal\helfi_paragraphs_curated_event_list\Entity\LinkedEventsEvent[] */ $entities = $storage->loadMultiple($ids); foreach ($entities as $item) { diff --git a/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php b/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php index 3666ea3b7..fbcd41211 100644 --- a/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php +++ b/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php @@ -22,7 +22,7 @@ public function hasEnded() : bool { $end_time = $this->get('end_time')?->value; if (!$end_time) { - return false; + return FALSE; } $datetime = new DrupalDateTime($end_time); diff --git a/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraintValidator.php b/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraintValidator.php index f374553b8..e3e555d7b 100644 --- a/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraintValidator.php +++ b/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraintValidator.php @@ -26,7 +26,7 @@ public function validate(mixed $value, Constraint $constraint) { } foreach ($events as $key => $event) { - if ($event->end_time->date->getTimeStamp() < time()) { + if ($event->hasEnded()) { $this->context->buildViolation($constraint->containsEndedEvents, ['%title' => $event->title->value]) ->atPath($key) ->addViolation(); From c6aa5cedcc2385525c4846f0c36c5a6a72e4491e Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Tue, 22 Oct 2024 11:04:20 +0300 Subject: [PATCH 17/18] UHF-10259: Remove bloat comment --- .../Validation/Constraint/CuratedEventConstraintValidator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraintValidator.php b/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraintValidator.php index e3e555d7b..98fea1714 100644 --- a/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraintValidator.php +++ b/modules/helfi_paragraphs_curated_event_list/src/Plugin/Validation/Constraint/CuratedEventConstraintValidator.php @@ -20,7 +20,6 @@ public function validate(mixed $value, Constraint $constraint) { $events = $value->referencedEntities(); - // Early return if no events are selected. if (empty($events)) { return; } From cd5e55bb42d3c9c099329ea28b1a5c286f538150 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Tue, 22 Oct 2024 11:34:49 +0300 Subject: [PATCH 18/18] UHF-10259: Store event types as const in bundle class --- .../helfi_paragraphs_curated_event_list.module | 2 +- .../src/Entity/LinkedEventsEvent.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module index 829cb39c7..93531f9c9 100644 --- a/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module +++ b/modules/helfi_paragraphs_curated_event_list/helfi_paragraphs_curated_event_list.module @@ -49,7 +49,7 @@ function helfi_paragraphs_curated_event_list_paragraph_view( foreach ($entities as $item) { $item->addCacheableDependency($entity); - $build[$item->hasEnded() ? 'past_events' : 'future_or_ongoing'][] = Drupal::entityTypeManager() + $build[$item->getEventType()][] = Drupal::entityTypeManager() ->getViewBuilder('linkedevents_event') ->view($item); } diff --git a/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php b/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php index fbcd41211..34ae83bbd 100644 --- a/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php +++ b/modules/helfi_paragraphs_curated_event_list/src/Entity/LinkedEventsEvent.php @@ -12,6 +12,9 @@ */ final class LinkedEventsEvent extends ExternalEntity { + public const FUTURE_OR_ONGOING = 'future_or_ongoing'; + public const PAST_EVENTS = 'past_events'; + /** * Check if event has ended and return result. * @@ -29,4 +32,14 @@ public function hasEnded() : bool { return $datetime->format('U') < time(); } + /** + * Returns event type. + * + * @return string + * Event type string. + */ + public function getEventType() : string { + return $this->hasEnded() ? self::PAST_EVENTS : self::FUTURE_OR_ONGOING; + } + }