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