Jump to content

Quick guide to the comments system


ryan

Recommended Posts

I'm trying to add a comments section for specific posts on my blog, but ever since adding the comments field to the post template, the admin panel is no longer running the Reno theme, and an attempt to load up access/users throws this:

Error: Uncaught Error: Call to a member function has() on null in /wire/core/Users.php:99
Stack trace:
#0 /wire/core/PagesType.php(265): ProcessWire\Users->loaded(Object(ProcessWire\User))
#1 /wire/core/PagesType.php(395): ProcessWire\PagesType->find('id>0, sort=name')
#2 /wire/modules/Inputfield/InputfieldSelector/InputfieldSelector.module(386): ProcessWire\PagesType->getIterator()
#3 /wire/modules/Inputfield/InputfieldSelector/InputfieldSelector.module(1680): ProcessWire\InputfieldSelector->setup()
#4 [internal function]: ProcessWire\InputfieldSelector->___render()
#5 /wire/core/Wire.php(374): call_user_func_array(Array, Array)
#6 /wire/core/WireHooks.php(549): ProcessWire\Wire->_callMethod('___render', Array)
#7 /wire/core/Wire.php(399): ProcessWire\WireHooks->runHooks(Object(P (line 99 of /wire/core/Users.php) 

If I remove the comment field from the template, the error persists.

Here is the line in question:

if(!$roles->has("id=$guestID")) $page->get('roles')->add($this->wire('roles')->getGuestRole());

Any ideas?

Update: And now that I've logged out, I can't log in again - entire site is showing an internal server error, which I assume is the same as the above.

Update: No, now the error is this:

2016-12-28 18:32:55	guest	https://blog.rockett.pw/?/	Error: 	Exception: You do not have permission to execute this module - ProcessPageView (in /wire/core/Modules.php line 1236)

?

Link to comment
Share on other sites

1 minute ago, adrian said:

Looks like you are not alone:

Yeah - I saw those. And no replies!! Also bumped into the issue on the old GH repo, but Ryan is unable to reproduce.

What's interesting is that this only happened when I added the field to the template, and that process took a lot longer than normal...

Link to comment
Share on other sites

4 minutes ago, adrian said:

Any chance it's a third party module? Maybe you could rename the modules db table and the site/modules directory, and clean up the filecompiler cache directory. Maybe also the caches db table?

It could be, but I doubt it for some reason - everything was running smoothly until I made the field assignment. I have cleared the compiler cache in the directory and the table. Will try those renames now and see what happens.

Link to comment
Share on other sites

I think I'm going to have to merge the new data with my local install manually from the DB and re-upload.

So much for adding comments. Maybe I'll use Disqus instead.

Update: Site is up and running again. Disqus added. Thanks for the help Adrian. Looks like this is something that couldn't have been fixed too easily.

And I do still have the broken site, if you'd like me to try anything. :)

Link to comment
Share on other sites

  • 5 weeks later...

Maybe somebody can help me with this. I tried the following code for paginated comments:

 $commentLimit = 2; // comments to display per page
    $start = ($input->pageNum - 1) * $commentLimit;
    $desc = false;
    $selector = "page=$page, sort=-created, start=$start, limit=" . ($commentLimit + 1);

// find the comments. replace "comments" with the name of your comments field
    // I also used the new find command and not the deprecated findComments
    $comments = FieldtypeComments::find("comments", $selector);

in one of my template.php, but it throws an exeption:

Class 'FieldtypeComments' not found

What is wrong?

Link to comment
Share on other sites

On 28/01/2017 at 5:03 AM, jmartsch said:

in one of my template.php, but it throws an exeption:


Class 'FieldtypeComments' not found

What is wrong?

That's a namespace problem - for some reason your template is not being compiled with the ProcessWire namespace. You can add the namespace...

$comments = ProcessWire\FieldtypeComments::find("comments", $selector);

...but you shouldn't call the find() method statically anyway. The comment block for the deprecated findComments() explains how the find() method should be called:

@deprecated Use $field->type->find($field, $selectorString) instead.

 

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

