3

I'm using Drupal, CiviCRM, CiviCRM Entity and Webform CiviCRM.

My hope is to link an "activity" (described on a Drupal node) to a group ("Sign up for news about this activity").

So the Group Id would be stored in a field on the Drupal node, and when I click "Sign up" I'm taken to a form where I can sign up to that group.

Is there a way of doing that with Webform CiviCRM?

I've tried setting "Enable Tags and Groups Fields" to "Yes" but I can't see how to pass in the Group Id.

Cheers!

3

Here's the code I wrote to achieve my goal ...

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form['#id'], 'webform-client-form-') === 0) {
    if (in_array(
      'default-via-parameter',
      $form['submitted']['mailing_list']['civicrm_1_contact_1_other_group']['#attributes']['class'])
    ) {
      $params = drupal_get_query_parameters();
      if (filter_var($params['gid'], FILTER_VALIDATE_INT, array('options' => array('min_range' => 1)))) {
        $form['submitted']['mailing_list']['civicrm_1_contact_1_other_group']['#default_value'] = array($params['gid']);
      }
    }
  }
}

Details ...

  • Check if it's the right form
  • Check that I want to allow the default to be overwritten (via a CSS class attached to the field)
  • Check if the Group Id is supplied as an argument, and is valid
  • Set the default group to the one supplied

The form can be used without supplying a default to choose any group. If a default is supplied the user can use this, or even override it

  • That looks good, just be careful of one "gotcha" which is that if the user is already a member of other groups, this will remove them! – Coleman Aug 11 '17 at 13:59
  • Thanks for the heads up, @coleman! In my test environment, I'm not seeing that my contact is being unsubscribed from other groups. – Ken West Aug 12 '17 at 4:03
  • The gotcha @Coleman is referring to is described at civicrm.stackexchange.com/a/17139/169 and in my case I'm offering a form that doesn't get it's 'contact 1' from an existing contact. – Ken West Aug 12 '17 at 4:13
2

Why not just use webform_civicrm? You can inject Markup elements on a webform to dress up you page; and then with Groups -> - User Select - you can get complete control over what Groups people can select from and you can customize their label:

First add Groups to your form in the webform tab:

enter image description here

Hit Edit on the Webform Element (labeled Group):

enter image description here

On the form it looks like this - and you can even change the Label for Group(s) to Newsletter(s)

enter image description here

  • Karin, that's what I'm doing. What I'm wanting to do is control which of the groups are selected when the form loads, ideally by passing in the Group Id as a URL parameter. – Ken West Aug 10 '17 at 4:50
  • Why not let the user select? If you want to you could have a page with buttons -> Newsletter A click here etc and then have a different webform to for each of your Newsletters - but why not show them all in one form and let the user decide which one(s they can select from a preselected list - that you've decided on in your webform Element? – KarinG - Semper IT Aug 10 '17 at 5:19
  • Karin, there are too many groups to make that a nice UX for the user. I could have N webforms each with M options, but that starts being a maintenance headache. – Ken West Aug 10 '17 at 6:59
  • You have M times N different Newsletter Groups? (you can remove all non Newsletter groups from the UI from being listed in the Webform element); How many Newsletter groups do you have? There is no way to pass &gid into a webform - but if there was - people would have to go to all webforms one at a time to subscribe if they wanted to subscribe to more than one? – KarinG - Semper IT Aug 10 '17 at 13:01

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.