Skip to content

Attaching Media form spate media library #42

Open

Description

@OliverZiegler

We are currently using this package, and in combination with nova-flexible-content this is a winner 🎉

As nova-flexible-content now resolved the issue with advanced-nova-media-library field, it would be great to use this also with nova-page.

In the issue you state:

Spatie's MediaLibrary\HasMedia\HasMedia interface & MediaLibrary\HasMedia\HasMediaTrait should both be implemented on the resource model (not on the Flexible layout!) ;

Is there any idea how to get this working?
Would be nice to define the media collections in the template files and use advanced-nova-media-library field 🤔

Activity

  1. toonvandenbos commented on Aug 23, 2019

    @toonvandenbos
    Member

    Hi @OliverZiegler,

    The problem here is that Spatie's library requires a model in order to link the media to it. By default, this package does not use models (data is stored in JSON files). Therefore, you should switch to database storage. This will however probably not work with the current version of nova-flexible-content, since the model is difficult to fetch from inside a Flexible content.

    I'm working on a fix for a similar issue (the one you linked in your post) as we speak, I'll try to find a solution that also fixes this one.

  2. toonvandenbos commented on Aug 23, 2019

    @toonvandenbos
    Member

    Hi @OliverZiegler,

    Could you update both packages (using dev-master for nova-page) and tell me if this issue is fixed?

    Here's how I got it working (you should probably re-publish the package's configuration file):

    1. Switch Nova-Page's "default_source" configuration to Database ;
    2. Create a new model like this:
    namespace App;
    
    use Whitecube\NovaPage\Sources\StaticPage;
    use Spatie\MediaLibrary\HasMedia\HasMedia;
    use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
    
    class Page extends StaticPage implements HasMedia
    {
        use HasMediaTrait;
    }
    1. Go back to nova-page's configuration and set sources.database.model to your new Page model (or whatever you named it).
  3. OliverZiegler commented on Aug 26, 2019

    @OliverZiegler
    Author

    Thanks for the fast response!!

    So with the still missing PR from advanced-nova-media-library I got it working with nova-flexible-content but with some limitations...

    • Adding the same Layout multiple times overwrites the images (related to this issue I think.)
    • Defining single file collections won't work (also related to this issue

    One more issue (not related to nova-flexible-content) is adding Images to the page directly.
    Defining advanced-nova-medialibrary fields on the Template worked for me (inspired by your trait in nova-flexible-content) by adding the following code to the template:

    Make the template implement use Spatie\MediaLibrary\HasMedia\HasMedia; and adding the trait with use Spatie\MediaLibrary\HasMedia\HasMediaTrait;.

    Furthermore adding:

        protected function getMediaModel() : HasMedia
        {
            $model = $this->getSource()->getOriginal($this);
    
            \Log::info($model);
    
            if(is_null($model) || !($model instanceof HasMedia)) {
                throw new \Exception('Origin HasMedia model not found.');
            }
    
            return $model;
        }
    
    
        public function getMedia(string $collectionName = 'default', $filters = [])
        {
            return app(MediaRepository::class)->getCollection($this->getMediaModel(), $collectionName, $filters);
        }
    
        public function addMedia($file)
        {
            return app(FileAdderFactory::class)
                ->create($this->getMediaModel(), $file)
                ->preservingOriginal();
        }
  4. GarethSomers commented on Sep 3, 2019

    @GarethSomers
    Contributor

    Hey guys,

    When running:

    Pressing save won't do anything 😕

    Update

    My fault, ignore me :)

  5. amidesfahani commented on Oct 11, 2020

    @amidesfahani

    does it world?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions