0

Following by Sulu documentation, I try to add selection field type to display collection type resource (Room object) in admin form. Currently, I can select these elements from list,

select element

selected element

but after form submit and reload any record cannot be shown, despite that the Room object exist in Event entity:

empty list

What I do wrong?

Code:

sulu_admin.yaml

sulu_admin:
...

# Registering Selection Field Types in this section
field_type_options:
    selection:
        room_selection:
            default_type: list_overlay
            resource_key: rooms
            types:
                list_overlay:
                    adapter: table
                    list_key: rooms
                    display_properties:
                        - name
                    icon: su-clock
                    label: 'app.rooms'
                    overlay_title: 'app.rooms'

event_details.xml

        <property name="rooms" type="room_selection">
             <meta>
                 <title>app.rooms</title>
             </meta>
        </property>
papec.23
  • 3
  • 2

1 Answers1

0

Your api response should just contain a flat list of room ids for rooms. Assuming you have an Event entity with a $rooms property, add a public function getRoomIds() {} to your Event entity and add the @VirtualProperty("rooms") annotation to that method. Probably you have to add the @Exclude annotation to public function getRooms() {}

EDIT:

I just tried it, if you have an entity like the following, it works as expected. If it's still not working, maybe you have a problem with your jms serializer config.

/**
 * @Serializer\ExclusionPolicy("all")
 */
class Event
{
    /**
     * @Serializer\Expose()
     */
    private $otherProperty;

    /**
     * @var Collection<int, Room>
     */
    private $rooms;

    public function getRooms(): Collection
    {
        return $this->rooms;
    }

    /**
     * @Serializer\VirtualProperty()
     * @Serializer\SerializedName("rooms")
     */
    public function getRoomIds(): array
    {
        return $this->rooms->map(function (Room $room) {
            return $room->getId();
        });
    }
}
Luca Rath-Heel
  • 210
  • 1
  • 8
  • Following your instructions my entity's code looks like: ` /** * @Serializer\Exclude() * @return Collection|Room[] */ public function getRooms():? Collection { return $this->rooms; } /** * @VirtualProperty("rooms") */ public function getRoomIds(): array { $list = []; foreach ($this->rooms as $room) { $list[] = $room->getId(); } return $list; } ` but it still not working. – papec.23 Jul 08 '20 at 12:22
  • What does the api response for `rooms` look like now? – Luca Rath-Heel Jul 09 '20 at 07:40
  • No i meant this: https://i.stack.imgur.com/1O3MV.jpg ... Did this api response change after you changed your code? Also, do you get any javascript error in your console? – Luca Rath-Heel Jul 09 '20 at 13:02