CommentItem.php 6.51 KB
Newer Older
1 2
<?php

3
namespace Drupal\comment\Plugin\Field\FieldType;
4

5
use Drupal\comment\CommentInterface;
6
use Drupal\comment\CommentManagerInterface;
7
use Drupal\comment\Entity\CommentType;
8
use Drupal\Core\Field\FieldDefinitionInterface;
9
use Drupal\Core\Field\FieldStorageDefinitionInterface;
10
use Drupal\Core\Form\FormStateInterface;
11
use Drupal\Core\TypedData\DataDefinition;
12
use Drupal\Core\Field\FieldItemBase;
13
use Drupal\Core\Session\AnonymousUserSession;
14
use Drupal\Core\Url;
15 16 17 18 19 20 21 22

/**
 * Plugin implementation of the 'comment' field type.
 *
 * @FieldType(
 *   id = "comment",
 *   label = @Translation("Comments"),
 *   description = @Translation("This field manages configuration and presentation of comments on an entity."),
23
 *   list_class = "\Drupal\comment\CommentFieldItemList",
24
 *   default_widget = "comment_default",
25 26
 *   default_formatter = "comment_default",
 *   cardinality = 1,
27 28
 * )
 */
29
class CommentItem extends FieldItemBase implements CommentItemInterface {
30 31 32

  /**
   * {@inheritdoc}
33
   */
34
  public static function defaultStorageSettings() {
35
    return [
36
      'comment_type' => '',
37
    ] + parent::defaultStorageSettings();
38 39 40 41 42
  }

  /**
   * {@inheritdoc}
   */
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' => CommentInterface::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 59
      ->setLabel(t('Comment status'))
      ->setRequired(TRUE);
60 61 62

    $properties['cid'] = DataDefinition::create('integer')
      ->setLabel(t('Last comment ID'));
63

64 65 66
    $properties['last_comment_timestamp'] = DataDefinition::create('integer')
      ->setLabel(t('Last comment timestamp'))
      ->setDescription(t('The time that the last comment was created.'));
67

68 69 70
    $properties['last_comment_name'] = DataDefinition::create('string')
      ->setLabel(t('Last comment name'))
      ->setDescription(t('The name of the user posting the last comment.'));
71

72 73
    $properties['last_comment_uid'] = DataDefinition::create('integer')
      ->setLabel(t('Last comment user ID'));
74

75 76 77
    $properties['comment_count'] = DataDefinition::create('integer')
      ->setLabel(t('Number of comments'))
      ->setDescription(t('The number of comments.'));
78

79
    return $properties;
80 81 82 83 84
  }

  /**
   * {@inheritdoc}
   */
85
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
86 87 88
    return [
      'columns' => [
        'status' => [
89 90 91
          'description' => 'Whether comments are allowed on this entity: 0 = no, 1 = closed (read only), 2 = open (read/write).',
          'type' => 'int',
          'default' => 0,
92 93 94 95 96
        ],
      ],
      'indexes' => [],
      'foreign keys' => [],
    ];
97 98 99 100 101
  }

  /**
   * {@inheritdoc}
   */
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 111 112 113
      '#type' => 'checkbox',
      '#title' => t('Threading'),
      '#default_value' => $settings['default_mode'],
      '#description' => t('Show comment replies in a threaded list.'),
114 115
    ];
    $element['per_page'] = [
116
      '#type' => 'number',
117 118
      '#title' => t('Comments per page'),
      '#default_value' => $settings['per_page'],
119
      '#required' => TRUE,
120
      '#min' => 1,
121
      '#max' => 1000,
122 123
    ];
    $element['anonymous'] = [
124 125 126
      '#type' => 'select',
      '#title' => t('Anonymous commenting'),
      '#default_value' => $settings['anonymous'],
127
      '#options' => [
128 129 130
        CommentInterface::ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
        CommentInterface::ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
        CommentInterface::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 136 137
      '#type' => 'checkbox',
      '#title' => t('Show reply form on the same page as comments'),
      '#default_value' => $settings['form_location'],
138 139
    ];
    $element['preview'] = [
140 141 142
      '#type' => 'radios',
      '#title' => t('Preview comment'),
      '#default_value' => $settings['preview'],
143
      '#options' => [
144 145 146
        DRUPAL_DISABLED => t('Disabled'),
        DRUPAL_OPTIONAL => t('Optional'),
        DRUPAL_REQUIRED => t('Required'),
147 148
      ],
    ];
149 150 151 152

    return $element;
  }

153 154 155 156 157 158 159
  /**
   * {@inheritdoc}
   */
  public static function mainPropertyName() {
    return 'status';
  }

160 161 162 163
  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
164 165 166
    // There is always a value for this field, it is one of
    // CommentItemInterface::OPEN, CommentItemInterface::CLOSED or
    // CommentItemInterface::HIDDEN.
167 168 169
    return FALSE;
  }

170 171 172
  /**
   * {@inheritdoc}
   */
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 181 182 183 184 185
    $entity_type = $this->getEntity()->getEntityTypeId();
    foreach ($comment_types as $comment_type) {
      if ($comment_type->getTargetEntityTypeId() == $entity_type) {
        $options[$comment_type->id()] = $comment_type->label();
      }
    }
186
    $element['comment_type'] = [
187 188 189
      '#type' => 'select',
      '#title' => t('Comment type'),
      '#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' => Url::fromRoute('entity.comment_type.collection')->toString()]),
192 193
      '#default_value' => $this->getSetting('comment_type'),
      '#disabled' => $has_data,
194
    ];
195 196 197
    return $element;
  }

198 199 200 201 202 203 204 205 206 207 208 209 210 211
  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $statuses = [
      CommentItemInterface::HIDDEN,
      CommentItemInterface::CLOSED,
      CommentItemInterface::OPEN,
    ];
    return [
      'status' => $statuses[mt_rand(0, count($statuses) - 1)],
    ];
  }

212
}