Changes
This commit is contained in:
@@ -6,16 +6,18 @@ use App\Filament\Resources\PageResource;
|
||||
use App\Models\SubSection;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CreatePage extends CreateRecord
|
||||
{
|
||||
protected static string $resource = PageResource::class;
|
||||
|
||||
protected array $seoData;
|
||||
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
$subSection = SubSection::find($data['sub_section_id']);
|
||||
|
||||
|
||||
if ($subSection == null) {
|
||||
$data['path'] = $data['slug'];
|
||||
} elseif($subSection->mainSection == null) {
|
||||
@@ -25,23 +27,50 @@ class CreatePage extends CreateRecord
|
||||
}
|
||||
unset($data['sub_section_id']);
|
||||
|
||||
$this->seoData = $this->generateSeo($data);
|
||||
|
||||
$data['search_data'] = $this->generateSearchData($data['content']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function afterCreate(): void
|
||||
{
|
||||
$this->record->seo()->create($this->seoData);
|
||||
}
|
||||
|
||||
private function generateSeo(array $data) : array
|
||||
{
|
||||
$title = $data['title'];
|
||||
$rowData = $this->getFirstBlockByName('paragraph', $data['content']);
|
||||
$description = strip_tags($rowData['data']['content']);
|
||||
|
||||
return [
|
||||
'title' => $title,
|
||||
'description' => Str::limit($description, 160),
|
||||
];
|
||||
}
|
||||
private function getFirstBlockByName(string $name, array $content) : array|null
|
||||
{
|
||||
$data = null;
|
||||
foreach ($content as $block) {
|
||||
$data = ($block['type'] === $name) ? $block : null;
|
||||
break;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function generateSearchData(array $data) : string
|
||||
{
|
||||
$result = "";
|
||||
foreach ($data['content'] as $block) {
|
||||
foreach ($data as $block) {
|
||||
$result .= $this->getDataFromBlocks($block);
|
||||
}
|
||||
|
||||
// Удаляем лишние пробелы и переносы строк
|
||||
$result = preg_replace('/\s+/', ' ', $result);
|
||||
$result = trim($result);
|
||||
|
||||
|
||||
// Приводим текст к нижнему регистру
|
||||
$data['search_data'] = strtolower($result);
|
||||
|
||||
return $data;
|
||||
return strtolower($result);
|
||||
}
|
||||
|
||||
private function getDataFromBlocks($block) : string
|
||||
|
||||
Reference in New Issue
Block a user