public function getPageMedia(HasMediaSetInterface $page, $name = null)
 {
     if (null === $name) {
         list($name) = $this->mediaSetDefinition->getMediaSetDefinition($page);
     }
     $mediaSet = $this->getPageMediaSet($page);
     if (false === isset($mediaSet[$name])) {
         return null;
     }
     return $mediaSet[$name];
 }
 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]);
     });
 }