Inside /wire/modules/Fieldtype/FieldtypeComments/FieldtypeComments.module there's a commented section (with @todo comments) for enabling textformatter options, again inside /wire/modules/Fieldtype/FieldtypeComments/Comment.php, textformatter settings are bypassed and comment is presented as is.

When I uncomment these sections, and add a few formatters, they work just fine :).
Is there any reason why these sections are commented out?

Also, is there a way for people to delete their comments?

  • Like 1
Link to comment
Share on other sites

Where to go?

<?php echo $page->comments->render();?>
Quote

Error: Call to a member function render() on a non-object (line 26 of C:\wamp\www\processwire-master\site\assets\cache\FileCompiler\site\templates\basic-page.php) 

 

Link to comment
Share on other sites

var_dump($page->comments); null
Quote

namespace ProcessWire;
Compile Error: Namespace declaration statement has to be the very first statement in the script (line 27 of C:\wamp\www\processwire-master\site\assets\cache\FileCompiler\site\templates\basic-page.php) 

 

 

Link to comment
Share on other sites

Okay, after some remote debugging collaboration with @Timea over Teamviewer, it turns out there was a typo where he/she created/renamed the comments field to Comments and tried to render it using $page->comments where he/she should've used $page->Comments instead. After renaming the field back to lowercase comments, error was fixed :)

  • Like 3
Link to comment
Share on other sites

  • 4 months later...

Hey everyone. I was reading carefully today almost any post about custom comment listing and forms styling and am super thankful to everyone that shared their knowledge so that a newbie like me could manage to get the comment listing properly applying my custom styles.

As far as the the Comment header I have required to post the number of persons that have commented the page, I was facing an issue of singular/plural of the word persons (in my case but could be comment/s etc.) of the count. Looking for a lazy way to achieve that, I found one small function that takes the check for me. If someone is interested to test it, feel free to add this code to your _functions.php or include it in your template code:

//Original code: https://stackoverflow.com/questions/4728933/function-switching-between-singular-and-plural
function plural( $amount, $singular = '', $plural = 's' ) {
    if ( $amount === 1 ) {
        return $singular;
    }
    return $plural;
}

And in my template comments rendering call I used it like this:

	<?php 
		$com_heading = '';
		$count = count($page->comments);
		$person = ' person' . plural($count);
		if($count) { 
			$com_heading = "Only " . $count . $person . " commented this recipe. Be the next one!"; 
		}
		echo $page->comments->render(array(
		'headline' => '<h3 class=\'lined\'>' . $com_heading . '</h3>',
		)); 
	?>

Maybe there is an easier/smarter way to achieve this, but this works fine for my needs and thought someone might find it useful one day...

Link to comment
Share on other sites

@MilenKo You can just use translation functions for this:

echo _n('person', 'persons', $page->comments->count);

From the core:

// LanguageFunctions.php

/**
 * Perform a language translation with singular and plural versions
 * 
 * @param string $textSingular Singular version of text (when there is 1 item)
 * @param string $textPlural Plural version of text (when there are multiple items or 0 items)
 * @param int $count Quantity of items, should be 0 or more.
 * @param string $textdomain Textdomain for the text, may be class name, filename, or something made up by you. If omitted, a debug backtrace will attempt to determine automatically.
 * @return string Translated text or original text if translation not available.
 *
 */
function _n($textSingular, $textPlural, $count, $textdomain = null) {
	return $count == 1 ? __($textSingular, $textdomain) : __($textPlural, $textdomain); 	
}

 

  • Like 2
Link to comment
Share on other sites

@fbg13 & @abdus I really liked your approach as it allows much more simplicity and would avoid bulking up the _function.php . So for the record, here is my code that is fully operational and now shorter and simpler:

	<?php 
		$com_heading = '';
		
		//Counts the number of comments to list
		$count = count($page->comments);
		
		//Define comment header text
		$com_heading = "Only $count " . _n('person', 'persons', $page->comments->count) . " commented this recipe. Be the next one!"; 
		
		//Render comment list
		echo $page->comments->render(array(
		
		//Insert the custom code for the CommentList headline
		'headline' => '<h3 class=\'lined\'>' . $com_heading . '</h3>',
		)); 
	?>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...