Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\comment;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Access\AccessResult;
|
Chris@0
|
6 use Drupal\Core\Field\FieldItemList;
|
Chris@0
|
7 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Defines a item list class for comment fields.
|
Chris@0
|
11 */
|
Chris@0
|
12 class CommentFieldItemList extends FieldItemList {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * {@inheritdoc}
|
Chris@0
|
16 */
|
Chris@0
|
17 public function get($index) {
|
Chris@0
|
18 // The Field API only applies the "field default value" to newly created
|
Chris@0
|
19 // entities. In the specific case of the "comment status", though, we need
|
Chris@0
|
20 // this default value to be also applied for existing entities created
|
Chris@0
|
21 // before the comment field was added, which have no value stored for the
|
Chris@0
|
22 // field.
|
Chris@0
|
23 if ($index == 0 && empty($this->list)) {
|
Chris@0
|
24 $field_default_value = $this->getFieldDefinition()->getDefaultValue($this->getEntity());
|
Chris@0
|
25 return $this->appendItem($field_default_value[0]);
|
Chris@0
|
26 }
|
Chris@0
|
27 return parent::get($index);
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * {@inheritdoc}
|
Chris@0
|
32 */
|
Chris@0
|
33 public function offsetExists($offset) {
|
Chris@0
|
34 // For consistency with what happens in get(), we force offsetExists() to
|
Chris@0
|
35 // be TRUE for delta 0.
|
Chris@0
|
36 if ($offset === 0) {
|
Chris@0
|
37 return TRUE;
|
Chris@0
|
38 }
|
Chris@0
|
39 return parent::offsetExists($offset);
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * {@inheritdoc}
|
Chris@0
|
44 */
|
Chris@0
|
45 public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE) {
|
Chris@0
|
46 if ($operation === 'edit') {
|
Chris@0
|
47 // Only users with administer comments permission can edit the comment
|
Chris@0
|
48 // status field.
|
Chris@0
|
49 $result = AccessResult::allowedIfHasPermission($account ?: \Drupal::currentUser(), 'administer comments');
|
Chris@0
|
50 return $return_as_object ? $result : $result->isAllowed();
|
Chris@0
|
51 }
|
Chris@0
|
52 if ($operation === 'view') {
|
Chris@17
|
53 // Only users with "post comments" or "access comments" permission can
|
Chris@0
|
54 // view the field value. The formatter,
|
Chris@0
|
55 // Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter,
|
Chris@0
|
56 // takes care of showing the thread and form based on individual
|
Chris@0
|
57 // permissions, so if a user only has ‘post comments’ access, only the
|
Chris@0
|
58 // form will be shown and not the comments.
|
Chris@0
|
59 $result = AccessResult::allowedIfHasPermission($account ?: \Drupal::currentUser(), 'access comments')
|
Chris@0
|
60 ->orIf(AccessResult::allowedIfHasPermission($account ?: \Drupal::currentUser(), 'post comments'));
|
Chris@0
|
61 return $return_as_object ? $result : $result->isAllowed();
|
Chris@0
|
62 }
|
Chris@0
|
63 return parent::access($operation, $account, $return_as_object);
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 }
|