annotate core/modules/comment/src/Tests/CommentTestTrait.php @ 19:fa3358dc1485 tip

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