You are here

social_follow_content.module in Open Social 8

The Social core module.

File

modules/social_features/social_follow_content/social_follow_content.module
View 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

Namesort descending 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().