Htmlable label - illegal offset type

Hello, everyone. I'm having a little problem with label(). My goal would be to replace the label text with a small image (a google logo). The problem is that when I try to use a new HtmlString as the label value, it returns an error like ‘Illegal offset type’ no matter what I enter. As soon as I enter a normal string instead, everything is fine. I also found this example online:
->label(fn() => new HtmlString('I accept the <a class="underline" href="/terms" target="_blank">terms and conditions</a>'))
->label(fn() => new HtmlString('I accept the <a class="underline" href="/terms" target="_blank">terms and conditions</a>'))
but I can't figure out what the problem is. Is it possible that it does work only on Forms? Thank you all very much for your support 😄
Solution:
@Dennis Koch I solved the problem: in AppServiceProvider I created this function: ``` protected static function translateLabel(Field|Column $element): string...
Jump to solution
5 Replies
Dennis Koch
Dennis Koch4mo ago
This works for me:
Tables\Columns\TextColumn::make('name')
->label(fn () => new HtmlString('<b>test</b>'))
Tables\Columns\TextColumn::make('name')
->label(fn () => new HtmlString('<b>test</b>'))
Can you share the error (via Flare) for more infos? And the actual code you are using?
Davide Cariola
Davide CariolaOP4mo ago
Hi Dennis, thanks for replying. I don't have a Flare account sadly, I attached a screenshot of the error. Here's the code of the column:
TextColumn::make('propertyFree.metric.google_news')
->label(fn () => new HtmlString('<b>test</b>'))
->state(function ($record) : View {
$bool = $record->propertyFree->metric && $record->propertyFree->metric->google_news ? true : false;
return view(
'filament.tables.publication.columns.check',
['bool' => $bool],
);
})
->toggleable(),
TextColumn::make('propertyFree.metric.google_news')
->label(fn () => new HtmlString('<b>test</b>'))
->state(function ($record) : View {
$bool = $record->propertyFree->metric && $record->propertyFree->metric->google_news ? true : false;
return view(
'filament.tables.publication.columns.check',
['bool' => $bool],
);
})
->toggleable(),
It's pretty much standard. This is a Livewire component which has Filament forms & table. Everything works has a charm, except this.
No description
Dennis Koch
Dennis Koch4mo ago
You return a view as the state. I think that’s the issue. State is a value
Davide Cariola
Davide CariolaOP4mo ago
Ok, I'll look into that 🤔 thank you Dennis! Sadly, also with this kind of code:
IconColumn::make('test')
->label(fn() => new HtmlString('<b>test</b>'))
->boolean()
->toggleable(),
IconColumn::make('test')
->label(fn() => new HtmlString('<b>test</b>'))
->boolean()
->toggleable(),
It returns the same error. I'm working on already-done code, so I'm trying to understand what's happening 😅 @Dennis Koch could it be that I have this in the AppServiceProvider?
Field::configureUsing(function (Field $field){
$field->translateLabel();
});

Column::configureUsing(function (Column $column) {
$column->translateLabel();
});
Field::configureUsing(function (Field $field){
$field->translateLabel();
});

Column::configureUsing(function (Column $column) {
$column->translateLabel();
});
EDIT: Yes, I commented the code above and now it works without problems, but I really need the labels to be automatically translated without me writing it 😅
Solution
Davide Cariola
Davide Cariola4mo ago
@Dennis Koch I solved the problem: in AppServiceProvider I created this function:
protected static function translateLabel(Field|Column $element): string
{
$label = $element->getLabel();

if($label instanceof HtmlString){
return $label;
}

return __($label);
}
protected static function translateLabel(Field|Column $element): string
{
$label = $element->getLabel();

if($label instanceof HtmlString){
return $label;
}

return __($label);
}
and then:
Field::configureUsing(function (Field $field){
self::translateLabel($field);
});

Column::configureUsing(function (Column $column) {
self::translateLabel($column);
});
Field::configureUsing(function (Field $field){
self::translateLabel($field);
});

Column::configureUsing(function (Column $column) {
self::translateLabel($column);
});
For now it's working 😄 thanks for your input!
Want results from more Discord servers?
Add your server