octobercms / october

Self-hosted CMS platform based on the Laravel PHP Framework.
https://octobercms.com/
Other
11.01k stars 2.21k forks source link

Implement select For type: taglist #5776

Closed sqwk closed 7 months ago

sqwk commented 8 months ago

When using a taglist field in relation mode, one cannot define a select: concat() like for the relation type.

users:
    type: taglist
    mode: relation
    customTags: false
    keyFrom: id
    select: concat(first_name, ' ', last_name)
    useKey: true

This would be helpful when linking multiple authors/users/owners via a many to many relationship with the built in backend_users. type: relation (checkboxes) works fine, as does the list representation when using the relation manager, but both take up considerably more UI room than a simple taglist.

daftspunk commented 8 months ago

Hey @sqwk

To confirm, is this configuration used in Tailor or a plugin?

sqwk commented 8 months ago

This is a plugin. I haven't tried it with Tailor.

daftspunk commented 8 months ago

The labelling may not need to occur at the SQL level. Try adding an accessor method to your model...

public function getFullNameAttribute(): string
{
    return "{$this->first_name} {$this->last_name}";
}

Then use nameFrom: full_name

sqwk commented 8 months ago

I don't believe I can add an accessor to a model that I don't control (\Backend\Models\User in this particular case).

daftspunk commented 8 months ago

Adding the method via extension might work

\October\Rain\Extension\Container::extendClass(\Backend\Models\User::class, function($model) {
    $model->addDynamicMethod('getFullNameAttribute', function() use ($model) {
        return "{$model->first_name} {$model->last_name}";
    });
});

edit: The method already exists on \Backend\Models\User::class