Answers.Body: <p>Digging into the hook reference documentation at <a href="https://docs.civicrm.org/dev/en/master/hooks/hook_civicrm_buildForm/" rel="nofollow noreferrer">https://docs.civicrm.org/dev/en/master/hooks/hook_civicrm_buildForm/</a> I can see that the correct approach is</p>
  
  <pre><code>if ($form-&gt;_eventId == 1) {
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-23T12:26:40.257Z
Answers.LastActivityDate: 2017-03-23T12:26:40.257Z
Answers.UserDisplayName: Graham
Answers.Body: <p>As Graham mentioned this is the correct approach:</p>
  
  <pre><code>if ($form-&gt;_eventId == 1) {
  </code></pre>
  
  <p>It is possible that _eventId is protected, so you can not access it directly. In that case use</p>
  
  <pre><code>$eventId = $form-&gt;getVar('_eventId');
  if ($eventId == 1) {
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-23T13:45:34.213Z
Answers.LastActivityDate: 2017-03-23T13:45:34.213Z
Answers.UserDisplayName: ErikH - CiviCooP
Body: <p>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 (<a href="http://www.jackrabbithanna.com/articles/easy-jquery-modificaiton-civicrm-forms" rel="nofollow noreferrer">http://www.jackrabbithanna.com/articles/easy-jquery-modificaiton-civicrm-forms</a>) has been most helpful in getting me much of the way.</p>
  
  <p>That guidance talks about targeting a specific contribution page by using:</p>
  
  <pre><code>    function tctaextend_civicrm_buildForm($formName, &amp;$form) {
  if($formName=='CRM_Contribute_Form_Contribution_Main'){
       if($form-&gt;_id==2 ){
        CRM_Core_Resources::singleton()-&gt;addScriptFile('com.skvare.testextend', 'my_alter_contribform.js');
       }
     }
  }
  </code></pre>
  
  <p>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.:</p>
  
  <pre><code>    function myextension_civicrm_buildForm($formName, &amp;$form) {
  if($formName=='CRM_Event_Form_Registration_Register'){
       if($form-&gt;_id==1 ){
        CRM_Core_Resources::singleton()-&gt;addScriptFile('com.example.myextension', 'my_alter_eventform.js');
       }
     }
  }
  </code></pre>
  
  <p>But that generates a php notice:</p>
  
  <blockquote>
    <p>Notice: Undefined property: CRM_Event_Form_Registration_Register::$_id in myextension_civicrm_buildForm() </p>
  </blockquote>
  
  <p>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.</p>
  
  <p>If I remove the line</p>
  
  <pre><code> if($form-&gt;_id==1 ){
  </code></pre>
  
  <p>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?</p>
  
Tags: <extension><hooks>
Title: How to target a specific event registration form in an extension
ThreadText: # How to target a specific event registration form in an extension
  
  (Asked by: username_0 on 2017-03-23)
  
  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?
  
  **Tags:** extension, hooks
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2017-03-23. Score: 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) {
  
  
  ---
  
  ### Answer by username_1 on 2017-03-23. Score: 1
  
  As username_0 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) {
  
  
  ---
  
  