PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php

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