According to https://www.drupal.org/node/2100015: This means you can add commenting to any entity type by adding a comments field.
We're building a custom entity type, using a custom storage controller (in essence a REST backend), we can define the comment type and select our custom entity, but when trying to add the comment 'field' it does not show in the field.
Either the CR is wrong in stating that it can be added to any entity type while it isn't, or CR is right and there's something wrong with the code.


| Comment | File | Size | Author |
|---|---|---|---|
| #81 | allow_comments_to_be-2496699-81.patch | 115.45 KB | sylus |
| #68 | interdiff-66-68.txt | 6.03 KB | jelle_s |
| #68 | 2496699-comment-entity-reference-68.patch | 112.35 KB | jelle_s |
| Screenshot from 2015-05-28 22:07:11.png | 97.68 KB | attiks | |
| Screenshot from 2015-05-28 22:06:01.png | 27.95 KB | attiks |
Comments
Comment #1
berdir95% sure that this is not a core problem.
Comment requires that the host entity has an integer ID. To figure that out, it loads the base field definitions and uses those to verify that.
You either have a non-integer field definition for your ID or you didn't specify base field definitions.
See comment_form_field_ui_field_storage_add_form_alter() and _comment_entity_uses_integer_id().
Comment #2
attiks commentedYou're right, our entities do not have an integer key. Why is this a requirement?
Renamed the issue
Comment #3
berdirBecause comments have a reference to the commented entity with the entity_type/entity_id base fields, and that's string/integer. It's not possible to vary that by the reference and changing it to string would be a performance regression for all the ID entity types.
See #2205215: {comment} and {comment_entity_statistics} only support integer entity ids.
I'm sorry, but for 8.x, that's pretty much by design and won't change.
Comment #4
attiks commentedRelated issue #2496913: Don't expose entity types with string ids as a target option when creating comment types
So there's going to be a contrib module, 99% similar to comment only to support string id's, sorry but this does not make any sense.
So can we reconsider this and allow comments to be attached to all entities, like the CR says?
Comment #5
attiks commented#3 I read the issue twice, but I'm missing the performance problem part, the decision seems to be made by #2205215-38: {comment} and {comment_entity_statistics} only support integer entity ids:
Comment #6
damienmckennaFYI Comment module is not the only thing with this limitation, much of core (and contrib) is built around integer-based primary keys.
Changing this to a feature request for 8.1.
Comment #7
attiks commentedIsn't it easier to fix this instead of postponing? Are we actually going to release a version that allows string as keys, but when you use it, you cannot use half of core anymore? Doesn't that sound a bit silly?
I thought the great plan was to use the API, so this shouldn't be a problem to implement, according to @Berdir it was done for performance reasons, but I don't seem to find the issue.
Comment #8
attiks commentedProof of concept patch, tested with external entities using string ids, most of the problems were related to #2107249: Don't assume that content entities have numeric IDs in EntityReferenceItem, which is also a bug (at least to me).
I expect failures since the other issues has them as well.
Comment #10
attiks commentedI have a question, if answered before, apologies
Why doesn't comment use a real entity_reference field, that gets attached similar how the boy field gets attached?
AFAIK this will allow us to attach it to any entity
Assigned to a comment maintainer
Comment #11
andypost@attiks Because 'comments' are not the ER but property (enable,disable,hidden), see the original conversion issue #731724: Convert comment settings into a field to make them work with CMI and non-node entities
The ER you keep in mind is from comment entity to parent entity does not fit in core now.
The only attempt to fix that was #1995944: Remove entity_id and entity_type from the comment table and replace with relationship tables but storing "entity_type+entity_id" to fetch comments bring a lot of overhead, I can't find the exact comment where it was measured but we spend a lot of time on that before.
Comment #12
attiks commented#11 I was taking about the entity_id and entity_type in the comment_field_data table, the entity_id gets created as an entity_reference by Comment::baseFieldDefinitions, the only problem is that it gets created as an int, the patch above forces all entity_references to be created as strings (breaking other things) but that is the only change needed to make comments work with string keyed entities.
So if there is a way to make entity_id into a string this might work, the only problem I see is that if the comment is attached to a node the data types do not match (node.nid being an int, comment_field_data.entity_id being a string).
Without the patch above, I tried altering the storage using CommentStorageSchema::getSharedTableFieldSchema as follows
which works, but the problem is that the value gets converted before being saved, probably because of the property definition, but I guess this is something similar as tried in #1995944: Remove entity_id and entity_type from the comment table and replace with relationship tables
Comment #13
larowlanMySQL will forgive you for putting ints into a varchar column, Postgres won't be so understanding.
If we do this, we accept that ID lookups aren't going to be as efficient as they'd be on an integer.
Comment #14
attiks commented#13 I know string indexes will be slower, but I think it is an acceptable trade off to make everything consistent and avoid having a comment module in contrib only to support strings as keys.
Comment #15
larowlanMillion dollar question is how much slower
Comment #16
jelle_sWith #2107249: Don't assume that content entities have numeric IDs in EntityReferenceItem we could convert the entity_id base field to field instances, which would automatically have the right schema according to the entity type they're referencing.
I'm not sure what to do with the statistics: Convert the entity_id to a string (but what about Postgres), ... (any other suggestions?)?
I'll have a look in to this.
Comment #17
andypost@Jelle_S this will bring to separate data tables for comments with int and string IDs, that means you move that primary ID into linked table - that's wrong.
Another trick here is a comment bundle (comment type) that defines fields and by that we need to keep this "commented entity id" in base table.
PS: conversion issue #2228763: Create a comment-type config entity and use that as comment bundles, require selection in field settings form
Comment #18
attiks commented#17 You'll get separate tables for each comment type, isn't the primary key supposed to be cid?
If not, any other ideas on how this can be solved?
Comment #19
jelle_sHere's what I got so far. Tests aren't green yet but I'm just uploading my progress thus far.
For some reason, in following code in the CommentManager, the field settings do not get updated after save, they just get overridden again and the entity reference points to a user in stead of an entity_test... I'm still baffled how this happens... Couldn't find an explanation so far:
Comment #20
andypost#18 makes sense but breaks BC in many places, at least history and comment statistics should use the same table split.
Suppose that should be done in contrib and then migrate to core
wonders if we have limits on entity_id as string
--
we use "commented_entity" all over module
is there a reason to keep that in manager?
otoh that looks like a trick...
Like having comment interface getCommentedEntity() method that accesses a field module provided field...
Comment #21
jelle_sI tested this patch in combination with #2107249: Don't assume that content entities have numeric IDs in EntityReferenceItem. Not sure if this patch needs it, but with it the relevant tests were green on my machine.
Comment #23
jelle_sI was working on an older D8 checkout. Rerolled patch.
Comment #24
jelle_sSmall error in reroll. New patch.
Comment #27
jelle_sStill won't be green, but should be a lot closer to it.
Comment #28
jelle_sstill some debug code
Comment #30
jelle_sNew patch. I think this should be green... Go testbot!
Side note:
This patch in itself does not allow comments to be attached to entities using a string primary key. But it changes the link between the entity and the comment to an entity_reference field. So when #2107249: Don't assume that content entities have numeric IDs in EntityReferenceItem lands (which is RTBC right now) it will be possible to do so.
Comment #31
jelle_sComment #33
jelle_sI fixed the exception, but there is still one fail in
Drupal\views\Tests\Entity\ViewEntityDependenciesTest: it expects comment, node and user as module dependencies for a view, but gets only comment and user. I'm not quite sure if it's actually a bug, or normal behavior, since the new views relation is provided by entity_reference, which already is a dependency for comment. I don't know enough about the way configuration management or views deals with this, so I could really use someone who knows a lot about those modules to have a look and see if it's normal behavior or a bug. If it's normal behavior, all that's left is to change that test...Comment #37
jelle_sEven weirder:
When I import the view on a D8 install with the patch and I execute following code in devel/php I do get the correct dependencies:
Result:
Comment #38
jelle_sNever mind. Turns out I was looking at the wrong view (sigh). The test_relationship_dependency view was still using the old "node" relationship (which doesn't exist anymore because it's been replaced by entity_reference). This patch should be green.
Comment #39
attiks commented#20
Regarding the history table, this is not for this patch since it only works with nodes. Even before this patch you were able to add comments to users, but the history was never used.
To solve the comment statistics, I think it is ok to move the cast into the SQL statement since the comment module assumes an SQL compatible backend anyway. Unless somebody knows a better way to solve this?
Comment #40
attiks commentedINNDER should be INNER
I guess this is untested code
Comment #41
dawehnerI'd not expected it to be tested in the first place :)
Comment #42
jelle_sThe existing tests still test all the functionality. This piece of code was untested, even before this patch. Everything about comment that was tested, is still tested with this patch. So to me, covering that bit of code with tests, seems out of scope for this issue. It was just discovered that it wasn't tested because of this patch, but it isn't the cause of it. Tentatively removing the Needs tests tag.
Comment #43
attiks commentedINNDER fixed
Comment #44
andypostbetter to file separate issue to add test for that
looks we have no tests for this handler at all
Comment #45
jelle_sAm I correct in saying that both should be separate issues since they didn't have any tests before this patch either? (Or should it be one separate issue for both tests?)
Comment #46
attiks commentedRegarding the statistics in #39 I think the best will be the cast the entity id to a varchar, so the index of comment_statistics can be used for the join
Comment #47
jelle_sNew patch with the entity_id cast, as described in #46.
Comment #49
jelle_sUgh, wrong classname...
Comment #50
jelle_sComment #53
attiks commentedI considering marking this as a bug since Drupal core isn't using his own API, does anybody has any objections?
Comment #54
larowlanNeeds profiling
Isn't a bug, see comments above - by design
Also why didn't we profile changing the column in HEAD to a string and casting numeric ids?
Comment #55
attiks commentedAny tips on how to profile?
Comment #56
attiks commentedI did some profiling using https://www.drupal.org/project/webprofiler but there is no difference in any of the numbers, which was to be expected. Numbers are not really accurate since the site has been reinstalled between tests.
Tested on homepage with 10 nodes and 50 comments in total, logged in as admin and cleared cache
Without patch
First run: 1216ms, 31MB, 388 queries in 359 ms
Second run: 687ms, 29MB, 314 queries in 192 ms
Third run: 120ms, 16MB, 83 queries in 16 ms
With patch
First run: 1060ms, 38MB, 393 queries in 315 ms
Second run: 427ms, 21MB, 88 queries in 68 ms
Third run: 234ms, 21MB, 69 queries in 19 ms
Tested on node page with 21 comments
Without patch
First run: 1468ms, 35MB, 580 queries in 425 ms
Second run: 456ms, 25MB, 135 queries in 48 ms
Third run: 226ms, 25MB, 95 queries in 25 ms
With patch
First run: 1520ms, 40MB, 596 queries in 520 ms
Second run: 413ms, 35MB, 139 queries in 64 ms
Third run: 600ms, 35MB, 99 queries in 77 ms
I anybody knows a better way to test this, let me know
If profiling using xhprof is needed let me know and I give it a try.
Comment #57
andypost@attiks I suggest to use one of scripts from https://www.drupal.org/contributor-tasks/profiling that will allow you to get more precised numbers
Comment #58
attiks commentedI ended up using xhprof directly and looked at Drupal\comment\CommentManager::getCountNewComments, both with and without the patch the walltime is the same, I'll try creating a full report once my dockers start behaving.
ps: The major difference is in sql queries: an extra join and a cast to a varchar, so I don't expect to see any difference
Comment #59
deepakaryan1988Comment #60
deepakaryan1988Rerolled patch #49
Comment #61
deepakaryan1988Comment #63
attiks commenteddeepakaryan1988 thanks for reroll, i think you missed some parts, can you check?
Comment #64
deepakaryan1988@attiks yeah sure!
I will check it tomorrow!!
Comment #65
jelle_sSince there's been no reaction from deepakaryan1988, I'll take a stab at a reroll.
Comment #66
jelle_sShould be just a straight reroll.
Leaving it assigned to me. I'll have a look at writing updates.
Comment #68
jelle_sFixed the test and added an update function.
Comment #69
deepakaryan1988@Jelle_S Thanks for patch.
I was quite buzy.
Comment #70
attiks commentedI finally found to time to push our external entities project for d8 (https://www.drupal.org/project/external_entities), it would be nice to be able to use core comment module. It works with the above patch.
Comment #71
andypostThis is a feature so 8.1 and later
And because there's serious data model change this could go to 9.x
Anyway we need to solve the issue with attaching comments to entities that have non-integer PKs
Comment #75
sylus commentedHere is my first go at trying to get this re-rolled. I tested it with my existing comment types and they all got switched to use entity reference.
There were unfortunately a few adjustments that had to be made to the earlier patch:
1) For some reason updateEntityType was never called so when calling comment_update_8302 it would always say "base table or view not found" as the commented_update_8302 tabe was never created. Not sure if this was a change in 8.3.x. To solve this I looked at other hook_update_n invocations and did the following:
2) I converted all array()'s to short array syntax.
3) There were a few psr4 namespaces removed since last patch that were needed so I added them back mostly just:
4) Slightly updated rdf module patch lines
5) I couldn't the relevant hal + migrate_drupal files to patch
6) I think there are going to be quite a few test failures as were on my local but wanted to post progress.
Comment #77
sylus commentedOne minor correction to views/filter/UserUid.php. Retriggering.
Comment #79
sylus commentedThis patch should fix the views.comment_recent schema issues and ForumIndexStorage missing $nid which was my error. ^_^
Comment #81
sylus commentedFix for the "Undefined variable: entity_test" for CommentCacheTagsTest.php is now called:
I can handle the remaining code quality issues in next patch but unsure about bulk of rest of issues.
I do know there are some changes to be made in both HAL + Rest module to switch entity_id to commented_entity_test.
Any guidance on what else I missed would be appreciated ^_^
Comment #83
sylus commentedOkay I thought I would post status here and get any opinions. I was talking about this issue with a few drupal developers on and offline and the general consensus was I should be moving the comments + this patch functionality to a new contrib module likely called "external_comments".
Unfortunately I need this workflow to provide comments / ratings to external entities via CKAN / SOLR and expose them restfully as a required case. The worry with this patch would be the potential need to update for every core release and general maintenance issues.
I'll still be working on updating this patch but thought I would mention intentions to create a new contrib with this extended functionality and obviously pointing to the modules existence to only provide primary key as string storage.
Thoughts? :)
Comment #96
davidwbarratt commentedI think moving all of
https://git.drupalcode.org/project/drupal/-/blob/38d18a4f1f172a12374228c...
into
https://git.drupalcode.org/project/drupal/-/blob/38d18a4f1f172a12374228c...
would basically fix this?