In a plugin's backend you can have models that show a grid of records to maintain tables.
In this sample I want to get country details from a selected country from the Rainlab/Location plugin.
In the columns YAML in plugins/yournamespace/yourplugin/models/yourtable/columns.yaml:
columns:
... here you have your own specs ...
country_id:
type: country_details
relation: country
invisible: true
path: ~/plugins/yournamespace/yourplugin/partials/columns/_country_flag.htm
... more specs ...
The new type is _countrydetails and will be defined in the Plugin.php.
In the Plugin.php:
use Rainlab\Location\Models\Country;
use Yournamespace\Yourplugin\Controllers\Yourcontroller as PluginController;
....
public function registerListColumnTypes()
{
return [
'country_details' => [$this, 'evalCountryDetailsListColumn'],
];
}
....
/**
* $value = field value, this is the value for the 'country_id:' in the YAML
* if the 'xxx:' field in the YAML doesn't exist in a model, this value will
* be null.
*
* $colum = column definition
* $record = model (record)
*/
public function evalCountryDetailsListColumn($value, $column, $record)
{
$country = Country::where('id', $record->country_id)->first();
$controller = new PluginController();
$partial = $controller->makePartial($column->path, [
'value' => $value,
'column' => $column,
'record' => $record,
'country' => $country
]);
return $partial;
}
Partial: ~/plugins/yournamespace/yourplugin/partials/columns/_country_flag.htm
<?php
if ($country) {
printf('<img src="https://www.countryflags.io/%1$s/flat/32.png"> %2$s',
$country->code,
$country->name
);
}
?>