Drupal 7 site:

We need to add data to certain donation pages. For instance, it should appear on id=1 but not on id=2. This includes some code and js.

I made a Main.extra.tpl and everything works fine when it appears on all pages, but I have not been able to restrict it to be included only on page id=1.

I read that you can do: {if $contributionPageId == "yourPageId"} Your custom messaging here {/if}

but wrapping my code in
{if $contributionPageId == "1"} {/if} doesn't work.

Is there a better way to do this?

Thanks, Rachel

Edit: For now I've modified Drupal's page.tpl.php with:

if ((arg(0) == 'civicrm' && arg(1) == 'contribute') && ($_GET['id'] == '1')):

which does the job - but I still believe there must be a better way.


There are two "better ways" you can try.

First, you can put a template override into a folder with the contribution page ID. So if the current override is at <overrides>/templates/CRM/Contribution/Form.php, then <overrides>/templates/CRM/Contribution/1/Form.php will only override page ID 1.

However, template overrides aren't very maintainable, and they're pretty hacky.

Instead, you can create an extension. You can use hook_civicrm_buildForm like so:

function myextension_civicrm_buildForm( $formName, &$form ) { 
  if ($formName == 'CRM_Contribution_Form_Main' ){
    CRM_Core_Resources::singleton()->addScriptFile('org.example.myextension', 'js/myjavascript.js');

This technique is covered in greater detail in the Resources Reference.

  • We'll be testing a bunch of pages, so separate template overrides for each page id would mean too many separate files to manage. Unless I can make the if statement in a civi template work, I'll need to keep my page.tpl.php hack in place until I have time to look at creating an extension - at least this way there is only 1 file to maintain. Thanks! – Rachel Dec 20 '17 at 21:06

Writing an extension is a better way of packaging this type of change.

As Jon pointed out, it's fairly easy with civix to create one and inject the js/css you need

However, it soon becomes a bit of a nightmare to try to handle the contribution page ids and on the top of having to track that id 42 has that lovely towel feature while id 84 has whatever special, you have users that start cloning and replacing and reshuffling stuff...

So assuming it's more than a quick hack, I would create a custom field (eg "layout" + a list of options and modify your extension so it reads this custom field value instead of the id to decide what to inject

And if you think whatever you are changing is generic enough, please consider submit it to our extension directory, and if not, making your repository public on github or somewhere would certainly help someone else facing a problem similar to yours


  • I needed a quick hack to get this up and running, and while my page.tpl.php fix is working, I'd like to keep the hack within CiviCRM. I think I might move it to an extension when there is more time. I am thinking of something along the lines of scanning for active contribution pages and then selecting which elements get added to them - perhaps checkboxes to attach certain elements to specific pages. This way there can be multiple options on multiple pages all managed from one location. It would also need to allow for the input of the text/html/css/js or the file to include. Interesting... – Rachel Dec 20 '17 at 21:02

While I know that an extension is a preferred method using the extra templates method works for me in some situations. (I also found it a great way to get my feet wet learning the possibilities of customizing and the API).

To answer why the {if $contributionPageId == "1"} {/if} code didn't work may be you need to wrap those variables in a literal tag.

From the documentation here: https://docs.civicrm.org/dev/en/latest/framework/templates/

One heads up … if your file contains Javascript (as it probably will), you will need to start your "extra" custom file with the Smarty {literal} tag and the tag. Then use {/literal}{$variable}{literal} if you need to use any Smarty code or variables. Finally, end the file with {/literal}.

  • I tried simplifying the Main.extra.tpl to just contain: {if $contributionPageId == "1"}Hello Workd {/if} and that does not display. It displays fine without the if statement. Any ideas? – Rachel Dec 20 '17 at 20:52

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.