comparison core/modules/comment/src/CommentFieldItemList.php @ 0:4c8ae668cc8c

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