Skip to content

Commit

Permalink
Add event_selection and single_event_selection content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasnatter committed Feb 3, 2021
1 parent 2f49214 commit fb2f9c3
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 2 deletions.
35 changes: 35 additions & 0 deletions config/packages/app_event_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
6 changes: 6 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' } ]
53 changes: 53 additions & 0 deletions src/Content/Type/EventSelection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace App\Content\Type;

use App\Entity\Event;
use Doctrine\ORM\EntityManagerInterface;
use Sulu\Component\Content\Compat\PropertyInterface;
use Sulu\Component\Content\SimpleContentType;

class EventSelection extends SimpleContentType
{
protected EntityManagerInterface $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->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<string, array<int>|null>
*/
public function getViewData(PropertyInterface $property): array
{
return [
'ids' => $property->getValue(),
];
}
}
43 changes: 43 additions & 0 deletions src/Content/Type/SingleEventSelection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace App\Content\Type;

use App\Entity\Event;
use Doctrine\ORM\EntityManagerInterface;
use Sulu\Component\Content\Compat\PropertyInterface;
use Sulu\Component\Content\SimpleContentType;

class SingleEventSelection extends SimpleContentType
{
protected EntityManagerInterface $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->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<string, int|null>
*/
public function getViewData(PropertyInterface $property): array
{
return [
'id' => $property->getValue(),
];
}
}
6 changes: 5 additions & 1 deletion translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
6 changes: 5 additions & 1 deletion translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit fb2f9c3

Please sign in to comment.