Answers.Body: <p>I think you can filter the views using CONTEXTUAL FILTERS. Add group custom field in CONTEXTUAL FILTERS set default value to use below php code</p>
  
  <pre><code>    civicrm_initialize();
      try {
        $result = civicrm_api3('Contact', 'getsingle', [
          'return' =&gt; ["groups"],
          'id' =&gt; "user_contact_id",
        ]);
        return $result['group'];
      }
      catch (Exception $e) {
        return 0;
      }
  </code></pre>
  
  <p><a href="https://i.sstatic.net/mg6OX.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/mg6OX.png" alt="enter image description here"></a></p>
  
  <p>Cheers</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-13T14:36:55.113Z
Answers.Text: it seems to work. But I've no results. How you reference groups to event custom field?
Answers.UserDisplayName: bass
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-13T14:57:51.837Z
Answers.Text: I guess you may need to figure out the return statement if logged in user in subscribed to more than 1 group .
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-13T15:18:51.543Z
Answers.Text: Sorry, perhaps I miss some thing.  Logged in user can be in more than 1 group and, I want to show all events. I think I'm wrong when I link the costum field to the group.
Answers.UserDisplayName: bass
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-13T12:08:03.977Z
Answers.LastActivityDate: 2019-06-13T12:08:03.977Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I have CiviCRM 5.12 on drupal 7. I want to create a drupal view containing the next events that are printed if only the contact belongs to an authorized group. That is, when I create an event, I link it to a group with a custom field and in the view, I want to filter the events and keep those reserved for the group containing the contact corresponding to the user drupal.  Please can you help me ?</p>
  
Tags: <civievent><custom-fields><groups>
Title: Listing events that only belong to a group
ThreadText: # Listing events that only belong to a group
  
  (Asked by: username_0 on 2019-06-13)
  
  I have CiviCRM 5.12 on drupal 7. I want to create a drupal view containing the next events that are printed if only the contact belongs to an authorized group. That is, when I create an event, I link it to a group with a custom field and in the view, I want to filter the events and keep those reserved for the group containing the contact corresponding to the user drupal. Please can you help me ?
  
  **Tags:** civievent, custom-fields, groups
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-06-13. Score: 1
  
  I think you can filter the views using CONTEXTUAL FILTERS. Add group custom field in CONTEXTUAL FILTERS set default value to use below php code
  
  ```
      civicrm_initialize();
      try {
        $result = civicrm_api3('Contact', 'getsingle', [
          'return' => ["groups"],
          'id' => "user_contact_id",
        ]);
        return $result['group'];
      }
      catch (Exception $e) {
        return 0;
      }
  
  ```
  
  [![enter image description here](https://i.sstatic.net/mg6OX.png)](https://i.sstatic.net/mg6OX.png)
  
  Cheers
  
  username_1
  
  > **Comments:**
  > **username_0**: it seems to work. But I've no results. How you reference groups to event custom field?  
  > **username_1**: I guess you may need to figure out the return statement if logged in user in subscribed to more than 1 group .  
  > **username_0**: Sorry, perhaps I miss some thing.  Logged in user can be in more than 1 group and, I want to show all events. I think I'm wrong when I link the costum field to the group.  
  
  ---
  
  