social_follow_content.module in Open Social 8.5
Same filename and directory in other branches
- 8.9 modules/social_features/social_follow_content/social_follow_content.module
- 8 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.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 Follow Content module.
File
modules/social_features/social_follow_content/social_follow_content.moduleView source
<?php
/**
* @file
* The Social Follow Content module.
*/
use Drupal\Core\Link;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\flag\Entity\Flag;
use Drupal\flag\Entity\Flagging;
use Drupal\flag\FlagInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function social_follow_content_event_enrollment_insert(EntityInterface $entity) {
social_follow_content_event_enrollment_follow($entity);
}
/**
* Implements hook_ENTITY_TYPE_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.
// Only logged in users can follow content.
$account = \Drupal::currentUser();
if (!$account
->isAuthenticated()) {
return;
}
$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);
}
}
}
}
/**
* Implements hook_social_user_account_header_account_links().
*
* Adds the "Following" link to the user menu.
*/
function social_follow_content_social_user_account_header_account_links(array $context) {
return [
'my_content' => [
'#type' => 'link',
'#attributes' => [
'title' => new TranslatableMarkup("View content I'm following"),
],
'#title' => new TranslatableMarkup('Following'),
'#weight' => 1000,
] + Url::fromRoute('view.following.following')
->toRenderArray(),
];
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
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,
]));
}
}
}
Functions
Name![]() |
Description |
---|---|
social_follow_content_comment_insert | Implements hook_ENTITY_TYPE_insert(). |
social_follow_content_event_enrollment_follow | Automatically follow content when user enrolls the event. |
social_follow_content_event_enrollment_insert | Implements hook_ENTITY_TYPE_insert(). |
social_follow_content_event_enrollment_update | Implements hook_ENTITY_TYPE_update(). |
social_follow_content_social_user_account_header_account_links | Implements hook_social_user_account_header_account_links(). |