Extension loads and I added the following hook to switch demographics template:

function myext_civicrm_alterTemplateFile( $formName, &$form, $context, &$tplName) {
switch($tplName) {
    case "CRM/Contact/Form/Edit/Demographics.tpl":
        $template = CRM_Core_Smarty::singleton();
        if ($template->template_exists($mytpl)){


I think this worked before. What did I wrong? I put a breakpoint with my debugger or let it run to cursor position, but it never even gets there, so I can't figure out what's wrong.

  • Can you put a breakpoint (or a debug statement) above the switch statement, to make sure that the hook is being called? It sounds like it might not be. Feb 27 '16 at 1:28
  • Well that's what I did. The hook was never called when creating / editing a contact. I discovered that hook_civicrm_alterContent does get called and I will use that instead, but that doesn't explain why alterTemplateFile was not called.
    – ñull
    Feb 29 '16 at 9:14

I use alter template file in 4.7.2. It has been working great for me.

I needed to switch out the contact editor form based on contact subtype. One important thing I found if doing this programically was the clear the template cache.

function customcomponent_civicrm_alterTemplateFile($formName, &$form, $context, &$tplName) {

    // swith contact template based on contact type
    switch ($formName) {
        case 'CRM_Contact_Form_Contact':

            $clear = true;

            if ($form->_contactType!='Individual'||empty($form->_contactSubType)) break;

            // there are some weird non printable chars on contact sub type
            $contactSubType = preg_replace('/\W/i','',$form->_contactSubType);

            // switch out the form based on contact type
            $newTpl = 'CRM/Contact_'.$form->_contactSubType.'/Form/Contact.tpl';
            $newDir = __DIR__ .'/templatesconditional/ContactSubType_'.$contactSubType;


    // clear out old templates -- found CRM_Admin_Form_Setting_UpdateConfigBackend::postProcess -> CRM_Core_Config::cleanup
    if (!empty($clear)) {
        $config = CRM_Core_Config::singleton();
        CRM_Utils_File::cleanDir($config->templateCompileDir, false);

    // switch out the templates
    $template = CRM_Core_Smarty::singleton();
    if (!empty($newTpl)&&$template->template_exists($newTpl)) $tplName = $newTpl;
    if (!empty($newDir)&&is_dir($newDir)) $template->addTemplateDir($newDir);

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.