When I try to add a comment field to a paragraph type, the comment type drop down menu shows no options at first. Eventually it seems to accept it, but when I try to display or edit a content type with paragraphs with that comment field, errors are produced. In general though, can comment fields be attached to entity types or just content types?

I'd like users to comment upon individual paragraph instances in a document for targeted content reviews.

Error msg was:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getEntityTypeId() on null in Drupal\comment\CommentForm->actions() (line 240 of core/modules/comment/src/CommentForm.php).

Comments

acaljuba’s picture

Working on it.

acaljuba’s picture

I found a problem.

The reason why the comment type drop down menu shows no options is because there are no relevant comment types targeting paragraph entity type at the moment. This is what I did:

1. Create new Paragraph type.
2. Add new field. Select Comments for a field type in the following page.
3. Select the Comment type to use for your comment field.

This is where confusion occurs. Comment type drop down menu shows no available comment types targeting Paragraph Entity type, so you have to create one yourself. In the description for the drop down menu you should see 'Select the Comment type to use for this comment field. Manage the comment types from the administration overview page.'.

4. Click on the link 'administration overview page'. You'll be redirected to Comment types page.
5. Click on '+Add comment type' button to add new comment type.
6. Fill the label and description fields. In the 'Targeted entity type' section bellow select Paragraph from the drop down menu. Click Save.

Now you have Comment type that targets Paragraphs Entity type.

7. Go to Structure->Paragraphs types.
8. Search for your Paragraph type you created earlier and click on 'Manage fields' button.
9. On the page that appears, look for Options column, select 'Delete' from drop down menu in the button for the comment field you created earlier. Click Delete button on the next page.

Recreate the comment field.

10. Click on '+Add field' button to add comment field.
12. Select Comments for a field type. Click Save and continue.
11. Now, in the drop down menu you should see Comment type you created listed in the available options. Select it and click Save.

This should do it. Try recreating the content with your new Paragraph type.

sjpeters79’s picture

Thanks acaljuba for providing those instructions and that worked but I'm having having an issue where the comment form is not rendering due to a missing uri on the paragraph. Does that happen for you?

scottsawyer’s picture

I just ran into this issue. I actually have the comment form appearing in my paragraph, however, whenever a user attempts to submit the comment, I get an nginx error. Checking my log, I have:

2017/12/25 14:30:24 [error] 14659#14659: *6932 FastCGI sent in stderr: "PHP message: Uncaught PHP Exception Drupal\Core\Entity\Exception\UndefinedLinkTemplateException: "No link template 'canonical' found for the 'paragraph' entity type" at /var/www/staging/drupal/web/core/lib/Drupal/Core/Entity/Entity.php line 215" while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: staging.xxxxxxx.com, request: "POST /comment/reply/paragraph/410/field_feedback HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "xxx.xxx.xxx.xxx:8081", referrer: "http://xxx.xxx.xxx.xxx:8081/node/120"

(domains and IPs redacted)

I have a comment type assigned to Paragraph as the target entity, I have a comment field attached to a Paragraph type. I have a node with 3 instances of that paragraph type ( so, 3 comment forms on one node ).

I am wondering if this is more of a comments issue than a paragraphs issue. Paragraphs do not ( to my knowledge ) have a canonical URI, so, there would be no link template for it, and comments should have a way to manage that. I haven't looked into the comments module yet, but I would not be surprised if it does something with it's parent entity's URL.

From https://api.drupal.org/api/drupal/core%21modules%21comment%21comment.mod...

/**
 * Entity URI callback.
 */
function comment_uri(CommentInterface $comment) {
  return new Url(
    'entity.comment.canonical',
    array(
      'comment' => $comment->id(),
    ),
    array('fragment' => 'comment-' . $comment->id())
  );
}

and maybe:

/**
 * Implements hook_entity_view().
 */
function comment_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($entity instanceof FieldableEntityInterface && $view_mode == 'rss' && $display->getComponent('links')) {
    /** @var \Drupal\comment\CommentManagerInterface $comment_manager */
    $comment_manager = \Drupal::service('comment.manager');
    $fields = $comment_manager->getFields($entity->getEntityTypeId());
    foreach ($fields as $field_name => $detail) {
      if ($entity->hasField($field_name) && $entity->get($field_name)->status != CommentItemInterface::HIDDEN) {
        // Add a comments RSS element which is a URL to the comments of this
        // entity.
        $options = array(
          'fragment' => 'comments',
          'absolute' => TRUE,
        );
        $entity->rss_elements[] = array(
          'key' => 'comments',
          'value' => $entity->url('canonical', $options),
        );
      }
    }
  }
}

So, possibly this is something the comment module is not actually capable of handling. Which seems like a bug.

EDIT: Looks like my issue is related: https://www.drupal.org/project/paragraphs/issues/2878615

Berdir’s picture

Yes, this is a duplicate of #2878615: Comments + entity->uri issue. All those UX problems identifed above are about core/comments.module and have nothing to do with paragraphs specifically. You would have the same problem adding a comment field on users.

The only problem is the canonical link template and we have the other issue for that. And I also think it should be fixed in core/comment.module but that can be discussed in the other issue.

Berdir’s picture

Status: Active » Closed (duplicate)
pianomansam’s picture

develop2015deepak’s picture

Also you can patch core entity module to from
https://www.drupal.org/project/drupal/issues/3186448#comment-13926393