Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / src / Plugin / Field / FieldType / CommentItem.php
1 <?php
2
3 namespace Drupal\comment\Plugin\Field\FieldType;
4
5 use Drupal\comment\CommentManagerInterface;
6 use Drupal\comment\Entity\CommentType;
7 use Drupal\Core\Field\FieldDefinitionInterface;
8 use Drupal\Core\Field\FieldStorageDefinitionInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Routing\UrlGeneratorTrait;
11 use Drupal\Core\TypedData\DataDefinition;
12 use Drupal\Core\Field\FieldItemBase;
13 use Drupal\Core\Session\AnonymousUserSession;
14
15 /**
16  * Plugin implementation of the 'comment' field type.
17  *
18  * @FieldType(
19  *   id = "comment",
20  *   label = @Translation("Comments"),
21  *   description = @Translation("This field manages configuration and presentation of comments on an entity."),
22  *   list_class = "\Drupal\comment\CommentFieldItemList",
23  *   default_widget = "comment_default",
24  *   default_formatter = "comment_default",
25  *   cardinality = 1,
26  * )
27  */
28 class CommentItem extends FieldItemBase implements CommentItemInterface {
29   use UrlGeneratorTrait;
30
31   /**
32    * {@inheritdoc}
33    */
34   public static function defaultStorageSettings() {
35     return [
36       'comment_type' => '',
37     ] + parent::defaultStorageSettings();
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public static function defaultFieldSettings() {
44     return [
45       'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
46       'per_page' => 50,
47       'form_location' => CommentItemInterface::FORM_BELOW,
48       'anonymous' => COMMENT_ANONYMOUS_MAYNOT_CONTACT,
49       'preview' => DRUPAL_OPTIONAL,
50     ] + parent::defaultFieldSettings();
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
57     $properties['status'] = DataDefinition::create('integer')
58       ->setLabel(t('Comment status'))
59       ->setRequired(TRUE);
60
61     $properties['cid'] = DataDefinition::create('integer')
62       ->setLabel(t('Last comment ID'));
63
64     $properties['last_comment_timestamp'] = DataDefinition::create('integer')
65       ->setLabel(t('Last comment timestamp'))
66       ->setDescription(t('The time that the last comment was created.'));
67
68     $properties['last_comment_name'] = DataDefinition::create('string')
69       ->setLabel(t('Last comment name'))
70       ->setDescription(t('The name of the user posting the last comment.'));
71
72     $properties['last_comment_uid'] = DataDefinition::create('integer')
73       ->setLabel(t('Last comment user ID'));
74
75     $properties['comment_count'] = DataDefinition::create('integer')
76       ->setLabel(t('Number of comments'))
77       ->setDescription(t('The number of comments.'));
78
79     return $properties;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public static function schema(FieldStorageDefinitionInterface $field_definition) {
86     return [
87       'columns' => [
88         'status' => [
89           'description' => 'Whether comments are allowed on this entity: 0 = no, 1 = closed (read only), 2 = open (read/write).',
90           'type' => 'int',
91           'default' => 0,
92         ],
93       ],
94       'indexes' => [],
95       'foreign keys' => [],
96     ];
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
103     $element = [];
104
105     $settings = $this->getSettings();
106
107     $anonymous_user = new AnonymousUserSession();
108
109     $element['default_mode'] = [
110       '#type' => 'checkbox',
111       '#title' => t('Threading'),
112       '#default_value' => $settings['default_mode'],
113       '#description' => t('Show comment replies in a threaded list.'),
114     ];
115     $element['per_page'] = [
116       '#type' => 'number',
117       '#title' => t('Comments per page'),
118       '#default_value' => $settings['per_page'],
119       '#required' => TRUE,
120       '#min' => 10,
121       '#max' => 1000,
122       '#step' => 10,
123     ];
124     $element['anonymous'] = [
125       '#type' => 'select',
126       '#title' => t('Anonymous commenting'),
127       '#default_value' => $settings['anonymous'],
128       '#options' => [
129         COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
130         COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
131         COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information'),
132       ],
133       '#access' => $anonymous_user->hasPermission('post comments'),
134     ];
135     $element['form_location'] = [
136       '#type' => 'checkbox',
137       '#title' => t('Show reply form on the same page as comments'),
138       '#default_value' => $settings['form_location'],
139     ];
140     $element['preview'] = [
141       '#type' => 'radios',
142       '#title' => t('Preview comment'),
143       '#default_value' => $settings['preview'],
144       '#options' => [
145         DRUPAL_DISABLED => t('Disabled'),
146         DRUPAL_OPTIONAL => t('Optional'),
147         DRUPAL_REQUIRED => t('Required'),
148       ],
149     ];
150
151     return $element;
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public static function mainPropertyName() {
158     return 'status';
159   }
160
161   /**
162    * {@inheritdoc}
163    */
164   public function isEmpty() {
165     // There is always a value for this field, it is one of
166     // CommentItemInterface::OPEN, CommentItemInterface::CLOSED or
167     // CommentItemInterface::HIDDEN.
168     return FALSE;
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
175     $element = [];
176
177     // @todo Inject entity storage once typed-data supports container injection.
178     //   See https://www.drupal.org/node/2053415 for more details.
179     $comment_types = CommentType::loadMultiple();
180     $options = [];
181     $entity_type = $this->getEntity()->getEntityTypeId();
182     foreach ($comment_types as $comment_type) {
183       if ($comment_type->getTargetEntityTypeId() == $entity_type) {
184         $options[$comment_type->id()] = $comment_type->label();
185       }
186     }
187     $element['comment_type'] = [
188       '#type' => 'select',
189       '#title' => t('Comment type'),
190       '#options' => $options,
191       '#required' => TRUE,
192       '#description' => $this->t('Select the Comment type to use for this comment field. Manage the comment types from the <a href=":url">administration overview page</a>.', [':url' => $this->url('entity.comment_type.collection')]),
193       '#default_value' => $this->getSetting('comment_type'),
194       '#disabled' => $has_data,
195     ];
196     return $element;
197   }
198
199   /**
200    * {@inheritdoc}
201    */
202   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
203     $statuses = [
204       CommentItemInterface::HIDDEN,
205       CommentItemInterface::CLOSED,
206       CommentItemInterface::OPEN,
207     ];
208     return [
209       'status' => $statuses[mt_rand(0, count($statuses) - 1)],
210     ];
211   }
212
213 }