Answers.Body: <p>I use alter template file in 4.7.2. It has been working great for me.</p>
  
  <p>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. </p>
  
  <pre><code>function customcomponent_civicrm_alterTemplateFile($formName, &amp;$form, $context, &amp;$tplName) {
  
      // swith contact template based on contact type
      switch ($formName) {
          case 'CRM_Contact_Form_Contact':
  
              $clear = true;
  
              if ($form-&gt;_contactType!='Individual'||empty($form-&gt;_contactSubType)) break;
  
              // there are some weird non printable chars on contact sub type
              $contactSubType = preg_replace('/\W/i','',$form-&gt;_contactSubType);
  
              // switch out the form based on contact type
              $newTpl = 'CRM/Contact_'.$form-&gt;_contactSubType.'/Form/Contact.tpl';
              $newDir = __DIR__ .'/templatesconditional/ContactSubType_'.$contactSubType;
  
              break;
      }
  
      // clear out old templates -- found CRM_Admin_Form_Setting_UpdateConfigBackend::postProcess -&gt; CRM_Core_Config::cleanup
      if (!empty($clear)) {
          $config = CRM_Core_Config::singleton();
          CRM_Utils_File::cleanDir($config-&gt;templateCompileDir, false);
          CRM_Utils_File::createDir($config-&gt;templateCompileDir);
      }
  
      // switch out the templates
      $template = CRM_Core_Smarty::singleton();
      if (!empty($newTpl)&amp;&amp;$template-&gt;template_exists($newTpl)) $tplName = $newTpl;
      if (!empty($newDir)&amp;&amp;is_dir($newDir)) $template-&gt;addTemplateDir($newDir);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-01T07:12:22.757Z
Answers.LastActivityDate: 2016-03-01T07:12:22.757Z
Answers.UserDisplayName: David Hayes
Body: <p>Extension loads and I added the following hook to switch demographics template:</p>
  
  <pre><code>function myext_civicrm_alterTemplateFile( $formName, &amp;$form, $context, &amp;$tplName) {
  switch($tplName) {
      case "CRM/Contact/Form/Edit/Demographics.tpl":
        $mytpl='../templates/CRMContactFormEditDemographics.tpl';
          $template = CRM_Core_Smarty::singleton();
          if ($template-&gt;template_exists($mytpl)){
            $tplName=$mytpl;
          }
      break;
    }    
  
  
  }
  </code></pre>
  
  <p>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. </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-02-27T01:28:14.457Z
Comments.Text: 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.
Comments.UserDisplayName: Jon G - Megaphone Tech
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-02-29T09:14:30.003Z
Comments.Text: 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.
Comments.UserDisplayName: ñull
Tags: <extension><hooks><debugging><custom-templates>
Title: Switch template in extension
ThreadText: # Switch template in extension
  
  (Asked by: username_0 on 2016-02-26)
  
  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":
        $mytpl='../templates/CRMContactFormEditDemographics.tpl';
          $template = CRM_Core_Smarty::singleton();
          if ($template->template_exists($mytpl)){
            $tplName=$mytpl;
          }
      break;
    }    
  
  }
  
  ```
  
  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.
  
  **Tags:** extension, hooks, debugging, custom-templates
  
  > **Comments:**
  > **username_1**: 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.  
  > **username_0**: 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.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2016-03-01. Score: 1
  
  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;
  
              break;
      }
  
      // 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);
          CRM_Utils_File::createDir($config->templateCompileDir);
      }
  
      // 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);
  }
  
  ```
  
  
  ---
  
  