How to show the value of Select Field Type in PyroCMS's TableBuilder?

270 Views Asked by At

I have a field called type and it is a select field type. I see the values instead of the keys in select HTML element of the form builder but, in the table builder I only see the key as the not the values. It is clear that is how they are derived from the database but it there a setting to switch this? I don't want to hard code this. I read some related pages in the documents but could not find a configuration for this.

    protected $fields = [
        'type' => [
            'type' => 'anomaly.field_type.select',
            'config' => [
                'options'       => ['foo' => 'Foo', 'bar' => 'Bar'],
                'mode'          => 'dropdown'
            ]
        ]
    ];
2

There are 2 best solutions below

0
Ryan Thompson On BEST ANSWER

Your table column value would look something like this:

protected $columns = [ 'type', // Shows the KEY value for select field types 'entry.type.value', // Would show the label value for select field type ];

Note that you can access presenter methods easily AND retain automatic field operations in columns by prefixing with entry..

2
Piterden On

The selectFT has

/* @var SelectFieldType $fieldType */
$fieldType = $entry->getFieldType('type');

$presenter = (new Decorator())->decorate($fieldType);

dd([
    'key'     => $presenter->key(),
    'value'   => $presenter->value(),
    'options' => $fieldType->getOptions(),
]);

Also, you can define columns like so:

/**
 * Table's columns
 *
 * @var array
 */
protected $columns = [
    'name' => [
        'heading' => 'Name',
        'value'   => '<strong>{entry.name}</strong>',
    ],
    'type' => [
        'heading' => 'Type',
    ],
    'categories' => [
        'heading' => 'Categories',
        'value'   => 'entry.type.categories.pluck("name")|join("<br>")',
    ],
];