Skip to content

Commit

Permalink
Tested new slug mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-layson committed Apr 8, 2024
1 parent cdd8200 commit 47b992f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/FilaCms.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function systemUser()

public function getModelFromResource($resource)
{
dd([$resource, self::$contentModels]);
return array_search($resource, self::$contentModels);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,43 @@ class CreateAbstractContentResource extends CreateRecord

protected function mutateFormDataBeforeCreate(array $data): array
{
$class = parent::static;
$class = get_class($this);
$parent = new $class();
$resource = $parent::getResource();
$model = FilaCms::getModelFromResource($resource);

FilaCms::getModelFromResource($parent->resource);
if (is_null($data['slug'])) {
// auto-generate then check
$data['slug'] = Str::slug($data['title']);
} else {
$data['slug'] = Str::slug($data['slug']);
}

if ($this->checkIfSlugExists($data['slug'], $model)) {
$increment = 1;

while (true) {
$slug = $data['slug'] . '-' . $increment;
if ($this->checkIfSlugExists($slug, $model) === FALSE) {
$data['slug'] = $slug;
break;
}
$increment++;
}
}

return $data;
}

protected function checkIfSlugExists($slug)
protected function checkIfSlugExists($slug, $modelName)
{
$model = new $modelName;

$data = $model->where('slug', $slug)->first();

if (is_null($data)) {
return FALSE;
}
return TRUE;
}
}

0 comments on commit 47b992f

Please sign in to comment.