PHP code example of ewk / moonshine-media

1. Go to this page and download the library: Download ewk/moonshine-media library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ewk / moonshine-media example snippets


use Ewk\Cms\Media\MoonShine\Pages\MediaLibraryPage;
use MoonShine\MenuManager\MenuItem;

MenuItem::make(MediaLibraryPage::class, static fn (): string => __('moonshine-media::library.title'), 'photo'),

$page = MediaQuery::make()
    ->search('afisha')
    ->inFolder($folderId)          // or ->inFolder(null, rootOnly: true)
    ->ofKind([FileKind::IMAGE, FileKind::VECTOR])
    ->withoutAlt('ru')
    ->unused()
    ->ownedBy('organizer', 7)
    ->sort(MediaSort::NEWEST)      // NEWEST, NAME, SIZE_DESC
    ->limit(60)                    // 1 to 200, 60 by default
    ->after($cursor)
    ->get();                       // MediaPage { items, nextCursor }

$this->app->extend(UsageLabelRegistry::class, static function (UsageLabelRegistry $registry): UsageLabelRegistry {
    $registry->register(new PageUsageLabelResolver());

    return $registry;
});

final class Tour extends Model
{
    use HasMediaAttachments;
}

$tour->attachedMedia('gallery');            // Collection<CmsMedia>, by position
$tour->attachedMedia('cover', 'ru');        // the Russian cover
$tour->firstAttachedMedia('cover', 'ru');   // ?CmsMedia
$tour->attachedMediaByLocale('cover');      // ['ru' => …, 'en' => …]
Tour::query()->withAttachedMedia(['cover'])->get(); // four queries for any number of tours

MediaPicker::make('Логотип', 'logo')->kinds(['image', 'vector']);
MediaPicker::make('Картинка для соцсетей', 'og_image')->withoutDescription();

use Ewk\Cms\Media\MoonShine\Concerns\InteractsWithMediaAttachments;
use Ewk\Cms\Media\MoonShine\Fields\MediaAttachmentsField;

final class VenueResource extends ModelResource
{
    use InteractsWithMediaAttachments;
}

// the form page
MediaAttachmentsField::make('Логотип', 'logo'),
MediaAttachmentsField::make('Галерея', 'gallery')->multiple()->max(6),
MediaAttachmentsField::make('Обложка', 'cover')->locale('ru')->kinds(['image']),

// its rules
...$this->getResource()->mediaAttachmentRules('cover', 'ru', 

use Ewk\Cms\Media\MoonShine\Fields\AttachmentPreview;

AttachmentPreview::make('Площадка', 'logo')->withTitle('name'),
AttachmentPreview::make('Тур', 'cover', fn (): string => app()->getLocale())->withTitle('name'),

use Ewk\Cms\Media\MoonShine\Library\UsageLinkRegistry;

$this->app->extend(UsageLinkRegistry::class, static fn (UsageLinkRegistry $links): UsageLinkRegistry => $links
    ->resource(Page::MORPH_ALIAS, PageResource::class)
    // A closure is asked when a link is built, for a resource that is configurable.
    ->resource(Header::MORPH_ALIAS, static fn (): string => config('my-cms.header_resource'))
    // A translation key, or a closure that answers with the caption.
    ->caption('page', 'my-cms::usages.page'));