0

I'm trying to use a custom extension rather than using a template override (template_name.extra.tpl) to implement some jQuery on an event registration form (essentially to hide some form fields unless the right radio button is selected). Jackrabbithanna's useful guide here (http://www.jackrabbithanna.com/articles/easy-jquery-modificaiton-civicrm-forms) has been most helpful in getting me much of the way.

That guidance talks about targeting a specific contribution page by using:

    function tctaextend_civicrm_buildForm($formName, &$form) {
if($formName=='CRM_Contribute_Form_Contribution_Main'){
     if($form->_id==2 ){
      CRM_Core_Resources::singleton()->addScriptFile('com.skvare.testextend', 'my_alter_contribform.js');
     }
   }
}

In my case I want to target a particular event. The form name is CRM_Event_Form_Registration_Register, so I thought I could do a straight replacement, e.g.:

    function myextension_civicrm_buildForm($formName, &$form) {
if($formName=='CRM_Event_Form_Registration_Register'){
     if($form->_id==1 ){
      CRM_Core_Resources::singleton()->addScriptFile('com.example.myextension', 'my_alter_eventform.js');
     }
   }
}

But that generates a php notice:

Notice: Undefined property: CRM_Event_Form_Registration_Register::$_id in myextension_civicrm_buildForm()

And also throws a fatal error if I try to create a new contribution page, even though the hook function is aiming to impact only event registration forms.

If I remove the line

 if($form->_id==1 ){

Then no errors, but my injected jQuery will presumably impact every event registration form rather than just the event I want to target? Where am I going wrong?

2

Digging into the hook reference documentation at https://docs.civicrm.org/dev/en/master/hooks/hook_civicrm_buildForm/ I can see that the correct approach is

if ($form->_eventId == 1) {
1

As Graham mentioned this is the correct approach:

if ($form->_eventId == 1) {

It is possible that _eventId is protected, so you can not access it directly. In that case use

$eventId = $form->getVar('_eventId');
if ($eventId == 1) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.