social_follow_content.module in Open Social 8
Same filename and directory in other branches
- 8.9 modules/social_features/social_follow_content/social_follow_content.module
 - 8.2 modules/social_features/social_follow_content/social_follow_content.module
 - 8.3 modules/social_features/social_follow_content/social_follow_content.module
 - 8.4 modules/social_features/social_follow_content/social_follow_content.module
 - 8.5 modules/social_features/social_follow_content/social_follow_content.module
 - 8.6 modules/social_features/social_follow_content/social_follow_content.module
 - 8.7 modules/social_features/social_follow_content/social_follow_content.module
 - 8.8 modules/social_features/social_follow_content/social_follow_content.module
 - 10.3.x modules/social_features/social_follow_content/social_follow_content.module
 - 10.0.x modules/social_features/social_follow_content/social_follow_content.module
 - 10.1.x modules/social_features/social_follow_content/social_follow_content.module
 - 10.2.x modules/social_features/social_follow_content/social_follow_content.module
 
The Social core module.
File
modules/social_features/social_follow_content/social_follow_content.moduleView source
<?php
/**
 * @file
 * The Social core module.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\flag\Entity\Flagging;
/**
 * Implements hook_ENTIT_YTYPE_insert().
 */
function social_follow_content_event_enrollment_insert(EntityInterface $entity) {
  social_follow_content_event_enrollment_follow($entity);
}
/**
 * Implements hook_ENTIT_YTYPE_update().
 */
function social_follow_content_event_enrollment_update(EntityInterface $entity) {
  social_follow_content_event_enrollment_follow($entity);
}
/**
 * Automatically follow content when user enrolls the event.
 */
function social_follow_content_event_enrollment_follow(EntityInterface $entity) {
  if ($entity->field_enrollment_status->value) {
    // Check if user already follow this content.
    $account = \Drupal::currentUser();
    $properties = [
      'uid' => $account
        ->id(),
      'entity_type' => 'node',
      'entity_id' => $entity->field_event->target_id,
      'flag_id' => 'follow_content',
    ];
    $flaggings = \Drupal::entityTypeManager()
      ->getStorage('flagging')
      ->loadByProperties($properties);
    $flagging = reset($flaggings);
    if (empty($flagging)) {
      $flagging = Flagging::create($properties);
      if ($flagging) {
        $flagging
          ->save();
        $message = t('You have successfully enrolled to this event. Also you are now following the event, which means you will receive notifications when a new comment is placed.');
        drupal_set_message($message);
      }
    }
  }
}Functions
| 
            Name | 
                  Description | 
|---|---|
| social_follow_content_event_enrollment_follow | Automatically follow content when user enrolls the event. | 
| social_follow_content_event_enrollment_insert | Implements hook_ENTIT_YTYPE_insert(). | 
| social_follow_content_event_enrollment_update | Implements hook_ENTIT_YTYPE_update(). |