Skip to content

Commit a0955a1

Browse files
Phiosssifox
andauthored
Allow customization of the title column's label (#2318)
* 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]>
1 parent cc32545 commit a0955a1

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Http/Controllers/Admin/ModuleController.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ abstract class ModuleController extends Controller
231231
*/
232232
protected $titleColumnKey = 'title';
233233

234+
/**
235+
* Label of the index column to use as name column.
236+
*
237+
* @var string
238+
*/
239+
protected $titleColumnLabel = 'Title';
240+
234241
/**
235242
* Name of the index column to use as identifier column.
236243
*
@@ -245,6 +252,13 @@ abstract class ModuleController extends Controller
245252
*/
246253
protected $titleFormKey;
247254

255+
/**
256+
* Label of the title field in forms.
257+
*
258+
* @var string
259+
*/
260+
protected $titleFormLabel = 'Title';
261+
248262
/**
249263
* Feature field name if the controller is using the feature route (defaults to "featured").
250264
*
@@ -643,6 +657,30 @@ protected function setTitleColumnKey(string $titleColumnKey): void
643657
$this->titleColumnKey = $titleColumnKey;
644658
}
645659

660+
/**
661+
* Sets the label to use for title column, defaults to `Title`.
662+
*/
663+
protected function setTitleColumnLabel(string $titleColumnLabel): void
664+
{
665+
$this->titleColumnLabel = $titleColumnLabel;
666+
}
667+
668+
/**
669+
* Sets the field to use as title in forms, defaults to `title`.
670+
*/
671+
protected function setTitleFormKey(string $titleFormKey): void
672+
{
673+
$this->titleFormKey = $titleFormKey;
674+
}
675+
676+
/**
677+
* Sets the label to use for title field in forms, defaults to `Title`.
678+
*/
679+
protected function setTitleFormLabel(string $titleFormLabel): void
680+
{
681+
$this->titleFormLabel = $titleFormLabel;
682+
}
683+
646684
/**
647685
* Usually not required, but in case customization is needed you can use this method to set the name of the model
648686
* this controller acts on.
@@ -789,6 +827,7 @@ protected function getIndexTableColumns(): TableColumns
789827
$columns->add(
790828
Text::make()
791829
->field($this->titleColumnKey)
830+
->title($this->titleColumnKey === 'title' && $this->titleColumnLabel === 'Title' ? twillTrans('twill::lang.main.title') : $this->titleColumnLabel)
792831
->sortable()
793832
->linkToEdit()
794833
);
@@ -1731,6 +1770,7 @@ protected function getIndexData(array $prependScope = []): array
17311770
'permalink' => $this->getIndexOption('permalink'),
17321771
'bulkEdit' => $this->getIndexOption('bulkEdit'),
17331772
'titleFormKey' => $this->titleFormKey ?? $this->titleColumnKey,
1773+
'titleFormLabel' => $this->titleFormLabel ?? $this->titleColumnLabel,
17341774
'baseUrl' => $baseUrl,
17351775
'permalinkPrefix' => $this->getPermalinkPrefix($baseUrl),
17361776
'additionalTableActions' => $this->additionalTableActions(),
@@ -2211,6 +2251,7 @@ protected function form(?int $id, ?TwillModelContract $item = null): array
22112251
'moduleName' => $this->moduleName,
22122252
'routePrefix' => $this->routePrefix,
22132253
'titleFormKey' => $this->titleFormKey ?? $this->titleColumnKey,
2254+
'titleFormLabel' => $this->titleFormLabel ?? $this->titleColumnLabel,
22142255
'publish' => $item->canPublish ?? true,
22152256
'publishDate24Hr' => Config::get('twill.publish_date_24h') ?? false,
22162257
'publishDateFormat' => Config::get('twill.publish_date_format') ?? null,

views/partials/create.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
@php
22
$titleFormKey = $titleFormKey ?? 'title';
3+
$titleFormLabel = $titleFormLabel ?? 'Title';
34
@endphp
45
<x-twill::input
56
:name="$titleFormKey"
6-
:label="$titleFormKey === 'title' ? twillTrans('twill::lang.modal.title-field') : ucfirst($titleFormKey)"
7+
:label="$titleFormKey === 'title' && $titleFormLabel === 'Title' ? twillTrans('twill::lang.modal.title-field') : $titleFormLabel"
78
:translated="$translateTitle ?? false"
89
:required="true"
910
on-change="formatPermalink"

0 commit comments

Comments
 (0)