PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/reasonat/test8
PHP | 212 lines | 144 code | 24 blank | 44 comment | 2 complexity | 9498b67b4ffb52ae2d1eedb5110b7042 MD5 | raw file
  1. <?php
  2. namespace Drupal\comment\Plugin\Field\FieldType;
  3. use Drupal\comment\CommentManagerInterface;
  4. use Drupal\comment\Entity\CommentType;
  5. use Drupal\Core\Field\FieldDefinitionInterface;
  6. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  7. use Drupal\Core\Form\FormStateInterface;
  8. use Drupal\Core\Routing\UrlGeneratorTrait;
  9. use Drupal\Core\TypedData\DataDefinition;
  10. use Drupal\Core\Field\FieldItemBase;
  11. use Drupal\Core\Session\AnonymousUserSession;
  12. /**
  13. * Plugin implementation of the 'comment' field type.
  14. *
  15. * @FieldType(
  16. * id = "comment",
  17. * label = @Translation("Comments"),
  18. * description = @Translation("This field manages configuration and presentation of comments on an entity."),
  19. * list_class = "\Drupal\comment\CommentFieldItemList",
  20. * default_widget = "comment_default",
  21. * default_formatter = "comment_default"
  22. * )
  23. */
  24. class CommentItem extends FieldItemBase implements CommentItemInterface {
  25. use UrlGeneratorTrait;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function defaultStorageSettings() {
  30. return array(
  31. 'comment_type' => '',
  32. ) + parent::defaultStorageSettings();
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function defaultFieldSettings() {
  38. return array(
  39. 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
  40. 'per_page' => 50,
  41. 'form_location' => CommentItemInterface::FORM_BELOW,
  42. 'anonymous' => COMMENT_ANONYMOUS_MAYNOT_CONTACT,
  43. 'preview' => DRUPAL_OPTIONAL,
  44. ) + parent::defaultFieldSettings();
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
  50. $properties['status'] = DataDefinition::create('integer')
  51. ->setLabel(t('Comment status'))
  52. ->setRequired(TRUE);
  53. $properties['cid'] = DataDefinition::create('integer')
  54. ->setLabel(t('Last comment ID'));
  55. $properties['last_comment_timestamp'] = DataDefinition::create('integer')
  56. ->setLabel(t('Last comment timestamp'))
  57. ->setDescription(t('The time that the last comment was created.'));
  58. $properties['last_comment_name'] = DataDefinition::create('string')
  59. ->setLabel(t('Last comment name'))
  60. ->setDescription(t('The name of the user posting the last comment.'));
  61. $properties['last_comment_uid'] = DataDefinition::create('integer')
  62. ->setLabel(t('Last comment user ID'));
  63. $properties['comment_count'] = DataDefinition::create('integer')
  64. ->setLabel(t('Number of comments'))
  65. ->setDescription(t('The number of comments.'));
  66. return $properties;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public static function schema(FieldStorageDefinitionInterface $field_definition) {
  72. return array(
  73. 'columns' => array(
  74. 'status' => array(
  75. 'description' => 'Whether comments are allowed on this entity: 0 = no, 1 = closed (read only), 2 = open (read/write).',
  76. 'type' => 'int',
  77. 'default' => 0,
  78. ),
  79. ),
  80. 'indexes' => array(),
  81. 'foreign keys' => array(),
  82. );
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  88. $element = array();
  89. $settings = $this->getSettings();
  90. $anonymous_user = new AnonymousUserSession();
  91. $element['default_mode'] = array(
  92. '#type' => 'checkbox',
  93. '#title' => t('Threading'),
  94. '#default_value' => $settings['default_mode'],
  95. '#description' => t('Show comment replies in a threaded list.'),
  96. );
  97. $element['per_page'] = array(
  98. '#type' => 'number',
  99. '#title' => t('Comments per page'),
  100. '#default_value' => $settings['per_page'],
  101. '#required' => TRUE,
  102. '#min' => 10,
  103. '#max' => 1000,
  104. '#step' => 10,
  105. );
  106. $element['anonymous'] = array(
  107. '#type' => 'select',
  108. '#title' => t('Anonymous commenting'),
  109. '#default_value' => $settings['anonymous'],
  110. '#options' => array(
  111. COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
  112. COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
  113. COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information'),
  114. ),
  115. '#access' => $anonymous_user->hasPermission('post comments'),
  116. );
  117. $element['form_location'] = array(
  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'] = array(
  123. '#type' => 'radios',
  124. '#title' => t('Preview comment'),
  125. '#default_value' => $settings['preview'],
  126. '#options' => array(
  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 = array();
  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 = array();
  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'] = array(
  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>.', array(':url' => $this->url('entity.comment_type.collection'))),
  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. }