Custom Column Rendering (listView)

Introduction

Zero CMS provides an elegant mechanism to customize column output in admin listings without hacking the controller or list layout views. By defining a listView parameter in the model's getConfig() array, you can point a model column to a specific PHP view file.

How it Works Behind the Scenes

Template::renderFile($viewPath, [ 'value' => $record->{$field}, 'record' => $record ]);

Example 1: Color-Coded Status Badges

To render the status column of a Model as color-coded status badges, define the column with listView in your model configuration:

// Inside src/Models/Page.php
'status' => [
    'type' => 'select',
    'label' => 'Status',
    'editable' => true,
    'listDisplay' => true,
    'listView' => 'fields/status',
    'required' => true
]

Then, create the view at src/Modules/Admin/Views/fields/status.php:

<?php
$val = strtolower($value ?? '');
$icon = 'status-draft';
$class = 'status-draft';

if ($val === 'published' || $val === 'completed') {
    $icon = 'status-published';
    $class = 'status-published';
} elseif($val === 'archived') {
    $icon = 'status-archived';
    $class = 'status-archived';
}
?>
<span class="status-badge<?php echo $class; ?>">
    <span class="icon-svg" title="<?php echo htmlspecialchars($val); ?>">
        <?php echo \Zero\Core\App::svg($icon); ?>
    </span>
</span>

Example 2: Site Enabled Modules as Pills

To display a site's enabled modules as color-coded pills instead of raw JSON text, specify 'listView' => 'fields/modules' in your Site model config:

// Inside src/Models/Site.php
'enabled_modules' => [
    'type' => 'modules',
    'label' => 'Enabled Modules',
    'editable' => true,
    'listDisplay' => true,
    'listView' => 'fields/modules'
]

Then implement the view in src/Modules/Admin/Views/fields/modules.php. It decodes the JSON array of modules and outputs them as a series of pills styled with CSS:

<?php
$modules = json_decode($value ?? '[]', true);
if (!is_array($modules)) {
    $modules = [];
}
?>
<div class="module-pills-container">
    <?php if (empty($modules)): ?>
        <span class="module-pill module-empty">No modules</span>
    <?php else: ?>
        <?php foreach ($modules as $module): ?>
            <?php
            $moduleLower = strtolower($module);
            $icon = 'settings';
            $label = htmlspecialchars($module);
            
            if ($moduleLower === 'blog') {
                $icon = 'edit-3';
                $label = 'Blog';
            } elseif($moduleLower === 'shop') {
                $icon = 'shop';
                $label = 'Shop';
            } elseif($moduleLower === 'howtos') {
                $icon = 'book-open';
                $label = 'How-Tos';
            }
            ?>
            <span class="module-pill module-<?php echo htmlspecialchars($moduleLower); ?>" title="<?php echo $label; ?>">
                <span class="icon-svg"><?php echo \Zero\Core\App::svg($icon); ?></span>
                <span class="module-label"><?php echo $label; ?></span>
            </span>
        <?php endforeach; ?>
    <?php endif; ?>
</div>