CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • add hook_civicrm_preSave brfore form mainProcess, make any sense?
Pages: [1]

Author Topic: add hook_civicrm_preSave brfore form mainProcess, make any sense?  (Read 603 times)

jimyhuang

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 3
add hook_civicrm_preSave brfore form mainProcess, make any sense?
October 22, 2012, 10:09:15 am
I tried to add participant serial number base on event to a custom field in seperated drupal module. And this value will be included in the mail when event confirm message enabled. But I struggle on this for a day to find out a clean way to implement this without hacking core ...

I tried hook_civicrm_pre / hook_civicrm_post. Unfortunately, these hook only called when hitting database, but after saving string into database, the thank-you page and confirm email still have no updated value(because value is in session).

I tried hook_civicrm_postProcess. Unfortunately, mail always send before hook_civicrm_postProcess triggered ( in register form action ).

I tried hook_civicrm_validate. Unfortunately, I can't get any post value in session in validate state. And this won't be effect the thank-you page and email.

I'm not sure why manipulate a string before form save and show correct value to user is so hard .... then I start to digg core.

Finally, I add a new hook before mainProcess, and using that hook to inject quickform value in session. Lucky result.

Patch of add civicrm_preSave hook:
https://github.com/jimyhuang/civicrm_participant_serial/blob/master/add-civicrm-hook.patch

Code of module to add serial number to use exists custom field.
https://github.com/jimyhuang/civicrm_participant_serial/blob/master/civicrm_participant_serial.module
Code: [Select]
function hook_civicrm_preSave($form_name, &$form){
....
            $session_id = '_'.$form->controller->_name.'_container';

            foreach($_SESSION[$session_id]['values'] as $k => $v){
              if(isset($v['custom_'.$cid])){
                $count++;
                $query = "SELECT t.{$dao->column_name} as serial FROM {$dao->table_name} t INNER JOIN civicrm_participant p ON p.id = t.entity_id WHERE p.event_id = {$event_id} AND p.is_test = {$is_test} ORDER BY t.{$dao->column_name} DESC";
                $sid = CRM_Core_DAO::singleValueQuery($query);
                if($sid){
                  $sid+=$count;
                }
                else{
                  $sid = 0 + $count;
                }
                $_SESSION[$session_id]['values'][$k]['custom_'.$cid] = $sid;
              }
            }
...

Not sure if this is a correct approach. Need feedback or suggestion for this approach.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: add hook_civicrm_preSave brfore form mainProcess, make any sense?
October 22, 2012, 12:05:06 pm
So I think I understand the proposal. To put it in terms of this document:

http://wiki.civicrm.org/confluence/display/CRMDOC42/QuickForm+Reference

You're saying that the "Process" phase should have a new step which calls a hook *before* doing any business logic (ie right before the "CRM_Core_Form::postProcess" step), right? I think that makes sense. It's a little tricky to give that hook a good name... how about "hook_civicrm_processForm"? Then, from the perspective of a module developer, the sequence of hooks would be:

Code: [Select]
hook_civicrm_buildForm => hook_civicrm_validateForm => hook_civicrm_processForm => hook_civicrm_postProcess

jimyhuang

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 3
Re: add hook_civicrm_preSave brfore form mainProcess, make any sense?
October 23, 2012, 09:22:09 am
Hello totten,

Thanks for the comment. Name of "presave" borrowed from drupal. Maybe have better name for that.
http://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/hook_node_presave/7

Another concern is I still have no good method to modify validated data in quickform session before mainProcess. So in my code, I access session directly. I don't think that is a good way.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • add hook_civicrm_preSave brfore form mainProcess, make any sense?

This forum was archived on 2017-11-26.