comparison core/modules/comment/src/Tests/CommentTestTrait.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
33 * (optional) The comment view mode to be used in comment field formatter. 33 * (optional) The comment view mode to be used in comment field formatter.
34 * Defaults to 'full'. 34 * Defaults to 'full'.
35 */ 35 */
36 public function addDefaultCommentField($entity_type, $bundle, $field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment', $comment_view_mode = 'full') { 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(); 37 $entity_manager = \Drupal::entityManager();
38 $entity_display_repository = \Drupal::service('entity_display.repository');
38 // Create the comment type if needed. 39 // Create the comment type if needed.
39 $comment_type_storage = $entity_manager->getStorage('comment_type'); 40 $comment_type_storage = $entity_manager->getStorage('comment_type');
40 if ($comment_type = $comment_type_storage->load($comment_type_id)) { 41 if ($comment_type = $comment_type_storage->load($comment_type_id)) {
41 if ($comment_type->getTargetEntityTypeId() !== $entity_type) { 42 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 throw new \InvalidArgumentException("The given comment type id $comment_type_id can only be used with the $entity_type entity type");
92 ->setComponent($field_name, [ 93 ->setComponent($field_name, [
93 'type' => 'comment_default', 94 'type' => 'comment_default',
94 'weight' => 20, 95 'weight' => 20,
95 ]) 96 ])
96 ->save(); 97 ->save();
97 foreach ($entity_manager->getFormModes($entity_type) as $id => $form_mode) { 98 foreach ($entity_display_repository->getFormModes($entity_type) as $id => $form_mode) {
98 $display = entity_get_form_display($entity_type, $bundle, $id); 99 $display = entity_get_form_display($entity_type, $bundle, $id);
99 // Only update existing displays. 100 // Only update existing displays.
100 if ($display && !$display->isNew()) { 101 if ($display && !$display->isNew()) {
101 $display->removeComponent($field_name)->save(); 102 $display->removeComponent($field_name)->save();
102 } 103 }
110 'type' => 'comment_default', 111 'type' => 'comment_default',
111 'weight' => 20, 112 'weight' => 20,
112 'settings' => ['view_mode' => $comment_view_mode], 113 'settings' => ['view_mode' => $comment_view_mode],
113 ]) 114 ])
114 ->save(); 115 ->save();
115 foreach ($entity_manager->getViewModes($entity_type) as $id => $view_mode) { 116 foreach ($entity_display_repository->getViewModes($entity_type) as $id => $view_mode) {
116 $display = entity_get_display($entity_type, $bundle, $id); 117 $display = entity_get_display($entity_type, $bundle, $id);
117 // Only update existing displays. 118 // Only update existing displays.
118 if ($display && !$display->isNew()) { 119 if ($display && !$display->isNew()) {
119 $display->removeComponent($field_name)->save(); 120 $display->removeComponent($field_name)->save();
120 } 121 }