Skip to content

Conversation

@karandatwani92
Copy link

@karandatwani92 karandatwani92 commented Jun 20, 2024

A user requested this feature so Devs can use $entry inside whenSaving closure to name files more logically.

For example, $entry->slug as file name:

CRUD::field('main_image')
->label('Main Image')
->type('image')
->withMedia([
  'whenSaving' => function($spatieMedia, $backpackMediaObject, $entry) {
    return $spatieMedia->usingFileName($entry->slug.'.jpg')->withResponsiveImages();
  }
]);

But one problem here:
I'm unable to figure out $index if used in subfield:

CRUD::field('gallery')
    ->type('repeatable')
    ->tab('Media')
    ->subfields([
        [
            'name' => 'image_title',
            'type' => 'text',
            'wrapper' => [
                'class' => 'form-group col-md-6',
            ],
        ],
        [
            'name' => 'gallery_image',
            'label' => 'image',
            'type' => 'image',
            'wrapper' => [
                'class' => 'form-group col-md-6',
            ],
            'withMedia' => [
                'whenSaving' => function ($spatieMedia, $backpackMediaObject, $entry) {
                    $index = 0; // <------HERE-----
                    $sub_slug = Str::slug($entry->gallery[$index]["image_title"]);
                    return $spatieMedia->usingFileName($sub_slug . '.jpg')->withResponsiveImages();
                }
            ],
        ]
    ]);

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants