PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/guillaumev/alkarama
PHP | 125 lines | 80 code | 8 blank | 37 comment | 9 complexity | c40e4bd1fd74b47249187f2f8c2e68a0 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. * @param string $comment_view_mode
  29. * (optional) The comment view mode to be used in comment field formatter.
  30. * Defaults to 'full'.
  31. */
  32. public function addDefaultCommentField($entity_type, $bundle, $field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment', $comment_view_mode = 'full') {
  33. $entity_manager = \Drupal::entityManager();
  34. // Create the comment type if needed.
  35. $comment_type_storage = $entity_manager->getStorage('comment_type');
  36. if ($comment_type = $comment_type_storage->load($comment_type_id)) {
  37. if ($comment_type->getTargetEntityTypeId() !== $entity_type) {
  38. throw new \InvalidArgumentException("The given comment type id $comment_type_id can only be used with the $entity_type entity type");
  39. }
  40. }
  41. else {
  42. $comment_type_storage->create(array(
  43. 'id' => $comment_type_id,
  44. 'label' => Unicode::ucfirst($comment_type_id),
  45. 'target_entity_type_id' => $entity_type,
  46. 'description' => 'Default comment field',
  47. ))->save();
  48. }
  49. // Add a body field to the comment type.
  50. \Drupal::service('comment.manager')->addBodyField($comment_type_id);
  51. // Add a comment field to the host entity type. Create the field storage if
  52. // needed.
  53. if (!array_key_exists($field_name, $entity_manager->getFieldStorageDefinitions($entity_type))) {
  54. $entity_manager->getStorage('field_storage_config')->create(array(
  55. 'entity_type' => $entity_type,
  56. 'field_name' => $field_name,
  57. 'type' => 'comment',
  58. 'translatable' => TRUE,
  59. 'settings' => array(
  60. 'comment_type' => $comment_type_id,
  61. ),
  62. ))->save();
  63. }
  64. // Create the field if needed, and configure its form and view displays.
  65. if (!array_key_exists($field_name, $entity_manager->getFieldDefinitions($entity_type, $bundle))) {
  66. $entity_manager->getStorage('field_config')->create(array(
  67. 'label' => 'Comments',
  68. 'description' => '',
  69. 'field_name' => $field_name,
  70. 'entity_type' => $entity_type,
  71. 'bundle' => $bundle,
  72. 'required' => 1,
  73. 'default_value' => array(
  74. array(
  75. 'status' => $default_value,
  76. 'cid' => 0,
  77. 'last_comment_name' => '',
  78. 'last_comment_timestamp' => 0,
  79. 'last_comment_uid' => 0,
  80. ),
  81. ),
  82. ))->save();
  83. // Entity form displays: assign widget settings for the 'default' form
  84. // mode, and hide the field in all other form modes.
  85. entity_get_form_display($entity_type, $bundle, 'default')
  86. ->setComponent($field_name, array(
  87. 'type' => 'comment_default',
  88. 'weight' => 20,
  89. ))
  90. ->save();
  91. foreach ($entity_manager->getFormModes($entity_type) as $id => $form_mode) {
  92. $display = entity_get_form_display($entity_type, $bundle, $id);
  93. // Only update existing displays.
  94. if ($display && !$display->isNew()) {
  95. $display->removeComponent($field_name)->save();
  96. }
  97. }
  98. // Entity view displays: assign widget settings for the 'default' view
  99. // mode, and hide the field in all other view modes.
  100. entity_get_display($entity_type, $bundle, 'default')
  101. ->setComponent($field_name, array(
  102. 'label' => 'above',
  103. 'type' => 'comment_default',
  104. 'weight' => 20,
  105. 'settings' => array('view_mode' => $comment_view_mode),
  106. ))
  107. ->save();
  108. foreach ($entity_manager->getViewModes($entity_type) as $id => $view_mode) {
  109. $display = entity_get_display($entity_type, $bundle, $id);
  110. // Only update existing displays.
  111. if ($display && !$display->isNew()) {
  112. $display->removeComponent($field_name)->save();
  113. }
  114. }
  115. }
  116. }
  117. }