CommentGroupContentFormatter.php in Open Social 8.6
Same filename and directory in other branches
- 8.9 modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 8 modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 8.2 modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 8.3 modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 8.4 modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 8.5 modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 8.7 modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 8.8 modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 10.3.x modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 10.0.x modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 10.1.x modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
- 10.2.x modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.php
File
modules/custom/group_core_comments/src/Plugin/Field/FieldFormatter/CommentGroupContentFormatter.phpView source
<?php
namespace Drupal\group_core_comments\Plugin\Field\FieldFormatter;
use Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\group\Entity\GroupContent;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Link;
/**
* Plugin implementation of the 'comment_group_content' formatter.
*
* @FieldFormatter(
* id = "comment_group_content",
* label = @Translation("Comment on group content"),
* field_types = {
* "comment"
* }
* )
*/
class CommentGroupContentFormatter extends CommentDefaultFormatter {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$output = parent::viewElements($items, $langcode);
$entity = $items
->getEntity();
// Exclude entities without the set id.
if (!empty($entity
->id())) {
$group_contents = GroupContent::loadByEntity($entity);
}
if (!empty($group_contents)) {
// Add cache contexts.
$output['#cache']['contexts'][] = 'group.type';
$output['#cache']['contexts'][] = 'group_membership';
$account = \Drupal::currentUser();
$group = reset($group_contents)
->getGroup();
$group_url = $group
->toUrl('canonical', [
'language' => $group
->language(),
]);
$access_post_comments = $this
->getPermissionInGroups('post comments', $account, $group_contents, $output);
if ($access_post_comments
->isForbidden()) {
$description = $this
->t('You are not allowed to comment on content in a group you are not member of. You can join the group @group_link.', [
'@group_link' => Link::fromTextAndUrl($this
->t('here'), $group_url)
->toString(),
]);
$output[0]['comment_form'] = [
'#prefix' => '<hr>',
'#markup' => $description,
];
}
$access_view_comments = $this
->getPermissionInGroups('access comments', $account, $group_contents, $output);
if ($access_view_comments
->isForbidden()) {
$description = $this
->t('You are not allowed to view comments on content in a group you are not member of. You can join the group @group_link.', [
'@group_link' => Link::fromTextAndUrl($this
->t('here'), $group_url)
->toString(),
]);
unset($output[0]);
$output[0]['comments'] = [
'#markup' => $description,
];
}
}
return $output;
}
/**
* Checks if account was granted permission in group.
*/
protected function getPermissionInGroups($perm, AccountInterface $account, $group_contents, &$output) {
$renderer = \Drupal::service('renderer');
foreach ($group_contents as $group_content) {
$group = $group_content
->getGroup();
// Add cacheable dependency.
$membership = $group
->getMember($account);
$renderer
->addCacheableDependency($output, $membership);
if ($group
->hasPermission($perm, $account)) {
return AccessResult::allowed()
->cachePerUser();
}
}
// Fallback.
return AccessResult::forbidden()
->cachePerUser();
}
}
Classes
Name | Description |
---|---|
CommentGroupContentFormatter | Plugin implementation of the 'comment_group_content' formatter. |