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

Add translatedFieldsByLocale property #38

Merged
merged 1 commit into from
Mar 18, 2020
Merged

Conversation

dduupp
Copy link
Contributor

@dduupp dduupp commented Mar 17, 2020

Trying to build a custom UI around translatable fields proved to be quite the challenge for me and my team. We want to have tabs, but it's near impossible to sort the data property of the Translatable class by locale. So in this PR, I've added a translatedFieldsByLocale property to simplify this process and offer more flexibility in terms of other UI solutions.

Here's an example of how we've implemented this solution with tabs in a custom mail template create view:

use Eminiarts\Tabs\Tabs;
use Eminiarts\Tabs\TabsOnEdit;
use NumaxLab\NovaCKEditor5Classic\CKEditor5Classic;
use Spatie\NovaTranslatable\Translatable;

class MailTemplate extends Resource
{
    use TabsOnEdit;

    ...
    public function fields(Request $request)
    {

        $translatedFields = Translatable::make($this->translatableFields());

        return [

            new Panel('', [

                ID::make()->sortable(),

                Select::make('mail_type')
                    ->options([])
                    ->exceptOnForms(),

                Text::make('name')
                    ->sortable()
                    ->rules('required', 'max:255'),

                Code::make('recipients')->json(),
                Code::make('cc')->json(),
                Code::make('bcc')->json(),

                Select::make('design')->options([]),
            ]),

            new Tabs('', $translatedFields->translatedFieldsByLocale)
        ];
    }

    protected function translatableFields() {

        return [
            Text::make('sender_name'),
            Text::make('sender_email'),
            Text::make('subject'),
            CKEditor5Classic::make('body'),
            Text::make('attachments'),
        ];
    }
}

The result looks something like this:

Screenshot 2020-03-17 at 18 27 27

@freekmurze freekmurze merged commit b1dd742 into spatie:master Mar 18, 2020
@freekmurze
Copy link
Member

Thanks!

@jorenvh
Copy link

jorenvh commented Oct 14, 2020

@dduupp thanks for this, works great for create/edit views. However the example breaks the resource overview page. Did you encounter this too and how did you solve this on your end? I can't seem to find a solution for this.

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

Successfully merging this pull request may close these issues.

3 participants