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'
);
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
$entity_field_manager = \Drupal::
service('entity_field.manager'
);
// Create the comment type if needed.
$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();
}