PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/comment/src/Tests/CommentTestTrait.php

https://gitlab.com/reasonat/test8
PHP | 121 lines | 79 code | 8 blank | 34 comment | 9 complexity | 37239df76299c2c66c4914f8197cdf65 MD5 | raw file
  1. <?php
  2. namespace Drupal\comment\Tests;
  3. use Drupal\Component\Utility\Unicode;
  4. use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
  5. /**
  6. * Provides common functionality for the Comment test classes.
  7. */
  8. trait CommentTestTrait {
  9. /**
  10. * Adds the default comment field to an entity.
  11. *
  12. * Attaches a comment field named 'comment' to the given entity type and
  13. * bundle. Largely replicates the default behavior in Drupal 7 and earlier.
  14. *
  15. * @param string $entity_type
  16. * The entity type to attach the default comment field to.
  17. * @param string $bundle
  18. * The bundle to attach the default comment field to.
  19. * @param string $field_name
  20. * (optional) Field name to use for the comment field. Defaults to
  21. * 'comment'.
  22. * @param int $default_value
  23. * (optional) Default value, one of CommentItemInterface::HIDDEN,
  24. * CommentItemInterface::OPEN, CommentItemInterface::CLOSED. Defaults to
  25. * CommentItemInterface::OPEN.
  26. * @param string $comment_type_id
  27. * (optional) ID of comment type to use. Defaults to 'comment'.
  28. */
  29. public function addDefaultCommentField($entity_type, $bundle, $field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment') {
  30. $entity_manager = \Drupal::entityManager();
  31. // Create the comment type if needed.
  32. $comment_type_storage = $entity_manager->getStorage('comment_type');
  33. if ($comment_type = $comment_type_storage->load($comment_type_id)) {
  34. if ($comment_type->getTargetEntityTypeId() !== $entity_type) {
  35. throw new \InvalidArgumentException("The given comment type id $comment_type_id can only be used with the $entity_type entity type");
  36. }
  37. }
  38. else {
  39. $comment_type_storage->create(array(
  40. 'id' => $comment_type_id,
  41. 'label' => Unicode::ucfirst($comment_type_id),
  42. 'target_entity_type_id' => $entity_type,
  43. 'description' => 'Default comment field',
  44. ))->save();
  45. }
  46. // Add a body field to the comment type.
  47. \Drupal::service('comment.manager')->addBodyField($comment_type_id);
  48. // Add a comment field to the host entity type. Create the field storage if
  49. // needed.
  50. if (!array_key_exists($field_name, $entity_manager->getFieldStorageDefinitions($entity_type))) {
  51. $entity_manager->getStorage('field_storage_config')->create(array(
  52. 'entity_type' => $entity_type,
  53. 'field_name' => $field_name,
  54. 'type' => 'comment',
  55. 'translatable' => TRUE,
  56. 'settings' => array(
  57. 'comment_type' => $comment_type_id,
  58. ),
  59. ))->save();
  60. }
  61. // Create the field if needed, and configure its form and view displays.
  62. if (!array_key_exists($field_name, $entity_manager->getFieldDefinitions($entity_type, $bundle))) {
  63. $entity_manager->getStorage('field_config')->create(array(
  64. 'label' => 'Comments',
  65. 'description' => '',
  66. 'field_name' => $field_name,
  67. 'entity_type' => $entity_type,
  68. 'bundle' => $bundle,
  69. 'required' => 1,
  70. 'default_value' => array(
  71. array(
  72. 'status' => $default_value,
  73. 'cid' => 0,
  74. 'last_comment_name' => '',
  75. 'last_comment_timestamp' => 0,
  76. 'last_comment_uid' => 0,
  77. ),
  78. ),
  79. ))->save();
  80. // Entity form displays: assign widget settings for the 'default' form
  81. // mode, and hide the field in all other form modes.
  82. entity_get_form_display($entity_type, $bundle, 'default')
  83. ->setComponent($field_name, array(
  84. 'type' => 'comment_default',
  85. 'weight' => 20,
  86. ))
  87. ->save();
  88. foreach ($entity_manager->getFormModes($entity_type) as $id => $form_mode) {
  89. $display = entity_get_form_display($entity_type, $bundle, $id);
  90. // Only update existing displays.
  91. if ($display && !$display->isNew()) {
  92. $display->removeComponent($field_name)->save();
  93. }
  94. }
  95. // Entity view displays: assign widget settings for the 'default' view
  96. // mode, and hide the field in all other view modes.
  97. entity_get_display($entity_type, $bundle, 'default')
  98. ->setComponent($field_name, array(
  99. 'label' => 'above',
  100. 'type' => 'comment_default',
  101. 'weight' => 20,
  102. ))
  103. ->save();
  104. foreach ($entity_manager->getViewModes($entity_type) as $id => $view_mode) {
  105. $display = entity_get_display($entity_type, $bundle, $id);
  106. // Only update existing displays.
  107. if ($display && !$display->isNew()) {
  108. $display->removeComponent($field_name)->save();
  109. }
  110. }
  111. }
  112. }
  113. }