From f9dd5b2f5746e360d28c36d843c59ee475b936c4 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Fri, 21 Oct 2016 07:15:08 +1100 Subject: [PATCH] Plugins can now define custom list column types --- modules/backend/widgets/Lists.php | 26 ++++++++++++++++++++ modules/system/classes/PluginBase.php | 10 ++++++++ modules/system/classes/PluginManager.php | 30 ++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/modules/backend/widgets/Lists.php b/modules/backend/widgets/Lists.php index fece182ea..1bd0434c0 100644 --- a/modules/backend/widgets/Lists.php +++ b/modules/backend/widgets/Lists.php @@ -12,6 +12,7 @@ use Carbon\Carbon; use October\Rain\Html\Helper as HtmlHelper; use October\Rain\Router\Helper as RouterHelper; use System\Helpers\DateTime as DateTimeHelper; +use System\Classes\PluginManager; use Backend\Classes\ListColumn; use Backend\Classes\WidgetBase; use ApplicationException; @@ -825,6 +826,9 @@ class Lists extends WidgetBase if (method_exists($this, 'eval'. studly_case($column->type) .'TypeValue')) { $value = $this->{'eval'. studly_case($column->type) .'TypeValue'}($record, $column, $value); } + else { + $value = $this->evalCustomListType($column->type, $record, $column, $value); + } /* * Apply default value. @@ -874,6 +878,28 @@ class Lists extends WidgetBase // 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 */ diff --git a/modules/system/classes/PluginBase.php b/modules/system/classes/PluginBase.php index fbfe12c5c..279033d41 100644 --- a/modules/system/classes/PluginBase.php +++ b/modules/system/classes/PluginBase.php @@ -181,6 +181,16 @@ class PluginBase extends ServiceProviderBase 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. * The templates must be returned in the following format: diff --git a/modules/system/classes/PluginManager.php b/modules/system/classes/PluginManager.php index adb16b51e..24c362461 100644 --- a/modules/system/classes/PluginManager.php +++ b/modules/system/classes/PluginManager.php @@ -55,6 +55,11 @@ class PluginManager */ protected $disabledPlugins = []; + /** + * @var array Cache of registration method results. + */ + protected $registrationMethodCache = []; + /** * @var boolean Prevent all plugins from registering or booting */ @@ -418,6 +423,31 @@ class PluginManager 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 //