src/Controller/Api/GalleryController.php
protected function handleWriteGalleryhasmedia(GalleryInterface $gallery, MediaInterface $media, GalleryHasMediaInterface $galleryHasMedia = null, Request $request)
    {
        $form = $this->formFactory->createNamed(null, 'sonata_media_api_form_gallery_has_media', $galleryHasMedia, [
            'csrf_protection' => false,
        ]);

        $form->handleRequest($request);

        if ($form->isValid()) {
            $galleryHasMedia = $form->getData();
            $galleryHasMedia->setMedia($media);

            // NEXT_MAJOR: remove this if/else block. Just call `$gallery->addGalleryHasMedia($galleryHasMedia);`
            if ($gallery instanceof GalleryMediaCollectionInterface) {
                $gallery->addGalleryHasMedia($galleryHasMedia);
            } else {
                $gallery->addGalleryHasMedias($galleryHasMedia);
            }
            $this->galleryManager->save($gallery);

            $context = new Context();
            $context->setGroups(['sonata_api_read']);
            $context->enableMaxDepth();

            $view = FOSRestView::create($galleryHasMedia);
            $view->setContext($context);

            return $view;
        }

        return $form;
    }
protected function handleWriteGalleryhasmedia(GalleryInterface $gallery, MediaInterface $media, GalleryHasMediaInterface $galleryHasMedia = null, Request $request)
    {
        $form = $this->formFactory->createNamed(null, 'sonata_media_api_form_gallery_has_media', $galleryHasMedia, [
            'csrf_protection' => false,
        ]);

        $form->handleRequest($request);

        if ($form->isValid()) {
            $galleryHasMedia = $form->getData();
            $galleryHasMedia->setMedia($media);

            // NEXT_MAJOR: remove this if/else block. Just call `$gallery->addGalleryHasMedia($galleryHasMedia);`
            if ($gallery instanceof GalleryMediaCollectionInterface) {
                $gallery->addGalleryHasMedia($galleryHasMedia);
            } else {
                $gallery->addGalleryHasMedias($galleryHasMedia);
            }
            $this->galleryManager->save($gallery);

            $context = new Context();
            $context->setGroups(['sonata_api_read']);
            $context->enableMaxDepth();

            $view = FOSRestView::create($galleryHasMedia);
            $view->setContext($context);

            return $view;
        }

        return $form;
    }
protected function handleWriteGalleryhasmedia ( GalleryInterface $ gallery , MediaInterface $ media , GalleryHasMediaInterface $ galleryHasMedia = null , Request $ request ) { $ form = $ this -> formFactory -> createNamed ( null , 'sonata_media_api_form_gallery_has_media' , $ galleryHasMedia , [ 'csrf_protection' => false , ] ) ; $ form -> handleRequest ( $ request ) ; if ( $ form -> isValid ( ) ) { $ galleryHasMedia = $ form -> getData ( ) ; $ galleryHasMedia -> setMedia ( $ media ) ; // NEXT_MAJOR: remove this if/else block. Just call `$gallery->addGalleryHasMedia($galleryHasMedia);` if ( $ gallery instanceof GalleryMediaCollectionInterface ) { $ gallery -> addGalleryHasMedia ( $ galleryHasMedia ) ; } else { $ gallery -> addGalleryHasMedias ( $ galleryHasMedia ) ; } $ this -> galleryManager -> save ( $ gallery ) ; $ context = new Context ( ) ; $ context -> setGroups ( [ 'sonata_api_read' ] ) ; $ context -> enableMaxDepth ( ) ; $ view = FOSRestView :: create ( $ galleryHasMedia ) ; $ view -> setContext ( $ context ) ; return $ view ; } return $ form ; }
Write a GalleryHasMedia, this method is used by both POST and PUT action methods.

@param GalleryInterface         $gallery
@param MediaInterface           $media
@param GalleryHasMediaInterface $galleryHasMedia
@param Request                  $request

@return FormInterface
Write a GalleryHasMedia this method is used by both POST and PUT action methods .