3

Drupal 7 / CiviCRM 4.5.8 Working on a site which has several contribution pages. We want to leave the majority of them accessible to anonymous users, but have just one that we want to restrict to "members-only".

In Drupal the $form variable passed in hook_form_alter() will have a '#access' parameter that can be set to false.

What's the best way to accomplish this with a CiviCRM form? Is there something that can be modified in the $form variable using hook_civicrm_buildForm()?

Some better way?

2
  • How is "members-only" defined here? Is it folks with a current CiviMember record? Commented Apr 7, 2015 at 16:16
  • "Members Only" is based on Drupal user role, I want to know more generally how to programmatically deny access to a single contribution page Commented Apr 7, 2015 at 16:26

1 Answer 1

3

Here's a quick and dirty drupal-ly way of doing it, but I still wonder about a CiviCRM specific methodology.

function civicrm_custom_access_civicrm_buildForm($formName, &$form) {
  global $user;

  $grant_access = 0;
  foreach($user->roles as $key => $role_name) {
    if($key == 3 || $key == 5 || $key == 4 || $key == 6 || $key == 7) {
      $grant_access = 1;
    } 
  }

  if($formName == 'CRM_Contribute_Form_Contribution_Main' && $form->_id == 2 && !$grant_access) {
    drupal_access_denied();
    drupal_exit();
  }

}
3
  • +1 for this approach, which since it uses hooks could also be done in a CiviCRM extension. Pretty much what I was going to suggest - intercept in hook_civicrm_buildForm(), deny access if user access conditions aren't met. Commented Apr 8, 2015 at 3:41
  • Sorry if this is a really stupid question -- but where would this code snippet be placed? Commented Dec 14, 2015 at 21:32
  • this bit of code implements hook_civicrm_buildForm().wiki.civicrm.org/confluence/display/CRMDOC/… this example my module name was civicrm_custom_access ....So place this bit of code in a small module...with the function name YOUR_MODULE_NAME_civicrm_buildForm() Commented Dec 15, 2015 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.