annotate core/modules/comment/src/Tests/CommentTestTrait.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
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@0 38 // Create the comment type if needed.
Chris@0 39 $comment_type_storage = $entity_manager->getStorage('comment_type');
Chris@0 40 if ($comment_type = $comment_type_storage->load($comment_type_id)) {
Chris@0 41 if ($comment_type->getTargetEntityTypeId() !== $entity_type) {
Chris@0 42 throw new \InvalidArgumentException("The given comment type id $comment_type_id can only be used with the $entity_type entity type");
Chris@0 43 }
Chris@0 44 }
Chris@0 45 else {
Chris@0 46 $comment_type_storage->create([
Chris@0 47 'id' => $comment_type_id,
Chris@0 48 'label' => Unicode::ucfirst($comment_type_id),
Chris@0 49 'target_entity_type_id' => $entity_type,
Chris@0 50 'description' => 'Default comment field',
Chris@0 51 ])->save();
Chris@0 52 }
Chris@0 53 // Add a body field to the comment type.
Chris@0 54 \Drupal::service('comment.manager')->addBodyField($comment_type_id);
Chris@0 55
Chris@0 56 // Add a comment field to the host entity type. Create the field storage if
Chris@0 57 // needed.
Chris@0 58 if (!array_key_exists($field_name, $entity_manager->getFieldStorageDefinitions($entity_type))) {
Chris@0 59 $entity_manager->getStorage('field_storage_config')->create([
Chris@0 60 'entity_type' => $entity_type,
Chris@0 61 'field_name' => $field_name,
Chris@0 62 'type' => 'comment',
Chris@0 63 'translatable' => TRUE,
Chris@0 64 'settings' => [
Chris@0 65 'comment_type' => $comment_type_id,
Chris@0 66 ],
Chris@0 67 ])->save();
Chris@0 68 }
Chris@0 69 // Create the field if needed, and configure its form and view displays.
Chris@0 70 if (!array_key_exists($field_name, $entity_manager->getFieldDefinitions($entity_type, $bundle))) {
Chris@0 71 $entity_manager->getStorage('field_config')->create([
Chris@0 72 'label' => 'Comments',
Chris@0 73 'description' => '',
Chris@0 74 'field_name' => $field_name,
Chris@0 75 'entity_type' => $entity_type,
Chris@0 76 'bundle' => $bundle,
Chris@0 77 'required' => 1,
Chris@0 78 'default_value' => [
Chris@0 79 [
Chris@0 80 'status' => $default_value,
Chris@0 81 'cid' => 0,
Chris@0 82 'last_comment_name' => '',
Chris@0 83 'last_comment_timestamp' => 0,
Chris@0 84 'last_comment_uid' => 0,
Chris@0 85 ],
Chris@0 86 ],
Chris@0 87 ])->save();
Chris@0 88
Chris@0 89 // Entity form displays: assign widget settings for the 'default' form
Chris@0 90 // mode, and hide the field in all other form modes.
Chris@0 91 entity_get_form_display($entity_type, $bundle, 'default')
Chris@0 92 ->setComponent($field_name, [
Chris@0 93 'type' => 'comment_default',
Chris@0 94 'weight' => 20,
Chris@0 95 ])
Chris@0 96 ->save();
Chris@0 97 foreach ($entity_manager->getFormModes($entity_type) as $id => $form_mode) {
Chris@0 98 $display = entity_get_form_display($entity_type, $bundle, $id);
Chris@0 99 // Only update existing displays.
Chris@0 100 if ($display && !$display->isNew()) {
Chris@0 101 $display->removeComponent($field_name)->save();
Chris@0 102 }
Chris@0 103 }
Chris@0 104
Chris@0 105 // Entity view displays: assign widget settings for the 'default' view
Chris@0 106 // mode, and hide the field in all other view modes.
Chris@0 107 entity_get_display($entity_type, $bundle, 'default')
Chris@0 108 ->setComponent($field_name, [
Chris@0 109 'label' => 'above',
Chris@0 110 'type' => 'comment_default',
Chris@0 111 'weight' => 20,
Chris@0 112 'settings' => ['view_mode' => $comment_view_mode],
Chris@0 113 ])
Chris@0 114 ->save();
Chris@0 115 foreach ($entity_manager->getViewModes($entity_type) as $id => $view_mode) {
Chris@0 116 $display = entity_get_display($entity_type, $bundle, $id);
Chris@0 117 // Only update existing displays.
Chris@0 118 if ($display && !$display->isNew()) {
Chris@0 119 $display->removeComponent($field_name)->save();
Chris@0 120 }
Chris@0 121 }
Chris@0 122 }
Chris@0 123 }
Chris@0 124
Chris@0 125 }