$CommentsForm doesn't show for DataObjects

Silverstripe Version:
4.9

Question:
Hi there,

I’ve extended a PostObject which is a simple DataObject with the Comments module and I can see fields created in the database. When I use the $CommentsForm it displays properly on the pages of SiteTree like the homepage, but it doesn’t show anything for PostObject. There’s nothing special indicated in the documentation so can’t tell if there’s something to do. PostObjects have the Comments tab in the back end, they provide comments but nothing on the front end, the form doesn’t appear for PostObject

Details of your query go here

[code]

Config for DataObjects


App\Hub\PostObject:
  extensions:
    - SilverStripe\Comments\Extensions\CommentsExtension
  comments:
    enabled: true # Enables commenting to be disabled for a specific class (or subclass of a parent with commenting enabled)

and for HTML side

<div class="CommentsInterface">
	$CommentsForm
</div>

When asking questions which involve specific modules, please include a link to the module(s) that the question is about. I assume in this case you’re using silverstripe/comments?

That configuration looks correct - at this stage I’d recommend stepping through with a debugger (or else just outputting some debug comments from the CommentsForm() method) to see if that method is even getting triggered, and if so, whether it’s being tripped up by the enabled check, for example.

Hi thanks for the answer, as there’s only one Comment module for Silverstripe 4, yes I talk about this one https://github.com/silverstripe/silverstripe-comments/tree/master

I tried everything, even to customize with renderWith but nothing works, I’m able by creating comments in the database to make them appear as comments of the DataObject but the CommentsForm still doesn’t seem to work when attached to the DataObject.

I tried to create a new form by myself but can’t find a way to pass the ID of the DataObject to the form. I’ve seen many others asking the same question and I didn’t find anything as an answer to this. I’m not a complete beginner nor a super savvy in SS4 but without a working example I’m more or less guessing that I’m following the right process. I’ve already lost 2 evenings just searching for a hint of answer. Any clue is welcome at this point.

Here it is, directly from @GuySartorelli comment above:

I’d recommend stepping through with a debugger (or else just outputting some debug comments from the CommentsForm() method) to see if that method is even getting triggered, and if so, whether it’s being tripped up by the enabled check, for example.

1 Like

I tried some debugging already, will continue testing with a debugger. For the time being it seems that calling the CommentsForm always triggers the form of the page holder. I will keep digging into this, I already checked all the options I found but maybe I missed something somewhere.

I checked with DebugBar, the template which the form calls to be rendered with “/vendor/silverstripe/comments/templates/CommentsInterface.ss” does appear, and only when the form is present in the template of the DataObject. Therefore it should work but there’s nothing in between the html tags where the form is meant to appear.

Ok so I found the solution

In case someone stumbles upon the same issue:

In CommentsExtension.php, the function getsCommentEnabled return false when there’s no owner which obviously on a DataObject may not happen. I changed it to return true instead and the form magically appears. From there, tweak the function at your needs.

public function getCommentsEnabled()
{
// Don’t display comments form for pseudo-pages (such as the login form)
if (!$this->owner->exists()) {
return true;
}

IMO, changing vendor code is just bad™.

owner is the instance the extension is applied to, and it is always defined. getCommentsEnabled returns false only if the DataObject is not present in the database, and IMO this is correct: you need to bind the comments to something.

// In CommentsExtension.php
if (!$this->owner->exists()) {
    return false;
}

// In DataObject.php
public function exists()
{
    return (isset($this->record['ID']) && $this->record['ID'] > 0);
}

My guess is you are trying to show comments on an unsaved DataObject (i.e. without an ID).

1 Like

Ok, I will try this solution. The DataObject has an ID and everything works fine outside of the form not showing up. I guess the function exists() is returning false instead of true for some reason

I can confirm the solution does work, and thanks a lot for your support, I wouldn’t be so far in my project without your answers.

This post was flagged by the community and is temporarily hidden.