CommentItem.php 6.63 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\comment\Plugin\Field\FieldType\CommentItem.
6 7
 */

8
namespace Drupal\comment\Plugin\Field\FieldType;
9

10
use Drupal\comment\CommentManagerInterface;
11
use Drupal\comment\Entity\CommentType;
12
use Drupal\Core\Field\FieldStorageDefinitionInterface;
13
use Drupal\Core\Form\FormStateInterface;
14
use Drupal\Core\TypedData\DataDefinition;
15
use Drupal\Core\Field\FieldItemBase;
16
use Drupal\Core\Session\AnonymousUserSession;
17 18 19 20 21 22 23 24 25 26 27 28

/**
 * 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."),
 *   default_widget = "comment_default",
 *   default_formatter = "comment_default"
 * )
 */
29
class CommentItem extends FieldItemBase implements CommentItemInterface {
30 31 32

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

  /**
   * {@inheritdoc}
   */
  public static function defaultInstanceSettings() {
    return array(
45
      'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
46
      'per_page' => 50,
47
      'form_location' => CommentItemInterface::FORM_BELOW,
48 49 50 51 52 53 54
      'anonymous' => COMMENT_ANONYMOUS_MAYNOT_CONTACT,
      'preview' => DRUPAL_OPTIONAL,
    ) + parent::defaultInstanceSettings();
  }

  /**
   * {@inheritdoc}
55
   */
56
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
57 58 59 60 61
    $properties['status'] = DataDefinition::create('integer')
      ->setLabel(t('Comment status value'));

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

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

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

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

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

78
    return $properties;
79 80 81 82 83
  }

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

  /**
   * {@inheritdoc}
   */
102
  public function instanceSettingsForm(array $form, FormStateInterface $form_state) {
103 104
    $element = array();

105
    $settings = $this->getSettings();
106

107
    $anonymous_user = new AnonymousUserSession();
108 109 110 111

    $element['comment'] = array(
      '#type' => 'details',
      '#title' => t('Comment form settings'),
112
      '#open' => TRUE,
113 114 115 116
      '#attributes' => array(
        'class' => array('comment-instance-settings-form'),
      ),
      '#attached' => array(
117
        'library' => array('comment/drupal.comment'),
118 119 120 121 122 123 124 125 126
      ),
    );
    $element['comment']['default_mode'] = array(
      '#type' => 'checkbox',
      '#title' => t('Threading'),
      '#default_value' => $settings['default_mode'],
      '#description' => t('Show comment replies in a threaded list.'),
    );
    $element['comment']['per_page'] = array(
127
      '#type' => 'number',
128 129
      '#title' => t('Comments per page'),
      '#default_value' => $settings['per_page'],
130 131 132 133
      '#required' => TRUE,
      '#min' => 10,
      '#max' => 1000,
      '#step' => 10,
134 135 136 137 138 139 140 141 142 143
    );
    $element['comment']['anonymous'] = array(
      '#type' => 'select',
      '#title' => t('Anonymous commenting'),
      '#default_value' => $settings['anonymous'],
      '#options' => array(
        COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
        COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
        COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information'),
      ),
144
      '#access' => $anonymous_user->hasPermission('post comments'),
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    );
    $element['comment']['form_location'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show reply form on the same page as comments'),
      '#default_value' => $settings['form_location'],
    );
    $element['comment']['preview'] = array(
      '#type' => 'radios',
      '#title' => t('Preview comment'),
      '#default_value' => $settings['preview'],
      '#options' => array(
        DRUPAL_DISABLED => t('Disabled'),
        DRUPAL_OPTIONAL => t('Optional'),
        DRUPAL_REQUIRED => t('Required'),
      ),
    );

    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function __get($name) {
    if ($name == 'status' && !isset($this->values[$name])) {
      // Get default value from field instance when no data saved in entity.
171
      $field_default_values = $this->getFieldDefinition()->getDefaultValue($this->getEntity());
172 173 174 175 176 177 178 179 180 181 182
      return $field_default_values[0]['status'];
    }
    else {
      return parent::__get($name);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
183 184 185
    // There is always a value for this field, it is one of
    // CommentItemInterface::OPEN, CommentItemInterface::CLOSED or
    // CommentItemInterface::HIDDEN.
186 187 188
    return FALSE;
  }

189 190 191
  /**
   * {@inheritdoc}
   */
192
  public function settingsForm(array &$form, FormStateInterface $form_state, $has_data) {
193 194
    $element = array();

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    // @todo Inject entity storage once typed-data supports container injection.
    // See https://drupal.org/node/2053415 for more details.
    $comment_types = CommentType::loadMultiple();
    $options = array();
    $entity_type = $this->getEntity()->getEntityTypeId();
    foreach ($comment_types as $comment_type) {
      if ($comment_type->getTargetEntityTypeId() == $entity_type) {
        $options[$comment_type->id()] = $comment_type->label();
      }
    }
    $element['comment_type'] = array(
      '#type' => 'select',
      '#title' => t('Comment type'),
      '#options' => $options,
      '#description' => t('Select the Comment type to use for this comment field.'),
      '#default_value' => $this->getSetting('comment_type'),
      '#disabled' => $has_data,
212 213 214 215
    );
    return $element;
  }

216
}