/** * @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()]; }
public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('page_media_set', 'collection', ['label' => false, 'allow_delete' => true, 'allow_add' => false, 'type' => PageMediaAdminType::class, 'options' => ['media_types' => $this->mediaSetDefinition->getMediaSetDefinition($this->page), 'images_only' => false === $this->page instanceof HasRichMediaInterface], 'attr' => ['nested_form' => true]]); $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { /** @var Collection $data */ $data = $event->getData(); $mediaSet = $this->mediaSetDefinition->getMediaSetDefinition($this->page); $types = array_map(function ($type) use($data) { return $data->filter(function (PageMedia $element) use($type) { return $type === $element->getType(); })->first() ?: (new PageMedia())->setType($type)->setPageId($this->page->getId())->setPageType($this->page->getType()); }, array_combine($mediaSet, $mediaSet)); $data->clear(); foreach ($types as $key => $item) { $data->set($key, $item); } $event->setData(['page_media_set' => $data]); }); }
public function getPageMediaSet(HasMediaSetInterface $page) { return $this->findBy(['pageType' => $page->getType(), 'pageId' => $page->getId()]); }