3 namespace Drupal\comment\Plugin\Field\FieldType;
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;
16 * Plugin implementation of the 'comment' field type.
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"
27 class CommentItem extends FieldItemBase implements CommentItemInterface {
28 use UrlGeneratorTrait;
33 public static function defaultStorageSettings() {
36 ] + parent::defaultStorageSettings();
42 public static function defaultFieldSettings() {
44 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
46 'form_location' => CommentItemInterface::FORM_BELOW,
47 'anonymous' => COMMENT_ANONYMOUS_MAYNOT_CONTACT,
48 'preview' => DRUPAL_OPTIONAL,
49 ] + parent::defaultFieldSettings();
55 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
56 $properties['status'] = DataDefinition::create('integer')
57 ->setLabel(t('Comment status'))
60 $properties['cid'] = DataDefinition::create('integer')
61 ->setLabel(t('Last comment ID'));
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.'));
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.'));
71 $properties['last_comment_uid'] = DataDefinition::create('integer')
72 ->setLabel(t('Last comment user ID'));
74 $properties['comment_count'] = DataDefinition::create('integer')
75 ->setLabel(t('Number of comments'))
76 ->setDescription(t('The number of comments.'));
84 public static function schema(FieldStorageDefinitionInterface $field_definition) {
88 'description' => 'Whether comments are allowed on this entity: 0 = no, 1 = closed (read only), 2 = open (read/write).',
101 public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
104 $settings = $this->getSettings();
106 $anonymous_user = new AnonymousUserSession();
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.'),
114 $element['per_page'] = [
116 '#title' => t('Comments per page'),
117 '#default_value' => $settings['per_page'],
123 $element['anonymous'] = [
125 '#title' => t('Anonymous commenting'),
126 '#default_value' => $settings['anonymous'],
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'),
132 '#access' => $anonymous_user->hasPermission('post comments'),
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'],
139 $element['preview'] = [
141 '#title' => t('Preview comment'),
142 '#default_value' => $settings['preview'],
144 DRUPAL_DISABLED => t('Disabled'),
145 DRUPAL_OPTIONAL => t('Optional'),
146 DRUPAL_REQUIRED => t('Required'),
156 public static function mainPropertyName() {
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.
173 public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
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();
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();
186 $element['comment_type'] = [
188 '#title' => t('Comment type'),
189 '#options' => $options,
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,
201 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
203 CommentItemInterface::HIDDEN,
204 CommentItemInterface::CLOSED,
205 CommentItemInterface::OPEN,
208 'status' => $statuses[mt_rand(0, count($statuses) - 1)],