/** * @param AdaptFormEvent $event */ public function adaptForm(AdaptFormEvent $event) { /** @var HasMediaSetInterface $page */ $page = $event->getPage(); if (false === $page instanceof HasMediaSetInterface) { return; } $mediaSet = $this->repository->getPageMediaSet($page); $type = new PageMediaCollectionAdminType($page, $this->mediaSetDefinition); $mediaWidget = new FormWidget($type, new ArrayCollection($mediaSet)); $event->getTabPane()->addTab(new Tab('Media Set', $mediaWidget)); }
/** * @param HasMediaSetInterface $page * * @return PageMedia[] */ private function getPageMediaSet(HasMediaSetInterface $page) { if (false === $this->preloadedMediaSets) { $data = $this->repository->createQueryBuilder('pm')->join(Media::class, 'media', Join::WITH, 'pm.media = media.id')->select(['pm.pageType', 'pm.pageId', 'pm.type', 'media.url'])->getQuery()->getResult(); $this->preloadedMediaSets = true; foreach ($data as $media) { $this->mediaSets[$media['pageType']][$media['pageId']][$media['type']] = $media['url']; } } if (false === isset($this->mediaSets[$page->getType()][$page->getId()])) { return []; } return $this->mediaSets[$page->getType()][$page->getId()]; }