Skip to content

Commit

Permalink
Allow customization of the title column's label (#2318)
Browse files Browse the repository at this point in the history
* Allow customize title column's label

* Preserve support for translated default title label and apply to title form field label

---------

Co-authored-by: Quentin Renard <[email protected]>
  • Loading branch information
Phiosss and ifox authored Jan 24, 2024
1 parent cc32545 commit a0955a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/Http/Controllers/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ abstract class ModuleController extends Controller
*/
protected $titleColumnKey = 'title';

/**
* Label of the index column to use as name column.
*
* @var string
*/
protected $titleColumnLabel = 'Title';

/**
* Name of the index column to use as identifier column.
*
Expand All @@ -245,6 +252,13 @@ abstract class ModuleController extends Controller
*/
protected $titleFormKey;

/**
* Label of the title field in forms.
*
* @var string
*/
protected $titleFormLabel = 'Title';

/**
* Feature field name if the controller is using the feature route (defaults to "featured").
*
Expand Down Expand Up @@ -643,6 +657,30 @@ protected function setTitleColumnKey(string $titleColumnKey): void
$this->titleColumnKey = $titleColumnKey;
}

/**
* Sets the label to use for title column, defaults to `Title`.
*/
protected function setTitleColumnLabel(string $titleColumnLabel): void
{
$this->titleColumnLabel = $titleColumnLabel;
}

/**
* Sets the field to use as title in forms, defaults to `title`.
*/
protected function setTitleFormKey(string $titleFormKey): void
{
$this->titleFormKey = $titleFormKey;
}

/**
* Sets the label to use for title field in forms, defaults to `Title`.
*/
protected function setTitleFormLabel(string $titleFormLabel): void
{
$this->titleFormLabel = $titleFormLabel;
}

/**
* Usually not required, but in case customization is needed you can use this method to set the name of the model
* this controller acts on.
Expand Down Expand Up @@ -789,6 +827,7 @@ protected function getIndexTableColumns(): TableColumns
$columns->add(
Text::make()
->field($this->titleColumnKey)
->title($this->titleColumnKey === 'title' && $this->titleColumnLabel === 'Title' ? twillTrans('twill::lang.main.title') : $this->titleColumnLabel)
->sortable()
->linkToEdit()
);
Expand Down Expand Up @@ -1731,6 +1770,7 @@ protected function getIndexData(array $prependScope = []): array
'permalink' => $this->getIndexOption('permalink'),
'bulkEdit' => $this->getIndexOption('bulkEdit'),
'titleFormKey' => $this->titleFormKey ?? $this->titleColumnKey,
'titleFormLabel' => $this->titleFormLabel ?? $this->titleColumnLabel,
'baseUrl' => $baseUrl,
'permalinkPrefix' => $this->getPermalinkPrefix($baseUrl),
'additionalTableActions' => $this->additionalTableActions(),
Expand Down Expand Up @@ -2211,6 +2251,7 @@ protected function form(?int $id, ?TwillModelContract $item = null): array
'moduleName' => $this->moduleName,
'routePrefix' => $this->routePrefix,
'titleFormKey' => $this->titleFormKey ?? $this->titleColumnKey,
'titleFormLabel' => $this->titleFormLabel ?? $this->titleColumnLabel,
'publish' => $item->canPublish ?? true,
'publishDate24Hr' => Config::get('twill.publish_date_24h') ?? false,
'publishDateFormat' => Config::get('twill.publish_date_format') ?? null,
Expand Down
3 changes: 2 additions & 1 deletion views/partials/create.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
@php
$titleFormKey = $titleFormKey ?? 'title';
$titleFormLabel = $titleFormLabel ?? 'Title';
@endphp
<x-twill::input
:name="$titleFormKey"
:label="$titleFormKey === 'title' ? twillTrans('twill::lang.modal.title-field') : ucfirst($titleFormKey)"
:label="$titleFormKey === 'title' && $titleFormLabel === 'Title' ? twillTrans('twill::lang.modal.title-field') : $titleFormLabel"
:translated="$translateTitle ?? false"
:required="true"
on-change="formatPermalink"
Expand Down

0 comments on commit a0955a1

Please sign in to comment.