View source
<?php
namespace Drupal\comment\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
trait CommentTestTrait {
public function addDefaultCommentField($entity_type, $bundle, $field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment', $comment_view_mode = 'full') {
$entity_type_manager = \Drupal::entityTypeManager();
$entity_display_repository = \Drupal::service('entity_display.repository');
$entity_field_manager = \Drupal::service('entity_field.manager');
$comment_type_storage = $entity_type_manager
->getStorage('comment_type');
if ($comment_type = $comment_type_storage
->load($comment_type_id)) {
if ($comment_type
->getTargetEntityTypeId() !== $entity_type) {
throw new \InvalidArgumentException("The given comment type id {$comment_type_id} can only be used with the {$entity_type} entity type");
}
}
else {
$comment_type_storage
->create([
'id' => $comment_type_id,
'label' => Unicode::ucfirst($comment_type_id),
'target_entity_type_id' => $entity_type,
'description' => 'Default comment field',
])
->save();
}
\Drupal::service('comment.manager')
->addBodyField($comment_type_id);
if (!array_key_exists($field_name, $entity_field_manager
->getFieldStorageDefinitions($entity_type))) {
$entity_type_manager
->getStorage('field_storage_config')
->create([
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'comment',
'translatable' => TRUE,
'settings' => [
'comment_type' => $comment_type_id,
],
])
->save();
}
if (!array_key_exists($field_name, $entity_field_manager
->getFieldDefinitions($entity_type, $bundle))) {
$entity_type_manager
->getStorage('field_config')
->create([
'label' => 'Comments',
'description' => '',
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'required' => 1,
'default_value' => [
[
'status' => $default_value,
'cid' => 0,
'last_comment_name' => '',
'last_comment_timestamp' => 0,
'last_comment_uid' => 0,
],
],
])
->save();
$entity_display_repository
->getFormDisplay($entity_type, $bundle)
->setComponent($field_name, [
'type' => 'comment_default',
'weight' => 20,
])
->save();
foreach ($entity_display_repository
->getFormModes($entity_type) as $id => $form_mode) {
$display = $entity_display_repository
->getFormDisplay($entity_type, $bundle, $id);
if ($display && !$display
->isNew()) {
$display
->removeComponent($field_name)
->save();
}
}
$entity_display_repository
->getViewDisplay($entity_type, $bundle)
->setComponent($field_name, [
'label' => 'above',
'type' => 'comment_default',
'weight' => 20,
'settings' => [
'view_mode' => $comment_view_mode,
],
])
->save();
foreach ($entity_display_repository
->getViewModes($entity_type) as $id => $view_mode) {
$display = $entity_display_repository
->getViewDisplay($entity_type, $bundle, $id);
if ($display && !$display
->isNew()) {
$display
->removeComponent($field_name)
->save();
}
}
}
}
}