Relationship Fields in Filters
Created 7 years ago by emergingdznsHow do I make the relationship fields in the filters of the table builder display the labels of the options and not the ids?
For example, I have a Category model and a relationship in the module for the CategoryModel. Same for the Type model. Then the $filters
array looks like this:
protected $filters = [
'name',
'category',
'type',
];
But when I view it, and pull down the select list, the ids appear. See this screenshot:
Also, what about using the created_at field as a filter? I'm still confused by this.
Any relationship fields should show name or title by default. anomaly.field_type.relationship
, for example.
Of course for right behavior, they should be setup correct, before that.
As I can see, you want to filter by no-relation field. That means that you need to write some lines of code, to solve your problem.
Please, take a look at the examples:
StatusFilterQuery there /core/anomaly/users-module/src/User/Table/UserTableBuilder.php
and /core/anomaly/posts-module/src/Post/Table/PostTableBuilder.php
Then you'd better to investigate how filters are appointed in other modules.
As for me, on you place, I would prefer to write a function in Presenter class of your model (stream).
'search' => [
'columns' => [
'name',
'slug',
'description',
'tags',
],
],
'status' => [
'filter' => 'select',
'query' => StatusFilterQuery::class,
'options' => [
'live' => 'defr.module.default::field.status.option.live',
'draft' => 'defr.module.default::field.status.option.draft',
'dead' => 'defr.module.default::field.status.option.dead',
],
],
'user' => [
'filter' => 'search',
'query' => RelationFilterQuery::class,
'fields' => [
'username',
'email',
'display_name',
'first_name',
'last_name',
],
'config' => [
'placeholder' => 'Search by user data...',
'prefix' => 'user_',
]
],
@emergingdzns this will use your title_column
from the related stream by default. But you can also set the title_name
too: https://pyrocms.com/documentation/relationship-field-type/latest#introduction/configuration
Thanks @ryanthompson! That was quite helpful. I needed to add the title_column. The title column is called "name". I thought it was automatic, but anyway it's working now.
One more question, is there a reason I can't add 'id'
to the $filters
array? I'd like for the user to be able to enter an id number to filter the list, but when I add 'id'
I get a 500 error.
In an ideal world, it would be great to have a config option for the filters such that we can define a wrapper for the input.
Thanks!
id
is technically the column related part of the field - the field would not include the _id
.
You could however use a generic input
filter and specify the heading
and the slug
will be the column you want to filter by:
'created_by' => [
'heading' => 'Created By',
'filter' => 'input',
]
Sorry @ryanthompson I didn't mean the created_by_id
. I'm all good there. I meant the actual id
field/column of the table.
Oh Duh. nevermind I added it like you did in that last example but just used id
instead of created_by
and it works!
@emergingdzns this will use your
title_column
from the related stream by default. But you can also set thetitle_name
too: https://pyrocms.com/documentation/relationship-field-type/latest#introduction/configuration