View Issue Details

IDProjectCategoryView StatusLast Update
0009030mantisbtcustom fieldspublic2008-08-12 09:36
Reporterocto Assigned Tovboctor  
PrioritynormalSeverityminorReproducibilityN/A
Status closedResolutionwon't fix 
Product Version1.1.1 
Summary0009030: [PATCH] Add a custom function to format custom fields
Description

The attached patch introduces a callback (custom function) to pretty-print the value of a custom field. An email with the patch has been sent to mantisbt-dev on Friday, March 28th.

Additional Information

In my case I've added a custom field names `Git-Commit' and I want to display the first line of the commit message along with the hexadecimal id. The possibilities are endless, though.

Tagspatch
Attached Files
mantis-1.1.1-custom_field_format.patch (3,616 bytes)   
diff -pur mantis-1.1.1.orig/core/custom_field_api.php mantis-1.1.1.octo/core/custom_field_api.php
--- mantis-1.1.1.orig/core/custom_field_api.php	2007-10-14 00:36:41.000000000 +0200
+++ mantis-1.1.1.octo/core/custom_field_api.php	2008-03-28 11:57:56.000000000 +0100
@@ -1266,30 +1266,12 @@
 
 	# --------------------
 	# Prepare a string containing a custom field value for display
-	# $p_def 		contains the definition of the custom field
 	# $p_field_id 	contains the id of the field
 	# $p_bug_id		contains the bug id to display the custom field value for
 	# NOTE: This probably belongs in the string_api.php
-	function string_custom_field_value( $p_def, $p_field_id, $p_bug_id ) {
-		$t_custom_field_value = custom_field_get_value( $p_field_id, $p_bug_id );
-		switch( $p_def['type'] ) {
-			case CUSTOM_FIELD_TYPE_EMAIL:
-				return "<a href=\"mailto:$t_custom_field_value\">$t_custom_field_value</a>";
-				break;
-			case CUSTOM_FIELD_TYPE_ENUM:
-			case CUSTOM_FIELD_TYPE_LIST:
-			case CUSTOM_FIELD_TYPE_MULTILIST:
-			case CUSTOM_FIELD_TYPE_CHECKBOX:
-				return str_replace( '|', ', ', $t_custom_field_value );
-				break;
-			case CUSTOM_FIELD_TYPE_DATE:
-				if ($t_custom_field_value != null) {
-					return date( config_get( 'short_date_format'), $t_custom_field_value) ;
-				}
-				break ;
-			default:
-				return string_display_links( $t_custom_field_value );
-		}
+	function custom_field_formatted_value( $p_field_id, $p_bug_id ) {
+		return helper_call_custom_function( 'custom_field_format',
+			array ($p_field_id, $p_bug_id) );
 	}
 
 	# --------------------
@@ -1299,7 +1281,7 @@
 	# $p_bug_id		contains the bug id to display the custom field value for
 	# NOTE: This probably belongs in the print_api.php
 	function print_custom_field_value( $p_def, $p_field_id, $p_bug_id ) {
-		echo string_custom_field_value( $p_def, $p_field_id, $p_bug_id );
+		echo custom_field_formatted_value( $p_field_id, $p_bug_id );
 	}
 
 	# --------------------
diff -pur mantis-1.1.1.orig/core/custom_function_api.php mantis-1.1.1.octo/core/custom_function_api.php
--- mantis-1.1.1.orig/core/custom_function_api.php	2007-10-14 00:36:41.000000000 +0200
+++ mantis-1.1.1.octo/core/custom_function_api.php	2008-03-28 11:57:56.000000000 +0100
@@ -392,4 +392,34 @@
 	# html_api.php.  For each button, this function needs to generate the enclosing '<td>' and '</td>'.
 	function custom_function_default_print_bug_view_page_custom_buttons( $p_bug_id ) {
 	}
+
+	# --------------------
+	# This function formats the value of a custom field for displaying in 
+	# a web page. This function is called from 
+	# core/custom_field_api.php:custom_field_formatted_value, so don't
+	# call this function when overriding 'custom_field_format' or you'll 
+	# end up with a deep recursion.
+	function custom_function_default_custom_field_format( $p_field_id, $p_bug_id ) {
+		$t_field_type = custom_field_type ($p_field_id);
+		$t_field_value = custom_field_get_value ($p_field_id, $p_bug_id);
+
+		switch ($t_field_type) {
+			case CUSTOM_FIELD_TYPE_EMAIL:
+				return "<a href=\"mailto:$t_field_value\">$t_field_value</a>";
+				break;
+			case CUSTOM_FIELD_TYPE_ENUM:
+			case CUSTOM_FIELD_TYPE_LIST:
+			case CUSTOM_FIELD_TYPE_MULTILIST:
+			case CUSTOM_FIELD_TYPE_CHECKBOX:
+				return str_replace( '|', ', ', $t_field_value );
+				break;
+			case CUSTOM_FIELD_TYPE_DATE:
+				if ($t_field_value != null) {
+					return date( config_get( 'short_date_format'), $t_field_value) ;
+				}
+				break ;
+			default:
+				return string_display_links( $t_field_value );
+		}
+	} # custom_function_default_custom_field_format
 ?>

Relationships

related to 0008314 acknowledged Support for extensible embedding objects like YouTube and data like customer data 

Activities

vboctor

vboctor

2008-04-03 22:53

manager   ~0017546

This work allows controlling the displaying but not the entering / updating of custom field values. You should get feedback from grangeway and daryn relating to how this patch fits in with their changes.

I've the following comments on the patch:

  1. custom_function_default_custom_field_format() should take in the def as a parameter. This data used to be passed in to the original functions is will avoid us having to re-fetch the data, if we already have it.

  2. custom_function_default_custom_field_format() - not all branches return a value and some paths "return" and then "break".

  3. The spacing around () doesn't comply with Mantis standards. Complying examples are: func( param ) or switch ( var ).