From 60ff81116b135989826c46d91ef7e22465804fe8 Mon Sep 17 00:00:00 2001 From: Bernhard Fischer Date: Thu, 9 Jan 2025 13:10:45 +0100 Subject: [PATCH] Improvement: Add field controllers for text and textarea and return text controller as fallback. #774 --- .../field/text/wbt_field_controller.php | 57 +++++++++++++++++++ .../field/textarea/wbt_field_controller.php | 57 +++++++++++++++++++ .../customfield/wbt_field_controller_info.php | 21 ++++++- 3 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 classes/local/customfield/field/text/wbt_field_controller.php create mode 100644 classes/local/customfield/field/textarea/wbt_field_controller.php diff --git a/classes/local/customfield/field/text/wbt_field_controller.php b/classes/local/customfield/field/text/wbt_field_controller.php new file mode 100644 index 0000000..231c776 --- /dev/null +++ b/classes/local/customfield/field/text/wbt_field_controller.php @@ -0,0 +1,57 @@ +. + +/** + * Extension of the customfield field controller for Wunderbyte table. + * + * @package local_wunderbyte_table + * @copyright 2024 Wunderbyte GmbH + * @author Bernhard Fischer-Sengseis + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_wunderbyte_table\local\customfield\field\text; + +// Important: Use the field controller for the right customfield. +use customfield_text\field_controller; +use local_wunderbyte_table\local\customfield\wbt_field_controller_base; + +/** + * Extension of the customfield field controller for Wunderbyte table. + * + * @package local_wunderbyte_table + * @copyright 2024 Wunderbyte GmbH + * @author Bernhard Fischer-Sengseis + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class wbt_field_controller extends field_controller implements wbt_field_controller_base { + + /** + * Get the actual string value of the customfield. + * + * @param string $key + * @return string the string value + */ + public function get_option_value_by_key(string $key): string { + global $DB; + + if (!empty($key)) { + // For normal text fields we need format_string. + return format_string($key); + } + return ''; + } +} diff --git a/classes/local/customfield/field/textarea/wbt_field_controller.php b/classes/local/customfield/field/textarea/wbt_field_controller.php new file mode 100644 index 0000000..abf0d1a --- /dev/null +++ b/classes/local/customfield/field/textarea/wbt_field_controller.php @@ -0,0 +1,57 @@ +. + +/** + * Extension of the customfield field controller for Wunderbyte table. + * + * @package local_wunderbyte_table + * @copyright 2024 Wunderbyte GmbH + * @author Bernhard Fischer-Sengseis + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_wunderbyte_table\local\customfield\field\textarea; + +// Important: Use the field controller for the right customfield. +use customfield_textarea\field_controller; +use local_wunderbyte_table\local\customfield\wbt_field_controller_base; + +/** + * Extension of the customfield field controller for Wunderbyte table. + * + * @package local_wunderbyte_table + * @copyright 2024 Wunderbyte GmbH + * @author Bernhard Fischer-Sengseis + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class wbt_field_controller extends field_controller implements wbt_field_controller_base { + + /** + * Get the actual string value of the customfield. + * + * @param string $key + * @return string the string value + */ + public function get_option_value_by_key(string $key): string { + global $DB; + + if (!empty($key)) { + // For textarea we need format_text. + return format_text($key); + } + return ''; + } +} diff --git a/classes/local/customfield/wbt_field_controller_info.php b/classes/local/customfield/wbt_field_controller_info.php index e46f1e5..71a441a 100644 --- a/classes/local/customfield/wbt_field_controller_info.php +++ b/classes/local/customfield/wbt_field_controller_info.php @@ -60,7 +60,8 @@ public static function create(stdClass $record) { if (class_exists($class)) { return new $class($record->id, $record); } - return null; + // By default, we return the text controller. + return new \local_wunderbyte_table\local\customfield\field\text\wbt_field_controller($record->id, $record); } /** @@ -96,7 +97,23 @@ public static function instantiate_by_shortnames(array $shortnames) { public static function get_instance_by_shortname(string $shortname) { if (!empty(self::$instances[$shortname])) { return self::$instances[$shortname]; + } else { + global $DB; + $sql = "SELECT cf.shortname AS filtercolumn, cf.* + FROM {customfield_field} cf + WHERE cf.shortname = :shortname"; + $params = ['shortname' => $shortname]; + if ($record = $DB->get_record_sql($sql, $params)) { + // We only add the instance, if a field controller exists. + if ($instance = self::create($record)) { + self::$instances[$record->shortname] = $instance; + return $instance; + } else { + return null; + } + } else { + return null; + } } - return null; } }