Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attaching Media form spate media library #42

Open
OliverZiegler opened this issue Aug 23, 2019 · 5 comments
Open

Attaching Media form spate media library #42

OliverZiegler opened this issue Aug 23, 2019 · 5 comments

Comments

@OliverZiegler
Copy link

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 🤔

@toonvandenbos
Copy link
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.

toonvandenbos added a commit that referenced this issue Aug 23, 2019
…or nova-flexible-content (HasMediaLibrary issue) compatibility. References #42
@toonvandenbos
Copy link
Member

toonvandenbos commented Aug 23, 2019

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).

@OliverZiegler
Copy link
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();
    }

@GarethSomers
Copy link
Contributor

GarethSomers commented Sep 3, 2019

Hey guys,

When running:

Pressing save won't do anything 😕

Update

My fault, ignore me :)

@amidesfahani
Copy link

does it world?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants