Skip to content

Commit

Permalink
Merge pull request #21 from SoipoServices/Making-page-translatable
Browse files Browse the repository at this point in the history
  • Loading branch information
soipo committed Aug 10, 2023
2 parents 757e609 + fd22bfc commit a1f0894
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateCmsPagesTableAddUrlKey extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{

Schema::table('pages', function (Blueprint $table) {
$table->text('name')->after('title');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('pages', function (Blueprint $table) {
$table->dropColumn('name');
});
}
}
3 changes: 2 additions & 1 deletion src/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SoipoServices\Cms\Http\Controllers;

use App\Helpers\DomainHelper;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function contact(): Application|Factory|View
*/
public function show(Request $request, $slug): Application|Factory|View
{
$page = cache()->remember("cms.".$slug, now()->addMinutes(10), function () use($slug) {
$page = cache()->remember("cms.".$slug, now()->addMinutes(10), function () use ($slug) {
return static::getModelClassName(Resources::PAGE)::where('slug', $slug)->published()->firstOrFail();
});

Expand Down
20 changes: 10 additions & 10 deletions src/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Tags\HasTags;
use Spatie\Translatable\HasTranslations;

class Page extends Model implements HasMedia
{
use SoftDeletes, InteractsWithMedia, HasTags, HasFactory, Sluggable, Publishable, GetClass;
const TRANSLATABLE = ['summary', 'title', 'body'];

use SoftDeletes, InteractsWithMedia, HasTags, HasFactory, Sluggable, Publishable, GetClass, HasTranslations;

/**
* Fillable properties.
Expand All @@ -29,12 +32,15 @@ class Page extends Model implements HasMedia
protected $fillable = [
'author_id',
'title',
'name',
'summary',
'body',
'scheduled_at',
'slug',
'is_home'
];

public array $translatable = self::TRANSLATABLE;

/**
* Appended fields.
Expand All @@ -48,16 +54,10 @@ class Page extends Model implements HasMedia
*/
protected $casts = [
'scheduled_at' => 'datetime',
// 'summary' => 'json',
// 'title' => 'json',
// 'body' => 'json'
'summary' => 'json',
'title' => 'json',
'body' => 'json'
];


// const TRANSLATABLE = ['summary', 'title', 'body'];

// public array $translatable = self::TRANSLATABLE;

public function registerMediaConversions(Media $media = null): void
{
$this->addMediaConversion('thumb')
Expand Down
4 changes: 2 additions & 2 deletions src/Nova/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public function fields(Request $request)
])->rules(['required', 'string'])
->sortable(),

BelongsToMany::make('Pages', 'pages', static::getNovaClassName(Resources::PAGE)),
BelongsToMany::make('Links', 'links', static::getNovaClassName(Resources::LINK)),
BelongsToMany::make(__('Pages'), 'pages', static::getNovaClassName(Resources::PAGE)),
BelongsToMany::make(__('Links'), 'links', static::getNovaClassName(Resources::LINK)),
];
}

Expand Down
13 changes: 8 additions & 5 deletions src/Nova/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,19 @@ public function fields(Request $request)

Text::make(__('Title'), 'title')
->sortable()
->translatable()
->rules(['required']),

Text::make(__('Key to url path'), 'name'),

Boolean::make(__('Is Home'), 'is_home'),

Text::make(__('Slug'), 'slug')
->dependsOn(
['title'],
['name'],
function (Text $field, NovaRequest $request, FormData $formData) {
if ($formData->title != optional($request->resource()::find($request->resourceId))->title) {
$field->value = Str::slug($formData->title);
if ($formData->name != optional($request->resource()::find($request->resourceId))->name) {
$field->value = Str::slug($formData->name);
$field->help(route('pages.preview', ['slug' => $field->value]));
}
}
Expand All @@ -77,9 +80,9 @@ function (Text $field, NovaRequest $request, FormData $formData) {

BelongsTo::make(__('Author'), 'author', static::getNovaClassName(Resources::AUTHOR)),

Textarea::make(__('Summary'), 'summary')->hideFromIndex(),
Textarea::make(__('Summary'), 'summary')->hideFromIndex()->translatable(),

Trix::make(__('Body'), 'body')->withFiles('media')->rules(['required', 'string']),
Trix::make(__('Body'), 'body')->withFiles('media')->rules(['required', 'string'])->translatable(),

DateTime::make(__('Scheduled For'), 'scheduled_at')->rules('nullable'),

Expand Down

0 comments on commit a1f0894

Please sign in to comment.