Plugins can now define custom list column types

This commit is contained in:
Samuel Georges 2016-10-21 07:15:08 +11:00
parent 88196fb660
commit f9dd5b2f57
3 changed files with 66 additions and 0 deletions

View File

@ -12,6 +12,7 @@ use Carbon\Carbon;
use October\Rain\Html\Helper as HtmlHelper; use October\Rain\Html\Helper as HtmlHelper;
use October\Rain\Router\Helper as RouterHelper; use October\Rain\Router\Helper as RouterHelper;
use System\Helpers\DateTime as DateTimeHelper; use System\Helpers\DateTime as DateTimeHelper;
use System\Classes\PluginManager;
use Backend\Classes\ListColumn; use Backend\Classes\ListColumn;
use Backend\Classes\WidgetBase; use Backend\Classes\WidgetBase;
use ApplicationException; use ApplicationException;
@ -825,6 +826,9 @@ class Lists extends WidgetBase
if (method_exists($this, 'eval'. studly_case($column->type) .'TypeValue')) { if (method_exists($this, 'eval'. studly_case($column->type) .'TypeValue')) {
$value = $this->{'eval'. studly_case($column->type) .'TypeValue'}($record, $column, $value); $value = $this->{'eval'. studly_case($column->type) .'TypeValue'}($record, $column, $value);
} }
else {
$value = $this->evalCustomListType($column->type, $record, $column, $value);
}
/* /*
* Apply default value. * Apply default value.
@ -874,6 +878,28 @@ class Lists extends WidgetBase
// Value processing // Value processing
// //
/**
* Process a custom list types registered by plugins.
*/
protected function evalCustomListType($type, $record, $column, $value)
{
$plugins = PluginManager::instance()->getRegistrationMethodValues('registerListColumnTypes');
foreach ($plugins as $availableTypes) {
if (!isset($availableTypes[$type])) {
continue;
}
$callback = $availableTypes[$type];
if (is_callable($callback)) {
return call_user_func_array($callback, [$value, $column, $record]);
}
}
throw new ApplicationException(sprintf('List column type "%s" could not be found.', $type));
}
/** /**
* Process as text, escape the value * Process as text, escape the value
*/ */

View File

@ -181,6 +181,16 @@ class PluginBase extends ServiceProviderBase
return []; return [];
} }
/**
* Registers custom back-end list column types introduced by this plugin.
*
* @return array
*/
public function registerListColumnTypes()
{
return [];
}
/** /**
* Registers any mail templates implemented by this plugin. * Registers any mail templates implemented by this plugin.
* The templates must be returned in the following format: * The templates must be returned in the following format:

View File

@ -55,6 +55,11 @@ class PluginManager
*/ */
protected $disabledPlugins = []; protected $disabledPlugins = [];
/**
* @var array Cache of registration method results.
*/
protected $registrationMethodCache = [];
/** /**
* @var boolean Prevent all plugins from registering or booting * @var boolean Prevent all plugins from registering or booting
*/ */
@ -418,6 +423,31 @@ class PluginManager
return $identifier; return $identifier;
} }
/**
* Spins over every plugin object and collects the results of a method call.
* @param string $methodName
* @return array
*/
public function getRegistrationMethodValues($methodName)
{
if (isset($this->registrationMethodCache[$methodName])) {
return $this->registrationMethodCache[$methodName];
}
$results = [];
$plugins = $this->getPlugins();
foreach ($plugins as $id => $plugin) {
if (!method_exists($plugin, $methodName)) {
continue;
}
$results[$id] = $plugin->{$methodName}();
}
return $this->registrationMethodCache[$methodName] = $results;
}
// //
// Disability // Disability
// //