I'm having trouble accessing the CiviCRM $form object on a Drupal site running CiviCRM.

In Drupal, I can use hook_form_alter() anywhere in code and make changes to any form object in Drupal. I'm trying to do the same thing for a CiviCRM form, but none of the functions or hooks seem to give me access to the $form object.

Also, I'm attempting to do this from a custom script with Drupal and CiviCRM bootstrapped. If someone could point me in the right direction, I would be most grateful.

[EDIT:] I've tried this, but can't get any data back for anything...

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';

eval(`cv php:boot`); // to bootstrap CiviCRM according to their documentation

function mycustomhook_civicrm_buildForm($formName, &$form) {

What am I missing?

  • On what form are you using this? Is mycustomhook module? Sep 24 '18 at 16:27
  • This code is sitting in a single separate PHP script, which will eventually accept a GET request from an AJAX call. The script should be able to edit the $form object before the form is submitted. It is intended as a way to copy jQuery changes to the DOM, back to the database when the form is submitted. The 'mycustomhook', is referencing the name of the theme in use, although not literally named "mycustomhook".
    – soupmagnet
    Sep 24 '18 at 16:48
  • I think you should use it in module or extension. I don't recon if its invoked through theme Sep 24 '18 at 17:13
  • 1
    I just tried doing this in a module and had the same results.
    – soupmagnet
    Sep 24 '18 at 17:53
  • If the form you rendering is a civi form which extends CRM_Core_Form then the hook should get invoked Sep 24 '18 at 17:54

@soupmagnet Welcome to CiviCRM world!

You can alter the form elements or add your own form element to any CiviCRM forms using CiviCRM buildForm hook. The documentation provided good example on how to do it. Please implement this hook either in CiviCRM extension or Drupal module.



  • I appreciate your input, but I've tried that (see my edit) and still not getting anything back.
    – soupmagnet
    Sep 24 '18 at 16:23

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.