whitecube / nova-page

Static pages content management for Laravel Nova
https://whitecube.github.io/nova-page
MIT License
238 stars 41 forks source link

Attaching Media form spate media library #42

Open OliverZiegler opened 5 years ago

OliverZiegler commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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; }


3. Go back to nova-page's configuration and set `sources.database.model` to your new `Page` model (or whatever you named it).
OliverZiegler commented 5 years ago

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

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 commented 5 years ago

Hey guys,

When running:

Pressing save won't do anything 😕

Update

My fault, ignore me :)

amidesfahani commented 3 years ago

does it world?