Veröffentlicht
2009-09-07 06:21
A while back, Lobo alluded to some recent modifications that allowed Joomla and Standalone users to more easily tap into CiviCRM's hooks (issue detailed here). Hooks provide a powerful way to modify/customize Civi functionality without hacking core, thus making future upgrade paths much easier. And in recent months, the core team and others have used the blog to detail some creative ways to use hooks for some really great advanced functionality, such as creating event discounts via coupon codes.
A quick word of background and explanation re: these recent changes --
Hooks *have* been available to Joomla for some time, but not without making a few hacks to the Joomla core and then referencing the hooks file(s) in the Civi settings file -- clearly not ideal functionality, as the whole goal is to avoid making modifications to core code files. The recent improvements allow you to tap into hooks without any changes to Joomla or CiviCRM.
For you Joomla (and standalone) users out there who are interested in using hooks, here's a quick primer on what you need to do, as the method is slightly different from how Drupalers implement them.
Your hooks code should be in a file named civicrmHooks.php, and saved to the CiviCRM root directory, which is /administrator/components/com_civicrm/civicrm/. If you have setup a php override directory location (strongly recommended), you may save it there (configure the location in Administer CiviCRM > Global Settings > Directories). This is suggested as it keeps the file outside of the core folders (again, helping to ensure smoother future upgrades). I typically create my override directory as: /media/civicrm/customphp/, and drop the file there.
The second requirement is that in the file itself, all hook functions must be prefixed with "joomla_" rather than "_hook".
The only limitation to hook functionality in Joomla/Standalone is that you can only have one hook implementation per installation (unlike Drupal where multiple modules can implement the same hook and do different things). This should not be a huge issue, as it can be overcome through conditional clauses in the code, though eventually it would be good to have the full flexibility available.
So here's a simple example. I had a contribution page (id=1) on a site and wanted the country field in the billing block to have the default value set to the US. Here's my civicrmHook.php file content:
function joomla_civicrm_buildForm( $formName, &$form ) { if ( $formName == 'CRM_Contribute_Form_Contribution_Main' && $form->getVar( '_id' ) == 1 ) { $defaults['country_id-5'] = 1228; $form->setDefaults( $defaults ); } }
Filed under
Comments
Just a follow-up to this. If you are using standalone rather than Joomla, then the hook structure is:
standalone_civicrm_buildForm($formName, &$form) {
// Do something awesome.
}
And like Brian said, all the same rules apply. Best to use a custom PHP override directory outside of the civicrm/ directory (and outside of your web directory, too).
--Andrew