Skip to content

Best way to customize relation display fields #58

@deizel

Description

@deizel

Recently had a requirement to display a shorter name for a related field in the index table (from the default name field to one called short_name).

This is the most elegant solution I could come up with:

    public function index()
    {
        $action = $this->Crud->action();
        $action->config([
            'scaffold.fields' => [
                'facility_id' => [
                    'formatter' => function ($name, $value, $entity) {
                        return $this->getView()->Html->link(
                            $entity->facility->short_name,
                            ['controller' => 'Facilities', 'action' => 'view', $entity->facility->id]
                        );
                    }
                ],

Other things I tried included:

  • Using an element formatter (though getting all the information I needed to create the link in a reusable way, such as the target controller name, wasn't pretty so I abandoned that idea.)
  • Changing the display field on the association (we needed the full name in the search filters, so this involved changing the displayField in beforePaginate and back again in relatedModel, which wasn't ideal either.)

Is this a common enough use case where it should be configurable like like title and label?

    public function index()
    {
        $action = $this->Crud->action();
        $action->config([
            'scaffold.fields' => [
                'facility_id' => ['displayField' => 'short_name'],

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions