feat: implement page template hero functionality with dynamic image handling and localization support for enhanced visual presentation

This commit is contained in:
Ümit Tunç
2026-05-22 07:36:24 +03:00
parent ead0077fde
commit 11cb89c944
11 changed files with 232 additions and 34 deletions
@@ -35,7 +35,6 @@ class ContentSection
RichEditor::make('content')
->label(__('pages.content_field'))
->required()
->fileAttachmentsDisk('public')
->fileAttachmentsDirectory('pages')
->fileAttachmentsVisibility('public')
@@ -2,12 +2,14 @@
namespace App\Filament\Admin\Resources\Pages\Schemas\Sections;
use App\Support\PageTemplateHero;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Utilities\Get;
class PageSettingsSection
{
@@ -15,6 +17,13 @@ class PageSettingsSection
{
return Section::make(__('pages.form_section_page_settings'))
->schema([
Select::make('template')
->label(__('pages.template_field'))
->options(PageTemplateHero::templateFormOptions())
->default('default')
->live()
->columnSpanFull(),
FileUpload::make('featured_image')
->label(__('pages.featured_image_field'))
->image()
@@ -27,7 +36,13 @@ class PageSettingsSection
'4:3',
'1:1',
])
->helperText(__('pages.featured_image_helper_text'))
->helperText(function (Get $get): string {
if (PageTemplateHero::hasHero($get('template'))) {
return __('pages.featured_image_template_hero_helper');
}
return __('pages.featured_image_helper_text');
})
->columnSpanFull(),
Select::make('author_id')
@@ -61,25 +76,8 @@ class PageSettingsSection
->preload()
->helperText(__('pages.parent_helper_text'))
->columnSpanFull(),
Select::make('template')
->label(__('pages.template_field'))
->options([
'default' => __('pages.template_default'),
'landing' => __('pages.template_landing'),
'blog' => __('pages.template_blog'),
'contact' => __('pages.template_contact'),
'home' => 'Home',
'corporate.testimonials' => 'Müşteri Görüşleri (Kurumsal)',
'corporate.logos' => 'Logolarımız (Kurumsal)',
'corporate.partners' => 'Çözüm Ortaklarımız (Kurumsal)',
'corporate.bank-accounts' => 'Banka Bilgilerimiz (Kurumsal)',
'corporate.online-payment' => 'Online Ödeme (Kurumsal)',
])
->default('default')
->columnSpanFull(),
TextInput::make('sort_order')
TextInput::make('sort_order')
->label(__('pages.sort_order_field'))
->numeric()
->default(0)
+4 -2
View File
@@ -8,6 +8,7 @@ use App\Models\HeaderTemplate;
use App\Models\FooterTemplate;
use App\Services\TemplateService;
use App\Support\PageStructuredData;
use App\Support\PageTemplateHero;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
@@ -145,6 +146,7 @@ class PageController extends Controller
return view($view, [
'page' => $page,
'pageHero' => $page ? PageTemplateHero::resolveForPage($page) : null,
'settings' => $settings,
'sections' => $page ? ($page->parsed_sections ?? $page->sections ?? $page->data ?? []) : [],
'templatedSections' => $page ? ($page->templated_sections ?? collect([])) : collect([]),
@@ -192,8 +194,8 @@ class PageController extends Controller
? ($page->translate('meta_description') ?: ($page->excerpt ?? null))
: ($page->meta_description ?? $page->excerpt ?? null);
if ($page->featured_image) {
$metaImage = asset('storage/' . $page->featured_image);
if ($page->featured_image_url) {
$metaImage = $page->featured_image_url;
}
}
+8 -6
View File
@@ -3,6 +3,7 @@
namespace App\Models;
use App\Models\SectionTemplate;
use App\Support\PageTemplateHero;
use App\Traits\HasTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -115,13 +116,14 @@ class Page extends Model
return '/' . $this->slug;
}
public function getFeaturedImageUrlAttribute()
public function getFeaturedImageUrlAttribute(): ?string
{
if ($this->featured_image) {
return asset('storage/' . $this->featured_image);
}
return null;
return PageTemplateHero::urlForPage($this);
}
public function usesTemplateHeroImage(): bool
{
return ! $this->featured_image && PageTemplateHero::hasHero($this->template);
}
/**
+121
View File
@@ -0,0 +1,121 @@
<?php
namespace App\Support;
use App\Models\Page;
class PageTemplateHero
{
public static function config(?string $template): ?array
{
if (! filled($template)) {
return null;
}
$heroes = config('page_templates.heroes', []);
return $heroes[$template] ?? null;
}
public static function hasHero(?string $template): bool
{
return filled(static::imagePath($template));
}
public static function imagePath(?string $template): ?string
{
return static::config($template)['image'] ?? null;
}
public static function webpPath(?string $template): ?string
{
return static::config($template)['webp'] ?? null;
}
public static function url(?string $template): ?string
{
$path = static::imagePath($template);
return $path ? asset($path) : null;
}
public static function webpUrl(?string $template): ?string
{
$path = static::webpPath($template);
return $path ? asset($path) : null;
}
public static function featuredStoragePath(Page $page): ?string
{
$value = $page->featured_image;
if (blank($value)) {
return null;
}
if (is_array($value)) {
$value = $value[0] ?? null;
}
return is_string($value) && $value !== '' ? $value : null;
}
public static function urlForPage(Page $page): ?string
{
if ($path = static::featuredStoragePath($page)) {
return asset('storage/' . $path);
}
return static::url($page->template);
}
/**
* Hero alanı için görsel verisi (yüklenen öne çıkan görsel veya şablon varsayılanı).
*
* @return array{image: string, webp?: string, width?: int, height?: int, from_upload: bool}|null
*/
public static function resolveForPage(?Page $page, ?string $templateFallback = null): ?array
{
if ($page && ($path = static::featuredStoragePath($page))) {
return [
'image' => asset('storage/' . $path),
'from_upload' => true,
];
}
$template = $page?->template ?? $templateFallback;
$config = static::config($template);
if (! $config) {
return null;
}
return array_merge($config, ['from_upload' => false]);
}
/**
* @return array<string, string>
*/
public static function templateFormOptions(): array
{
$base = [
'default' => __('pages.template_default'),
'landing' => __('pages.template_landing'),
'blog' => __('pages.template_blog'),
'contact' => __('pages.template_contact'),
'home' => 'Home',
'corporate.testimonials' => 'Müşteri Görüşleri (Kurumsal)',
'corporate.logos' => 'Logolarımız (Kurumsal)',
'corporate.partners' => 'Çözüm Ortaklarımız (Kurumsal)',
'corporate.bank-accounts' => 'Banka Bilgilerimiz (Kurumsal)',
'corporate.online-payment' => 'Online Ödeme (Kurumsal)',
];
$fromConfig = collect(config('page_templates.labels', []))
->mapWithKeys(fn (string $label, string $key) => [$key => $label])
->all();
return array_merge($base, $fromConfig);
}
}