Section::make('Server Variables')
->columnSpanFull()
->columns([
'default' => 1,
'lg' => 2,
])
->schema(function () {
/** @var Server $server */
$server = Filament::getTenant();
$variableComponents = [];
/** @var ServerVariable $serverVariable */
foreach ($server->serverVariables->sortBy(fn ($serverVariable) => $serverVariable->variable->sort) as $serverVariable) {
if (!$serverVariable->variable->user_viewable) {
continue;
}
$text = TextInput::make($serverVariable->variable->name)
->hidden(fn (Component $component) => $this->shouldHideComponent($serverVariable, $component))
->disabled(fn () => !$serverVariable->variable->user_editable)
->required(fn () => in_array('required', explode('|', $serverVariable->variable->rules)))
->rules([
fn (): Closure => function (string $attribute, $value, Closure $fail) use ($serverVariable) {
$validator = Validator::make(['validatorkey' => $value], [
'validatorkey' => $serverVariable->variable->rules,
]);
if ($validator->fails()) {
$message = str($validator->errors()->first())->replace('validatorkey', $serverVariable->variable->name);
$fail($message);
}
},
]);
$select = Select::make($serverVariable->variable->name)
->hidden(fn (Component $component) => $this->shouldHideComponent($serverVariable, $component))
->disabled(fn () => !$serverVariable->variable->user_editable) // TODO: find better way, doesn't look so nice
->options(fn () => $this->getSelectOptionsFromRules($serverVariable))
->selectablePlaceholder(false);
$components = [$text, $select];
foreach ($components as &$component) {
$component = $component
->live(onBlur: true)
->afterStateUpdated(function ($state) use ($serverVariable) {
$this->update($state, $serverVariable->variable->env_variable);
})
->hintIcon('tabler-code')
->label(fn () => $serverVariable->variable->name)
->default(fn () => $serverVariable->variable_value ?? $serverVariable->variable->default_value)
->hintIconTooltip(fn () => $serverVariable->variable->rules)
->prefix(fn () => '{{' . $serverVariable->variable->env_variable . '}}')
->helperText(fn () => empty($serverVariable->variable->description) ? '—' : $serverVariable->variable->description);
}
$variableComponents = array_merge($variableComponents, $components);
}
return $variableComponents;
}),
]);