Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/comment/src/Tests/CommentTestTrait.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | 12f9dff5fda9 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\comment\Tests; | |
4 | |
5 use Drupal\Component\Utility\Unicode; | |
6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; | |
7 | |
8 /** | |
9 * Provides common functionality for the Comment test classes. | |
10 */ | |
11 trait CommentTestTrait { | |
12 | |
13 /** | |
14 * Adds the default comment field to an entity. | |
15 * | |
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. | |
18 * | |
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 | |
25 * 'comment'. | |
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. | |
34 * Defaults to 'full'. | |
35 */ | |
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"); | |
43 } | |
44 } | |
45 else { | |
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', | |
51 ])->save(); | |
52 } | |
53 // Add a body field to the comment type. | |
54 \Drupal::service('comment.manager')->addBodyField($comment_type_id); | |
55 | |
56 // Add a comment field to the host entity type. Create the field storage if | |
57 // needed. | |
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, | |
62 'type' => 'comment', | |
63 'translatable' => TRUE, | |
64 'settings' => [ | |
65 'comment_type' => $comment_type_id, | |
66 ], | |
67 ])->save(); | |
68 } | |
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', | |
73 'description' => '', | |
74 'field_name' => $field_name, | |
75 'entity_type' => $entity_type, | |
76 'bundle' => $bundle, | |
77 'required' => 1, | |
78 'default_value' => [ | |
79 [ | |
80 'status' => $default_value, | |
81 'cid' => 0, | |
82 'last_comment_name' => '', | |
83 'last_comment_timestamp' => 0, | |
84 'last_comment_uid' => 0, | |
85 ], | |
86 ], | |
87 ])->save(); | |
88 | |
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', | |
94 'weight' => 20, | |
95 ]) | |
96 ->save(); | |
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(); | |
102 } | |
103 } | |
104 | |
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, [ | |
109 'label' => 'above', | |
110 'type' => 'comment_default', | |
111 'weight' => 20, | |
112 'settings' => ['view_mode' => $comment_view_mode], | |
113 ]) | |
114 ->save(); | |
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(); | |
120 } | |
121 } | |
122 } | |
123 } | |
124 | |
125 } |