Answers.Body: <p>There are multiple ways to do this. One is to use a .extra.tpl which has to have the same path and first part of the name as the original .tpl where you want it to get inserted. If you name it that way the tpl file will get picked up automatically. So in this case the original .tpl that controls that event screen is in templates/CRM/Event/Form/ManageEvent/EventInfo.tpl, so you want to make a file templates/CRM/Event/Form/ManageEvent/EventInfo.extra.tpl</p>
  
  <p>Another way is like in your original question but then you're missing the line from the docs (<a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/" rel="nofollow noreferrer">https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/</a>) that says</p>
  
  <pre><code>CRM_Core_Region::instance('page-body')-&gt;add(array(
    'template' =&gt; "{$templatePath}/testfield.tpl"
  ));
  </code></pre>
  
  <p>to tell it the path and name of your template file. Otherwise it doesn't know about your .tpl file.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-06T00:23:25.220Z
Answers.LastActivityDate: 2019-11-06T00:23:25.220Z
Answers.UserDisplayName: Demerit
Body: <p>I'm trying to add a custom field to the Event form programmatically.</p>
  
  <p>So far, I've created a new extension and enabled it, with hook_civicrm_buildForm function in a PHP file. The function's being executed as I can get it stop at a breakpoint. I'm using the addEntityRef method to add a new field.</p>
  
  <pre><code>/**
   * Implements hook_civicrm_buildForm()
   * 
   * Adds a field for linking a parent event
   *
   * @param string $formName
   * @param CRM_Event_Form_ManageEvent_EventInfo $form
   * @return void
   */
  function myExtension_civicrm_buildForm($formName, &amp;$form) {
  
    if ($formName == 'CRM_Event_Form_ManageEvent_EventInfo') {
  
      $form-&gt;addEntityRef('my_field', ts('My Field'), [
        'entity' =&gt; 'Event',
        'placeholder' =&gt; ts('- any -'),
        'select' =&gt; ['minimumInputLength' =&gt; 0],
      ]);
  
      $form-&gt;buildQuickForm();
    }
  }
  </code></pre>
  
  <p>I've followed other posts by adding a template file named my_field.tpl in my extension's templates directory with the contents:</p>
  
  <pre><code>&lt;tr id="my-field-tr"&gt;
    &lt;td&gt;Parent Event:&lt;/td&gt;
    &lt;td&gt;{$form.my_field.html}&lt;/td&gt;
  &lt;/tr&gt;
  {* Reposition the above block after the Event Type field *}
  &lt;script type="text/javascript"&gt;
    cj('#my-field-tr').insertAfter('.crm-event-manage-eventinfo-form-block-event_type_id')
  &lt;/script&gt;
  </code></pre>
  
  <p>But I am struggling to get the field to appear.
  TIA</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-05T13:22:38.437Z
Comments.Text: Could you try adding a complete table? I usually have trouble too just adding a <tr> with jQuery but adding a table works fine.
Comments.UserDisplayName: ErikH - CiviCooP
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-05T14:05:11.057Z
Comments.Text: I tried but no luck, I'm thinking is it something to do with the tabs that are on CiviEvents. Like would I have to tell it which tab I want the field to be on or would the markup repositioning do that for me?
Comments.UserDisplayName: AlbionBrown
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-05T16:16:04.153Z
Comments.Text: Just wondering where you saw that the file should be named my_field.tpl? My thinking is it would be named templates/CRM/Event/Form/ManageEvent/EventInfo.extra.tpl. And I don't think you need to call `$form->buildQuickForm();`
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-05T16:35:44.023Z
Comments.Text: I got the clue from here - https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/
  I also didn't have any luck adding those templates/CRM/Event/Form/ManageEvent/ directories and renaming the file ExtraInfo.extra.tpl
Comments.UserDisplayName: AlbionBrown
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-05T17:38:17.483Z
Comments.Text: Ah it did actually work! I had an extra . in my file name! Thanks so much for the suggestion. Do you know why it has to be in that format? Also, are you able to submit an answer? Because I'll make it as correct.
Comments.UserDisplayName: AlbionBrown
Tags: <civievent>
Title: Can't add custom field to form
ThreadText: # Can't add custom field to form
  
  (Asked by: username_0 on 2019-11-05)
  
  I'm trying to add a custom field to the Event form programmatically.
  So far, I've created a new extension and enabled it, with hook_civicrm_buildForm function in a PHP file. The function's being executed as I can get it stop at a breakpoint. I'm using the addEntityRef method to add a new field.
  /**
   * Implements hook_civicrm_buildForm()
   * 
   * Adds a field for linking a parent event
   *
   * @param string $formName
   * @param CRM_Event_Form_ManageEvent_EventInfo $form
   * @return void
   */
  function myExtension_civicrm_buildForm($formName, &$form) {
  
    if ($formName == 'CRM_Event_Form_ManageEvent_EventInfo') {
  
      $form->addEntityRef('my_field', ts('My Field'), [
        'entity' => 'Event',
        'placeholder' => ts('- any -'),
        'select' => ['minimumInputLength' => 0],
      ]);
  
      $form->buildQuickForm();
    }
  }
  
  I've followed other posts by adding a template file named my_field.tpl in my extension's templates directory with the contents:
  
    Parent Event:
    {$form.my_field.html}
  
  {* Reposition the above block after the Event Type field *}
  
  
  But I am struggling to get the field to appear.
  TIA
  
  **Tags:** civievent
  
  > **Comments:**
  > **username_1**: Could you try adding a complete table? I usually have trouble too just adding a <tr> with jQuery but adding a table works fine.  
  > **username_0**: I tried but no luck, I'm thinking is it something to do with the tabs that are on CiviEvents. Like would I have to tell it which tab I want the field to be on or would the markup repositioning do that for me?  
  > **username_2**: Just wondering where you saw that the file should be named my_field.tpl? My thinking is it would be named templates/CRM/Event/Form/ManageEvent/EventInfo.extra.tpl. And I don't think you need to call `$form->buildQuickForm();`  
  > **username_0**: I got the clue from here - https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/ I also didn't have any luck adding those templates/CRM/Event/Form/ManageEvent/ directories and renaming the file ExtraInfo.extra.tpl  
  > **username_0**: Ah it did actually work! I had an extra . in my file name! Thanks so much for the suggestion. Do you know why it has to be in that format? Also, are you able to submit an answer? Because I'll make it as correct.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-11-06. Score: 2 | Accepted answer
  
  There are multiple ways to do this. One is to use a .extra.tpl which has to have the same path and first part of the name as the original .tpl where you want it to get inserted. If you name it that way the tpl file will get picked up automatically. So in this case the original .tpl that controls that event screen is in templates/CRM/Event/Form/ManageEvent/EventInfo.tpl, so you want to make a file templates/CRM/Event/Form/ManageEvent/EventInfo.extra.tpl
  Another way is like in your original question but then you're missing the line from the docs (https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/) that says
  CRM_Core_Region::instance('page-body')->add(array(
    'template' => "{$templatePath}/testfield.tpl"
  ));
  
  to tell it the path and name of your template file. Otherwise it doesn't know about your .tpl file.
  
  
  ---
  
  