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

Pass $entry to whenSaving Closure #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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: Needs Testing, Review or Docs
Development

Successfully merging this pull request may close these issues.

2 participants