Skip to content

Commit

Permalink
Config for NovaResourceHasMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgilles committed Jan 17, 2024
1 parent 6f98c27 commit fda7dd1
Showing 1 changed file with 43 additions and 25 deletions.
68 changes: 43 additions & 25 deletions src/Traits/NovaResourceHasMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Novius\LaravelMeta\Traits;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Laravel\Nova\Fields\Badge;
use Laravel\Nova\Fields\Image;
use Laravel\Nova\Fields\Select;
Expand All @@ -13,7 +14,7 @@

trait NovaResourceHasMeta
{
public function getSEONovaFields(): array
public function getSEONovaFields(array $config = []): array
{
/** @var Model&HasMeta $model */
$model = static::newModel();
Expand All @@ -25,8 +26,9 @@ public function getSEONovaFields(): array
$options = array_keys(IndexFollow::cases());

/** @var resource $this */
return [
Badge::make(trans('laravel-meta::messages.badge'), function () {
$fields = [];
if (Arr::get($config, 'badge', true)) {
$fields[] = Badge::make(trans('laravel-meta::messages.badge'), function () {
/** @var resource $this */
if ($this->resource->seo_title && $this->resource->seo_description) {
return '100%';
Expand All @@ -40,28 +42,44 @@ public function getSEONovaFields(): array
'0%' => 'danger',
'50%' => 'warning',
'100%' => 'success',
])->onlyOnIndex(),
Text::make(trans('laravel-meta::messages.seo_title'), $columnMeta.'.seo_title')
->rules('nullable', 'string')
->hideFromIndex(),
Textarea::make(trans('laravel-meta::messages.seo_description'), $columnMeta.'.seo_description')
->rules('nullable', 'string')
->hideFromIndex(),
Textarea::make(trans('laravel-meta::messages.seo_keywords'), $columnMeta.'.seo_keywords')
->rules('nullable', 'string')
->hideFromIndex(),
Select::make(trans('laravel-meta::messages.seo_robots'), $columnMeta.'.seo_robots')
->rules('required')
])->onlyOnIndex();
}
if (Arr::get($config, 'seo_title', true)) {
$fields[] = Text::make(trans('laravel-meta::messages.seo_title'), $columnMeta.'.seo_title')
->rules(Arr::get($config, 'required.seo_title', false) ? 'required' : 'nullable', 'string')
->hideFromIndex();
}
if (Arr::get($config, 'seo_description', true)) {
$fields[] = Textarea::make(trans('laravel-meta::messages.seo_description'), $columnMeta.'.seo_description')
->rules(Arr::get($config, 'required.seo_description', false) ? 'required' : 'nullable', 'string')
->hideFromIndex();
}
if (Arr::get($config, 'seo_keywords', true)) {
$fields[] = Textarea::make(trans('laravel-meta::messages.seo_keywords'), $columnMeta.'.seo_keywords')
->rules(Arr::get($config, 'required.seo_keywords', false) ? 'required' : 'nullable', 'string')
->hideFromIndex();
}
if (Arr::get($config, 'seo_robots', true)) {
$fields[] = Select::make(trans('laravel-meta::messages.seo_robots'), $columnMeta.'.seo_robots')
->rules(Arr::get($config, 'required.seo_robots', false) ? 'required' : 'nullable')
->options(array_combine($options, $options))
->hideFromIndex(),
Text::make(trans('laravel-meta::messages.og_title'), $columnMeta.'.og_title')
->rules('nullable', 'string')
->hideFromIndex(),
Text::make(trans('laravel-meta::messages.og_description'), $columnMeta.'.og_description')
->rules('nullable', 'string')
->hideFromIndex(),
Image::make(trans('laravel-meta::messages.og_image'), $columnMeta.'.og_image')
->hideFromIndex(),
];
->hideFromIndex();
}
if (Arr::get($config, 'og_title', true)) {
$fields[] = Text::make(trans('laravel-meta::messages.og_title'), $columnMeta.'.og_title')
->rules(Arr::get($config, 'required.og_title', false) ? 'required' : 'nullable', 'string')
->hideFromIndex();
}
if (Arr::get($config, 'og_description', true)) {
$fields[] = Text::make(trans('laravel-meta::messages.og_description'), $columnMeta.'.og_description')
->rules(Arr::get($config, 'required.og_description', false) ? 'required' : 'nullable', 'string')
->hideFromIndex();
}
if (Arr::get($config, 'og_image', true)) {
$fields[] = Image::make(trans('laravel-meta::messages.og_image'), $columnMeta.'.og_image')
->hideFromIndex();
}

return $fields;
}
}

0 comments on commit fda7dd1

Please sign in to comment.