Answers.Body: <p>Here's the code I wrote to achieve my goal ...</p>
  
  <pre><code>function mymodule_form_alter(&amp;$form, &amp;$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' =&gt; array('min_range' =&gt; 1)))) {
          $form['submitted']['mailing_list']['civicrm_1_contact_1_other_group']['#default_value'] = array($params['gid']);
        }
      }
    }
  }
  </code></pre>
  
  <p>Details ...</p>
  
  <ul>
  <li>Check if it's the right form</li>
  <li>Check that I want to allow the default to be overwritten (via a CSS class attached to the field)</li>
  <li>Check if the Group Id is supplied as an argument, and is valid</li>
  <li>Set the default group to the one supplied</li>
  </ul>
  
  <p>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</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-11T13:59:06.107Z
Answers.Text: 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!
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-12T04:03:09.430Z
Answers.Text: Thanks for the heads up, @coleman! In my test environment, I'm not seeing that my contact is being unsubscribed from other groups.
Answers.UserDisplayName: Ken West
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-12T04:13:13.667Z
Answers.Text: The gotcha @Coleman is referring to is described at https://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.
Answers.UserDisplayName: Ken West
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-11T05:13:33.597Z
Answers.LastActivityDate: 2017-08-11T05:13:33.597Z
Answers.UserDisplayName: Ken West
Answers.Body: <p>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:</p>
  
  <p>First add Groups to your form in the webform tab:</p>
  
  <p><a href="https://i.sstatic.net/1Wc1K.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1Wc1K.png" alt="enter image description here"></a></p>
  
  <p>Hit Edit on the Webform Element (labeled Group):</p>
  
  <p><a href="https://i.sstatic.net/Yi0Fl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Yi0Fl.png" alt="enter image description here"></a></p>
  
  <p>On the form it looks like this - and you can even change the Label for Group(s) to Newsletter(s)</p>
  
  <p><a href="https://i.sstatic.net/1tYXs.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1tYXs.png" alt="enter image description here"></a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-10T04:50:16.027Z
Answers.Text: 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.
Answers.UserDisplayName: Ken West
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-10T05:19:33.087Z
Answers.Text: 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?
Answers.UserDisplayName: KarinG - Semper IT
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-10T06:59:10.590Z
Answers.Text: 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.
Answers.UserDisplayName: Ken West
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-10T13:01:58.723Z
Answers.Text: 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?
Answers.UserDisplayName: KarinG - Semper IT
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-10T03:50:36.157Z
Answers.LastActivityDate: 2017-08-10T03:50:36.157Z
Answers.UserDisplayName: KarinG - Semper IT
Body: <p>I'm using Drupal, CiviCRM, CiviCRM Entity and Webform CiviCRM.</p>
  
  <p>My hope is to link an "activity" (described on a Drupal node) to a group ("Sign up for news about this activity").</p>
  
  <p>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.</p>
  
  <p>Is there a way of doing that with Webform CiviCRM?</p>
  
  <p>I've tried setting "Enable Tags and Groups Fields" to "Yes" but I can't see how to pass in the Group Id. </p>
  
  <p>Cheers!</p>
  
Tags: <drupal><webform-civicrm>
Title: Subscribe to group via a webform - pass in group Id
ThreadText: # Subscribe to group via a webform - pass in group Id
  
  (Asked by: username_0 on 2017-08-10)
  
  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!
  
  **Tags:** drupal, webform-civicrm
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2017-08-11. Score: 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
  
  > **Comments:**
  > **username_1**: 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!  
  > **username_0**: Thanks for the heads up, @username_1! In my test environment, I'm not seeing that my contact is being unsubscribed from other groups.  
  > **username_0**: The gotcha @username_1 is referring to is described at https://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.  
  
  ---
  
  ### Answer by username_2 on 2017-08-10. Score: 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](https://i.sstatic.net/1Wc1K.png)](https://i.sstatic.net/1Wc1K.png)
  
  Hit Edit on the Webform Element (labeled Group):
  
  [![enter image description here](https://i.sstatic.net/Yi0Fl.png)](https://i.sstatic.net/Yi0Fl.png)
  
  On the form it looks like this - and you can even change the Label for Group(s) to Newsletter(s)
  
  [![enter image description here](https://i.sstatic.net/1tYXs.png)](https://i.sstatic.net/1tYXs.png)
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_2**: 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?  
  > **username_0**: 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.  
  > **username_2**: 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?  
  
  ---
  
  