Calculate value for custom field on updating issue

General discussion about MantisBT Plugins

Moderators: Developer, Contributor

Post Reply
pollevr
Posts: 28
Joined: 25 Jun 2020, 15:12

Calculate value for custom field on updating issue

Post by pollevr »

Hi all,

in Mantis v. 2.24.1 I'm trying to write a plugin for setting a value calculated automatically, based on the values of other fields or custom fields.

In my plugin there are following code:

Code: Select all

class MyPluginPlugin extends MantisPlugin {
	function register() {
		...
	}
	function hooks(){
		return array(
			'EVENT_MYPLUGIN_UPDATE'      => 'update',
		);
	}
	function events() {
		return array(
			'EVENT_MYPLUGIN_UPDATE'      => EVENT_UPDATE_BUG,
		);
	}
	function update( $p_issue_id, $p_bug_data, $p_bugnote_text ) {
		$t_id = custom_field_get_id_from_name('<my_custom_field_name>');
		custom_field_set_value( $t_id , $p_issue_id, <my_new_value>); 
		return $p_bug_data;
	}
	...
}
When I update an issue I'll expect that the custom field <my_custom_field_name> will save with the <my_new_value>.

Why not?


Thanks all,
Cristian
Starbuck
Posts: 219
Joined: 14 Feb 2006, 02:53
Location: USA
Contact:

Re: Calculate value for custom field on updating issue

Post by Starbuck »

EVENT_UPDATE_BUG (Execute) : "This event allows plugins to perform post-processing of the bug data structure after being updated."
-- https://www.mantisbt.org/docs/master/en ... gle-plain/
If you want Mantis to work differently, use or create a plugin. Visit the Plugins forums.
Ask developers to create a plugin that you need - and motivate them to help you!
pollevr
Posts: 28
Joined: 25 Jun 2020, 15:12

Re: Calculate value for custom field on updating issue

Post by pollevr »

Sure,

so I'm expect that before it saves the record and after call my custom function update, so I get the old value and I can put the new value.

No?
Starbuck
Posts: 219
Joined: 14 Feb 2006, 02:53
Location: USA
Contact:

Re: Calculate value for custom field on updating issue

Post by Starbuck »

Yes ... if I understand you correctly. From what I understand about what you're doing, you need the events for after user data has been entered, and before it has been posted to the database. See details under:
https://www.mantisbt.org/docs/master/en ... ction.html

You are handling this event:
EVENT_UPDATE_BUG (Execute)
This event allows plugins to perform post-processing of the bug data structure after being updated.
I think you want these events:
EVENT_REPORT_BUG_DATA (Chain)
This event allows plugins to perform pre-processing of the new bug data structure after being reported from the user, but before the data is saved to the database. At this point, the issue ID is not yet known, as the data has not yet been persisted.

EVENT_UPDATE_BUG_DATA (Chain)
This event allows plugins to perform pre-processing of the updated bug data structure after being modified by the user, but before being saved to the database.
Then (I believe) your custom field data will be saved with the actual bug item.

Also look at the type of the event : Execute or Chain. The parameters and return values are different.

For EVENT_UPDATE_BUG, you don't get the ID, bug data, and note text. The doc says
Parameters
<Complex>: Original bug data structure (see core/bug_api.php)
<Complex>: Updated bug data structure (see core/bug_api.php)
And be sure that the data you're saving matches the specs for the custom field. For example, don't allow saving a category that isn't listed in =categories. See chapter 7 of the developer's guide.

In summary:

Use the right event for your purpose.
Check the specs for the inbound/outbound parameters.

Does that help?
If you want Mantis to work differently, use or create a plugin. Visit the Plugins forums.
Ask developers to create a plugin that you need - and motivate them to help you!
pollevr
Posts: 28
Joined: 25 Jun 2020, 15:12

Re: Calculate value for custom field on updating issue

Post by pollevr »

Hi Starbuck,

thanks for your patience.

Your logical reasoning is correct, but my problem is technical.

If my code is:

Code: Select all

	function init() {
		plugin_event_hook( 'EVENT_UPDATE_BUG_DATA', 'updateBugData' );
	}

	function updateBugData( $p_bug_data_updated, $p_bug_data_original ) {
		error_log("^^^original summary:".$p_bug_data_original->summary."^^^", 0);
		error_log("^^^updated summary:".$p_bug_data_updated->summary."^^^", 0);
		return $p_bug_data_updated;
	}
The first error_log write a blank value (and not the new value of summary field as I expeted), instead the second error_log write the new value of summary field (and not the original value), so I don't understand my mistake and I'm confused.

What's wrong?
Why $p_bug_data_updated->summary is blank or null?
pollevr
Posts: 28
Joined: 25 Jun 2020, 15:12

Re: Calculate value for custom field on updating issue

Post by pollevr »

Hi Starbuck,

now I understood, and my code is:

Code: Select all

	function updateBugData( $p_event, $p_bug_data_updated, $p_bug_data_original ) {
		var_dump($p_event);  //print the EVENT
		var_dump($p_bug_data_updated); //print the NEW data structure
		var_dump($p_bug_data_original); //print the ORIGINAL data structure
		...
		return $p_bug_data_updated;
	}
Now my target is modify and update some custom fields, but here I don't see any custom field... how can I do this?

I did this:

Code: Select all

	function updateBugData( $p_event, $p_bug_data_updated, $p_bug_data_original ) {
		...
		$bugDataId = $p_bug_data_updated->id;
		$myCustomVariable = <new_custom_value>;
		$myCustomFieldId = custom_field_get_id_from_name('<name_of_my_custom_field');
		custom_field_set_value( $mycustomFieldId , $bugDataId, $myCustomVariable);
		...
	}
But the new custom value not saved.

Thank you,
Cristian
pollevr
Posts: 28
Joined: 25 Jun 2020, 15:12

Re: Calculate value for custom field on updating issue

Post by pollevr »

Found it!

For custom fields I call a new function for event EVENT_UPDATE_BUG, so the custom fields will save.

Thank you for your support.


Bye,
Cristian
tet-com@freemail.hu
Posts: 22
Joined: 07 Feb 2019, 11:44

Re: Calculate value for custom field on updating issue

Post by tet-com@freemail.hu »

Hi Christian, where can I found and download these plugin?
Post Reply