1

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:

<tr id="my-field-tr">
  <td>Parent Event:</td>
  <td>{$form.my_field.html}</td>
</tr>
{* Reposition the above block after the Event Type field *}
<script type="text/javascript">
  cj('#my-field-tr').insertAfter('.crm-event-manage-eventinfo-form-block-event_type_id')
</script>

But I am struggling to get the field to appear. TIA

5
  • Could you try adding a complete table? I usually have trouble too just adding a <tr> with jQuery but adding a table works fine. Nov 5, 2019 at 13:22
  • 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? Nov 5, 2019 at 14:05
  • 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();
    – Demerit
    Nov 5, 2019 at 16:16
  • I got the clue from here - 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 Nov 5, 2019 at 16:35
  • 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. Nov 5, 2019 at 17:38

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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