CoolCMS API

ToggleRenderer extends CellRenderer
in package
implements InlineEditableRenderer uses InlineEditableTrait

Renders an inline editable switch, an alternative to {@see SelectRenderer} for toggling between two or more states.

Shares the whole inline-update mechanism (the inlineUpdate! signal, the property stored via manager->updateColumns(), handled in HandleTrait). A click switches to the next value in order (cycling when there are more than two). The target URL already carries the next value, so it is a plain link (post-redirect-get, no extra JS).

Each value can get a color role (success, danger, neutral) that tints the switch in its current state. For a typical "active" column use setYesNo(): no is danger (left), yes is success (right).

Table of Contents

Interfaces

InlineEditableRenderer
Renderer whose value can be changed inline from the list: the change sends the signal `inlineUpdate!` and saves the new property value to the database (handled in {@see \App\UI\Admin\System\Components\DataGridComponent\Traits\HandleTrait}).

Properties

$options  : array<int|string, string>
$updateProperty  : string|null
$variants  : array<int|string, string>

Methods

__construct()  : mixed
create()  : static
getOptions()  : array<int|string, string>
getTemplateName()  : string
Cell template name (`Cells/<type>.latte`).
getUpdateProperty()  : string
prepare()  : array<string, mixed>
Variables for the cell template.
setOptions()  : static
setUpdateProperty()  : static
Property (database column) the new value is saved to.
setVariants()  : static
Color role per value (value => success, danger, neutral).
setYesNo()  : static
Shortcut for the most common yes/no switch over a bool column.
toCsvValue()  : string
The `prepare()` vars have no `text` key (a switch has `label`/`variant`/`position`), so the CSV export uses the option label.
buildUpdateUrl()  : string
Builds the URL that switches to the next value: the default inline update (the `inlineUpdate!` signal calling `manager->updateColumns()` on the row column).
csvOptionLabel()  : string
Label of the current value for the CSV export. {@see SelectRenderer} / {@see ToggleRenderer} have no plain `text` key in the `prepare()` vars (options/current, label/variant for the switch UI), so their `toCsvValue()` uses this instead of the default from {@see CellRenderer}.
resolveOptionKey()  : int|string|null
Key in options matching the current value (compared as strings because of int vs. string keys), or null when the value is not among the options.
toScalarValue()  : mixed
Column value to a scalar comparable with the keys of `options` maps: a `BackedEnum` becomes its `value`, a pure enum its `name`, a bool 0/1. Other values pass unchanged.
toString()  : string
Column value (mixed) to text; non-stringable values give an empty string.

Properties

$variants

protected array<int|string, string> $variants = []

value => color role (success, danger, neutral)

Methods

create()

public static create() : static
Return values
static

getOptions()

public getOptions() : array<int|string, string>
Return values
array<int|string, string>

getTemplateName()

Cell template name (`Cells/<type>.latte`).

public getTemplateName() : string
Return values
string

prepare()

Variables for the cell template.

public prepare(mixed $value, object $entity, DataGrid $grid, BaseColumn $column) : array<string, mixed>
Parameters
$value : mixed
$entity : object
$grid : DataGrid
$column : BaseColumn
Return values
array<string, mixed>

setOptions()

public setOptions(array<int|string, string> $options) : static
Parameters
$options : array<int|string, string>

value => label

Return values
static

setUpdateProperty()

Property (database column) the new value is saved to.

public setUpdateProperty(string|null $updateProperty) : static
Parameters
$updateProperty : string|null
Return values
static

setVariants()

Color role per value (value => success, danger, neutral).

public setVariants(array<int|string, string> $variants) : static
Parameters
$variants : array<int|string, string>
Return values
static

setYesNo()

Shortcut for the most common yes/no switch over a bool column.

public setYesNo(string $yesLabel, string $noLabel) : static

The order (no, yes) sets the knob position: left is no, right is yes.

Parameters
$yesLabel : string
$noLabel : string
Return values
static

toCsvValue()

The `prepare()` vars have no `text` key (a switch has `label`/`variant`/`position`), so the CSV export uses the option label.

public toCsvValue(mixed $value, object $entity, DataGrid $grid, BaseColumn $column) : string
Parameters
$value : mixed
$entity : object
$grid : DataGrid
$column : BaseColumn
Return values
string

buildUpdateUrl()

Builds the URL that switches to the next value: the default inline update (the `inlineUpdate!` signal calling `manager->updateColumns()` on the row column).

protected buildUpdateUrl(DataGrid $grid, BaseColumn $column, object $entity, int|string $nextValue) : string

Subclasses (e.g. RelationToggleRenderer) override it with another signal.

Parameters
$grid : DataGrid
$column : BaseColumn
$entity : object
$nextValue : int|string
Return values
string

csvOptionLabel()

Label of the current value for the CSV export. {@see SelectRenderer} / {@see ToggleRenderer} have no plain `text` key in the `prepare()` vars (options/current, label/variant for the switch UI), so their `toCsvValue()` uses this instead of the default from {@see CellRenderer}.

protected csvOptionLabel(mixed $value) : string
Parameters
$value : mixed
Return values
string

resolveOptionKey()

Key in options matching the current value (compared as strings because of int vs. string keys), or null when the value is not among the options.

protected resolveOptionKey(mixed $value) : int|string|null
Parameters
$value : mixed
Return values
int|string|null

toScalarValue()

Column value to a scalar comparable with the keys of `options` maps: a `BackedEnum` becomes its `value`, a pure enum its `name`, a bool 0/1. Other values pass unchanged.

protected toScalarValue(mixed $value) : mixed

IMPORTANT: entities return an enum object for enum columns (typed property), so without this normalization the cell template receives an object and (string) $enum is a fatal error, not an empty output.

Parameters
$value : mixed

toString()

Column value (mixed) to text; non-stringable values give an empty string.

protected toString(mixed $value) : string

An enum prints its value/name; a bool keeps the (string) semantics (false is empty, not "0"), as the admin always printed it.

Parameters
$value : mixed
Return values
string
On this page

Search results