From fb2f9c39d2617596209d6c68dfc639b68edd92d2 Mon Sep 17 00:00:00 2001 From: Niklas Natter Date: Mon, 9 Nov 2020 14:29:01 +0100 Subject: [PATCH] Add event_selection and single_event_selection content-type --- config/packages/app_event_admin.yaml | 35 +++++++++++++++ config/services.yaml | 6 +++ src/Content/Type/EventSelection.php | 53 +++++++++++++++++++++++ src/Content/Type/SingleEventSelection.php | 43 ++++++++++++++++++ translations/admin.de.json | 6 ++- translations/admin.en.json | 6 ++- 6 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 src/Content/Type/EventSelection.php create mode 100644 src/Content/Type/SingleEventSelection.php diff --git a/config/packages/app_event_admin.yaml b/config/packages/app_event_admin.yaml index 3e0a60c1..b165550f 100644 --- a/config/packages/app_event_admin.yaml +++ b/config/packages/app_event_admin.yaml @@ -4,3 +4,38 @@ sulu_admin: routes: list: 'app.get_events' detail: 'app.get_event' + field_type_options: + selection: + event_selection: + default_type: 'list_overlay' + resource_key: 'events' + view: + name: 'app.event.add_form' + result_to_view: + id: 'id' + types: + list_overlay: + adapter: 'table' + list_key: 'events' + display_properties: + - 'name' + icon: 'su-calendar' + label: 'app.event_selection_label' + overlay_title: 'app.select_events' + single_selection: + single_event_selection: + default_type: 'list_overlay' + resource_key: 'events' + view: + name: 'app.event.add_form' + result_to_view: + id: 'id' + types: + list_overlay: + adapter: 'table' + list_key: 'events' + display_properties: + - 'name' + icon: 'su-calendar' + empty_text: 'app.no_event_selected' + overlay_title: 'app.select_event' diff --git a/config/services.yaml b/config/services.yaml index bd73083f..055c0297 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -47,3 +47,9 @@ services: App\Content\Type\AlbumSelection: tags: [{name: 'sulu.content.type', alias: 'album_selection'}] + + App\Content\Type\SingleEventSelection: + tags: [ { name: 'sulu.content.type', alias: 'single_event_selection' } ] + + App\Content\Type\EventSelection: + tags: [ { name: 'sulu.content.type', alias: 'event_selection' } ] diff --git a/src/Content/Type/EventSelection.php b/src/Content/Type/EventSelection.php new file mode 100644 index 00000000..8595c20c --- /dev/null +++ b/src/Content/Type/EventSelection.php @@ -0,0 +1,53 @@ +entityManager = $entityManager; + + parent::__construct('event_selection', []); + } + + /** + * @return Event[] + */ + public function getContentData(PropertyInterface $property): array + { + $ids = $property->getValue(); + + if (empty($ids)) { + return []; + } + + $events = $this->entityManager->getRepository(Event::class)->findBy(['id' => $ids]); + + $idPositions = array_flip($ids); + usort($events, function (Event $a, Event $b) use ($idPositions) { + return $idPositions[$a->getId()] - $idPositions[$b->getId()]; + }); + + return $events; + } + + /** + * @return array|null> + */ + public function getViewData(PropertyInterface $property): array + { + return [ + 'ids' => $property->getValue(), + ]; + } +} diff --git a/src/Content/Type/SingleEventSelection.php b/src/Content/Type/SingleEventSelection.php new file mode 100644 index 00000000..0eb2ba92 --- /dev/null +++ b/src/Content/Type/SingleEventSelection.php @@ -0,0 +1,43 @@ +entityManager = $entityManager; + + parent::__construct('single_event_selection', null); + } + + public function getContentData(PropertyInterface $property): ?Event + { + $id = $property->getValue(); + + if (empty($id)) { + return null; + } + + return $this->entityManager->getRepository(Event::class)->find($id); + } + + /** + * @return array + */ + public function getViewData(PropertyInterface $property): array + { + return [ + 'id' => $property->getValue(), + ]; + } +} diff --git a/translations/admin.de.json b/translations/admin.de.json index f012133c..a1aa91e5 100644 --- a/translations/admin.de.json +++ b/translations/admin.de.json @@ -13,5 +13,9 @@ "app.select_album": "Album auswählen", "app.events": "Events", "app.start_date": "Startdatum", - "app.end_date": "Enddatum" + "app.end_date": "Enddatum", + "app.event_selection_label": "{count} {count, plural, =1 {Event} other {Events}} ausgewählt", + "app.select_events": "Events auswählen", + "app.no_event_selected": "Kein Event ausgewählt", + "app.select_event": "Event auswählen" } diff --git a/translations/admin.en.json b/translations/admin.en.json index 109345a2..ecc7c0b5 100644 --- a/translations/admin.en.json +++ b/translations/admin.en.json @@ -13,5 +13,9 @@ "app.select_album": "Select album", "app.events": "Events", "app.start_date": "Start Date", - "app.end_date": "End Date" + "app.end_date": "End Date", + "app.event_selection_label": "{count} {count, plural, =1 {event} other {events}} selected", + "app.select_events": "Select events", + "app.no_event_selected": "No event selected", + "app.select_event": "Select event" }