3 namespace Drupal\comment\Tests;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
9 * Provides common functionality for the Comment test classes.
11 trait CommentTestTrait {
14 * Adds the default comment field to an entity.
16 * Attaches a comment field named 'comment' to the given entity type and
17 * bundle. Largely replicates the default behavior in Drupal 7 and earlier.
19 * @param string $entity_type
20 * The entity type to attach the default comment field to.
21 * @param string $bundle
22 * The bundle to attach the default comment field to.
23 * @param string $field_name
24 * (optional) Field name to use for the comment field. Defaults to
26 * @param int $default_value
27 * (optional) Default value, one of CommentItemInterface::HIDDEN,
28 * CommentItemInterface::OPEN, CommentItemInterface::CLOSED. Defaults to
29 * CommentItemInterface::OPEN.
30 * @param string $comment_type_id
31 * (optional) ID of comment type to use. Defaults to 'comment'.
32 * @param string $comment_view_mode
33 * (optional) The comment view mode to be used in comment field formatter.
36 public function addDefaultCommentField($entity_type, $bundle, $field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment', $comment_view_mode = 'full') {
37 $entity_manager = \Drupal::entityManager();
38 // Create the comment type if needed.
39 $comment_type_storage = $entity_manager->getStorage('comment_type');
40 if ($comment_type = $comment_type_storage->load($comment_type_id)) {
41 if ($comment_type->getTargetEntityTypeId() !== $entity_type) {
42 throw new \InvalidArgumentException("The given comment type id $comment_type_id can only be used with the $entity_type entity type");
46 $comment_type_storage->create([
47 'id' => $comment_type_id,
48 'label' => Unicode::ucfirst($comment_type_id),
49 'target_entity_type_id' => $entity_type,
50 'description' => 'Default comment field',
53 // Add a body field to the comment type.
54 \Drupal::service('comment.manager')->addBodyField($comment_type_id);
56 // Add a comment field to the host entity type. Create the field storage if
58 if (!array_key_exists($field_name, $entity_manager->getFieldStorageDefinitions($entity_type))) {
59 $entity_manager->getStorage('field_storage_config')->create([
60 'entity_type' => $entity_type,
61 'field_name' => $field_name,
63 'translatable' => TRUE,
65 'comment_type' => $comment_type_id,
69 // Create the field if needed, and configure its form and view displays.
70 if (!array_key_exists($field_name, $entity_manager->getFieldDefinitions($entity_type, $bundle))) {
71 $entity_manager->getStorage('field_config')->create([
72 'label' => 'Comments',
74 'field_name' => $field_name,
75 'entity_type' => $entity_type,
80 'status' => $default_value,
82 'last_comment_name' => '',
83 'last_comment_timestamp' => 0,
84 'last_comment_uid' => 0,
89 // Entity form displays: assign widget settings for the 'default' form
90 // mode, and hide the field in all other form modes.
91 entity_get_form_display($entity_type, $bundle, 'default')
92 ->setComponent($field_name, [
93 'type' => 'comment_default',
97 foreach ($entity_manager->getFormModes($entity_type) as $id => $form_mode) {
98 $display = entity_get_form_display($entity_type, $bundle, $id);
99 // Only update existing displays.
100 if ($display && !$display->isNew()) {
101 $display->removeComponent($field_name)->save();
105 // Entity view displays: assign widget settings for the 'default' view
106 // mode, and hide the field in all other view modes.
107 entity_get_display($entity_type, $bundle, 'default')
108 ->setComponent($field_name, [
110 'type' => 'comment_default',
112 'settings' => ['view_mode' => $comment_view_mode],
115 foreach ($entity_manager->getViewModes($entity_type) as $id => $view_mode) {
116 $display = entity_get_display($entity_type, $bundle, $id);
117 // Only update existing displays.
118 if ($display && !$display->isNew()) {
119 $display->removeComponent($field_name)->save();