social_post.module in Open Social 8
Same filename and directory in other branches
- 8.9 modules/social_features/social_post/social_post.module
- 8.2 modules/social_features/social_post/social_post.module
- 8.3 modules/social_features/social_post/social_post.module
- 8.4 modules/social_features/social_post/social_post.module
- 8.5 modules/social_features/social_post/social_post.module
- 8.6 modules/social_features/social_post/social_post.module
- 8.7 modules/social_features/social_post/social_post.module
- 8.8 modules/social_features/social_post/social_post.module
- 10.3.x modules/social_features/social_post/social_post.module
- 10.0.x modules/social_features/social_post/social_post.module
- 10.1.x modules/social_features/social_post/social_post.module
- 10.2.x modules/social_features/social_post/social_post.module
The Social post module.
File
modules/social_features/social_post/social_post.moduleView source
<?php
/**
* @file
* The Social post module.
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Access\AccessResult;
use Drupal\user\Entity\User;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Entity\Group;
/**
* Implements hook_form_FORM_ID_alter().
*/
function social_post_form_post_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_state
->getFormObject()
->getEntity()
->isNew()) {
// Load current user.
$account = User::load(Drupal::currentUser()
->id());
// Load compact notification view mode of the attached profile.
if ($account) {
$storage = \Drupal::entityTypeManager()
->getStorage('profile');
if (!empty($storage)) {
$user_profile = $storage
->loadByUser($account, 'profile');
if ($user_profile) {
$content = \Drupal::entityTypeManager()
->getViewBuilder('profile')
->view($user_profile, 'compact_notification');
// Add to a new field, so twig can render it.
$form['current_user_image'] = $content;
}
}
}
}
// Reset title display.
$form['field_post']['widget'][0]['#title_display'] = "";
// Set submit button caption to Post instead of Save.
$form['actions']['submit']['#value'] = t('Post', [], [
'context' => 'Post button',
]);
if (!empty($form['field_post']) && !empty($form['field_post']['widget'][0])) {
// For posting on the stream on the group stream.
if (!empty(_social_group_get_current_group())) {
$form['field_post']['widget'][0]['#placeholder'] = t('Say something to the group');
$form['field_post']['widget'][0]['#title'] = t('Say something to the group');
}
elseif (!empty(\Drupal::routeMatch()
->getParameter('user')) && \Drupal::routeMatch()
->getParameter('user')
->id() != \Drupal::currentUser()
->id()) {
$user_profile = \Drupal::routeMatch()
->getParameter('user');
$name = $user_profile
->getDisplayName();
$form['field_post']['widget'][0]['#placeholder'] = t('Leave a message to @name', [
'@name' => $name,
]);
$form['field_post']['widget'][0]['#title'] = t('Leave a message to @name', [
'@name' => $name,
]);
}
else {
$title = t('Say something to the Community');
$form['field_post']['widget'][0]['#title'] = $title;
$form['field_post']['widget'][0]['#placeholder'] = $title;
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Alter the comment_post_comment_form form.
*/
function social_post_form_comment_post_comment_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Reset title display.
$form['field_comment_body']['widget'][0]['#title_display'] = 'invisible';
// Set the action of the form to the current uri.
// This needs to be done, because we cannot override $form['#action']
// without breaking functionality.
$uri = Url::fromRoute('<current>')
->toString();
// Store in a hidden field.
$form['redirect_after_save'] = [
'#type' => 'hidden',
'#title' => t('Redir'),
'#default_value' => $uri,
];
// Submit function to retrieve the action uri and redirect to it.
$form['actions']['submit']['#submit'][] = '_social_post_comment_post_comment_form_submit';
}
/**
* Form submit for comment_post_comment_form.
*
* @param array $form
* Commnent on a post form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state interface.
*/
function _social_post_comment_post_comment_form_submit(array $form, FormStateInterface $form_state) {
// Fetch from the form.
$uri = $form_state
->getValue('redirect_after_save');
// Let's not trust this.
$uri = Html::escape($uri);
// Must be a valid URL.
if (UrlHelper::isValid($uri)) {
// Get the Url from the Form value and redirect to this url.
$url = Url::fromUserInput($uri);
// Set redirect.
$form_state
->setRedirectUrl($url);
}
}
/**
* Implements hook_views_data_alter().
*/
function social_post_views_data_alter(array &$data) {
$data['post']['post_visibility_access_filter'] = [
'title' => t('Post visibility access'),
'filter' => [
'title' => t('Post visibility access'),
'help' => t('Enable post access for public and community posts.'),
'field' => 'field_visibility',
'id' => 'post_visibility_access',
],
];
$data['post']['post_account_stream_filter'] = [
'title' => t('Post account stream'),
'filter' => [
'title' => t('Post account stream'),
'help' => t('Enable post for the account stream.'),
'field' => 'field_visibility',
'id' => 'post_account_stream',
],
];
$data['post']['post_group_stream_filter'] = [
'title' => t('Post group stream'),
'filter' => [
'title' => t('Post group stream'),
'help' => t('Enable post for the group stream.'),
'field' => 'field_visibility',
'id' => 'post_group_stream',
],
];
}
/**
* Implements hook_theme().
*/
function social_post_theme() {
$items = [
'post' => [
'render element' => 'elements',
'file' => 'post.page.inc',
'template' => 'post',
],
];
return $items;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function social_post_theme_suggestions_post(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#post'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'post__' . $sanitized_view_mode;
$suggestions[] = 'post__' . $entity
->bundle();
$suggestions[] = 'post__' . $entity
->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'post__' . $entity
->id();
$suggestions[] = 'post__' . $entity
->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Prepares variables for comment field templates.
*
* Default template: field--comment.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing render arrays for the list of
* comments, and the comment form. Array keys: comments, comment_form.
*
* @todo Rename to template_preprocess_field__comment() once
* https://www.drupal.org/node/939462 is resolved.
*/
function social_post_preprocess_field(array &$variables) {
$element = $variables['element'];
if ($element['#formatter'] == 'comment_post') {
// Create separate variables for the more_link.
$variables['more_link'] = $element[0]['more_link'];
}
}
/**
* Implements hook_ENTITY_TYPE_create_access().
*/
function social_post_comment_create_access(AccountInterface $account, array $context, $entity_bundle) {
if ($entity_bundle === 'post_comment') {
$group = \Drupal::routeMatch()
->getParameter('group');
if (is_numeric($group) && !$group instanceof GroupInterface) {
$group = Group::load($group);
}
if ($group) {
if ($group
->hasPermission('add post entities in group', $account)) {
return AccessResult::allowed()
->cachePerUser();
}
else {
return AccessResult::forbidden()
->cachePerUser();
}
}
}
return AccessResult::neutral();
}
Functions
Name | Description |
---|---|
social_post_comment_create_access | Implements hook_ENTITY_TYPE_create_access(). |
social_post_form_comment_post_comment_form_alter | Implements hook_form_FORM_ID_alter(). |
social_post_form_post_form_alter | Implements hook_form_FORM_ID_alter(). |
social_post_preprocess_field | Prepares variables for comment field templates. |
social_post_theme | Implements hook_theme(). |
social_post_theme_suggestions_post | Implements hook_theme_suggestions_HOOK(). |
social_post_views_data_alter | Implements hook_views_data_alter(). |
_social_post_comment_post_comment_form_submit | Form submit for comment_post_comment_form. |