sulu / sulu-docs

Sulu documentation
https://docs.sulu.io
22 stars 118 forks source link

DocumentManager/Fixtures add note about how to add media content to a document. #309

Open oafx opened 7 years ago

oafx commented 7 years ago

Hi, (me again, the fixture guy)

i'd like to suggest an extension of the DocumentManager/Fixtures documentation regarding the usage of media content (via the media id?) in fixtures. Being new to sulu it was quite a journey for me to figure out what i had to do, well i'm still not sure if i got it all right with the mediamanager now as it seems i can't crop my fixture images but i guess that's OT/MediaManager related.

Regards, Chris

danrot commented 7 years ago

What exactly did you do? I am not sure if the document manager is made for handling media stuff until now. Can you show your progress, and how you made that work?

oafx commented 7 years ago

@danrot Well ist kind of a bigger process to put everything together.. And i'm sure i'm not doing everything by convention. My motivation was to create content/pages that were not just filled with text but also images that are included in the media library because of various reasons. I'm using kind of a 'review apps' workflow for development which rebuilds the whole app from scratch inside of fresh docker containers so it's always nice to have some basic content that is integrated as starting point.

So it seemed i had to create an ORM Fixture that loads the media with a set $mediaType and $collection. I found that thesulu/SuluDeveloperBundle was (although seemingly deprecated/not longer maintained?) a good starting point for that.

[..]
class LoadMediaData implements FixtureInterface, OrderedFixtureInterface, ContainerAwareInterface
{
[..]
    public function load(ObjectManager $manager)
    {
        $imageDir = realpath(__DIR__ . '/../../Resources/data/media');
        $finder = new Finder();
        $finder->in($imageDir)->name('/\.jpg$|\.png$/');

        foreach ($finder as $file) {
            echo $file->getBasename();
            $uploadedFile = new UploadedFile($file->getPathname(), $file->getPathname(), null, null, null, true);
            $data = array(
                'id' => null,
                'locale' => 'de',
                'type' => $mediaType->getId(),
                'collection' => $collection->getId(),
                'name' => $file->getBasename(),
                'title' => substr($file->getBasename(), 0, -(strlen($file->getExtension()) + 1)),
            );
            $this->container->get('sulu_media.media_manager')->save($uploadedFile, $data, 1);
        }
    }
[..]
}

since the ORM fixtures are loaded previous to the Document fixtures in the sulu:build commands i now had just to 'search' for the right media to add into my documents eg.:

[..]
class PageFixture implements DocumentFixtureInterface, ContainerAwareInterface
{
[..]
    public function load(DocumentManager $documentManager)
    {
        [..]
        $imageMedia = $this->container->get('sulu_media.media_manager')->get($locale, ['search'=>'headerimageXY']);
        $mediaIds = array();

        foreach($imageMedia as $key => $image) {
            array_push($mediaIds, $image->getFile()->getId()); 
        }

after examining the ajax requests when adding media to a document via the admin interface and a lot of trail and error i figured out that i could add the media to the corresponding document property in the following manner (this is what i'd suggest to add to the fixtures documentation somehow):

//still inside public function load(DocumentManager $documentManager)
        $document = $documentManager->create('page');

        /** @var \Sulu\Bundle\ContentBundle\Document\PageDocument $document */
        $document = $documentManager->create('page');
        $document->setLocale($locale);
        $document->setTitle('foo bar page');
        $document->setStructureType('default');
        $document->setResourceSegment('/foo-bar-page');
        $document->setWorkflowStage(WorkflowStage::PUBLISHED);
        $document->getStructure()->bind(array(
            'title' => 'foo bar page',
            //adding the images:
            'headerimage' => array('displayOption' => 'top','ids' => $mediaIds)
        ));

        $document->setExtension(
            'excerpt',
            [
                'title' => 'foo title',
                'description' => 'bar description',
                'categories' => [],
                'tags' => []
            ]
        );

        $documentManager->persist($document, 'en', array(
            'parent_path' => '/cmf/sulu_io/contents',
        ));
        $documentManager->flush();
    }
[..]
}

I hope that explains it a bit better.

*edit: fixed some typos etc.

danrot commented 7 years ago

@oafx What you are explaining here sounds reasonable to me... If you create a PR in sulu-docs explaining all of that I would be happy to merge that 😃

And yeah, we would love to have a proper version of the SuluDeveloperBundle, especially for testing perfomance stuff. We started it, but never gave it enough attention to have a real good state...

oafx commented 7 years ago

@danrot i'm not sure about the scope of the changes in a PR since the topics are kind of overlapping (afaik there is no explaination of ORM fixtures and the usage of the MediaManager in the docs?) otherwise i'd be happy to help/contribute.

danrot commented 7 years ago

Hm, good question... Can we maybe link to the doctrine migrations documentation, and explain the usage of the MediaManager under Bundles/MediaBundle?