Skip to content

Listview label

We strongly need to optimize the List View entry as this is not really human readable:

alt text

We can override getLabel() function in our model.

// ...
public function getLabel(): string
{
    $label = $this->licenseplate; // magic __get function
    $model = $this->getProperty('model');
    if ($model->getValue())
        $label .= ' - ' . $model->toDisplayValue();
    return $label;
}
// ...

Tip: Intellisense

Your modern coding IDE will help you, if intellisense is enabled:

alt text

We highly recomment using VSCODE which is free to use.

That feels better now:

alt text

But there is room for improvement. Maybe you have already seen getLabelHtml() function in vscode screenshot above.

// ...
public function getLabelHtml(): string
{
    $label = (string) $this->licenseplate;
    $model = $this->getProperty('model');
    $month = $this->getProperty('manufacture_month');

    $details = [
        $model?->toDisplayValue(),
        $month?->toDisplayValue(),
    ];

    // remove empty entries
    $details = array_filter($details);

    if ($details)
        $label .= '<br/>' . '<small>' . implode(' &bull; ', $details) . '</small>';

    return $label;
}
// ...

Now that's almost perfect:

alt text

Last improvement for now: we have HTML-helpers for you. Replace getLabelHtml() by this code:

public function getLabelHtml(): string
{
    // gather our data
    $model = $this->getProperty('model');
    $month = $this->getProperty('manufacture_month');
    $details = [
        $model?->toDisplayValue(),
        $month?->toDisplayValue(),
    ];
    $details = array_filter($details);// remove empty entries

    // create a tile
    \System\UI\Output::use(); // important for autoloading
    $tile = new \System\UI\Output\Elements\Tile('car', $this->licenseplate);
    if ($details) $tile->subtitle = implode(' &bull; ', $details);
    return $tile;
}
alt text

Next

Calculate a vehicle's age