1

I'm trying to figure out allow commenting on the submissions so there can be a back and forth dialog between the user and site admin(s).

In Drupal 8 webform submissions are entities (type: webform_submission) and comments are a field.

I have created a new Comment Type (webform_submission_comment) with the "Target entity type" set to webform_submission).

How do I attach a comment field to the webform_submission entity?

3 Answers 3

3

Webform submissions are content entities without Field API enabled. Comments can only be attached to fieldable entities. Webform submission data is not stored Field API, but your use case is a very reasonable example of why Field API could/should be enabled. The challenge is the UX for supporting fields, and webform elements at the same time could be overwhelming. I am not prepared to support this the core Webform module. A dedicated webform_comment.module could be created to enable Field API and might even only support for adding comments to submissions.


You are going to have use hook_entity_type_alter() to add 'field_ui_base_route' to the WebformSubmission entity.

If your content entity is fieldable, provide 'field_ui_base_route' annotation, giving the name of the route that the Manage Fields, Manage Display, and Manage Form Display pages from the Field UI module will be attached to. This is usually the bundle settings edit page, or an entity type settings page if there are no bundles.

-- https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/8.2.x

2
  • Thank you for the explanation. Can you offer a place to start in making the Webform submission entity fieldable?
    – Russell
    Apr 27, 2018 at 21:52
  • I added some more notes to my answer.
    – jrockowitz
    Apr 28, 2018 at 12:59
0

I have got this working rather easily by doing the following:

  1. Implement the following hook in my custom module and clear the cache:

/**

  • Implements hook_entity_type_alter().
  • @param array $entity_types
  • @return void */ function mymodule_entity_type_alter(array &$entity_types) { $entity_types['webform_submission']->fieldable = TRUE; $entity_types['webform_submission']->set('field_ui_base_route', 'entity.webform.edit_form'); }
  1. With Comment module enabled, go to administer comment types: admin/structure/comment and add a comment type that targets webform submissions.
  2. Go to webform administration page: admin/structure/webform and click on the Operations dropdown for the webform you wish to add comments to. You should now find a Manage Fields link.
  3. Click on Manage Fields and add a comment field of the type you just created.
0

As @NewZeal mentioned I create custom module with this code:

<?php

function MYMODULE_entity_type_alter(array &$entity_types) {
    $entity_types['webform_submission']->fieldable = TRUE; 
    $entity_types['webform_submission']->set('field_ui_base_route', 'entity.webform.edit_form');
}

Now you can create fields for each webform but I if you go to /admin/structure/webform/manage/MYFORM/form-display i got this error:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getEntityTypeId() on null in Drupal\webform\Breadcrumb\WebformBreadcrumbBuilder->build() (line 159 of modules/webform/src/Breadcrumb/WebformBreadcrumbBuilder.php). 

How can I solve it?

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.