You are here

function social_follow_content_comment_insert in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_follow_content/social_follow_content.module \social_follow_content_comment_insert()
  2. 8.4 modules/social_features/social_follow_content/social_follow_content.module \social_follow_content_comment_insert()
  3. 8.6 modules/social_features/social_follow_content/social_follow_content.module \social_follow_content_comment_insert()
  4. 8.7 modules/social_features/social_follow_content/social_follow_content.module \social_follow_content_comment_insert()
  5. 8.8 modules/social_features/social_follow_content/social_follow_content.module \social_follow_content_comment_insert()
  6. 10.3.x modules/social_features/social_follow_content/social_follow_content.module \social_follow_content_comment_insert()
  7. 10.0.x modules/social_features/social_follow_content/social_follow_content.module \social_follow_content_comment_insert()
  8. 10.1.x modules/social_features/social_follow_content/social_follow_content.module \social_follow_content_comment_insert()
  9. 10.2.x modules/social_features/social_follow_content/social_follow_content.module \social_follow_content_comment_insert()

Implements hook_ENTITY_TYPE_insert().

File

modules/social_features/social_follow_content/social_follow_content.module, line 86
The Social Follow Content module.

Code

function social_follow_content_comment_insert(EntityInterface $entity) {

  /** @var \Drupal\comment\CommentInterface $entity */
  $entity = $entity
    ->getCommentedEntity();
  if (!$entity instanceof NodeInterface) {
    return;
  }
  $types = [];
  \Drupal::moduleHandler()
    ->alter('social_follow_content_types', $types);
  if (empty($types)) {
    return;
  }
  $flag = Flag::load('follow_content');
  if ($flag instanceof FlagInterface) {

    /** @var \Drupal\flag\FlagService $service */
    $service = \Drupal::service('flag');
    if (empty($service
      ->getFlagging($flag, $entity))) {
      $service
        ->flag($flag, $entity);

      // Get the unfollow link for this content.
      $url = Url::fromRoute('flag.action_link_unflag', [
        'flag' => $flag
          ->id(),
        'entity_id' => $entity
          ->id(),
      ]);

      // Set the real CSRF token.
      $url
        ->setOptions([
        'query' => [
          'token' => \Drupal::csrfToken()
            ->get($url
            ->getInternalPath()),
        ],
      ]);
      $flag_link = Link::fromTextAndUrl(t('Unfollow this content'), $url)
        ->toString();
      drupal_set_message(t('You are now automatically following this @type. @unfollow_link.', [
        '@type' => $entity
          ->getType(),
        '@unfollow_link' => $flag_link,
      ]));
    }
  }
}