Jump to content
ryan

Quick guide to the comments system

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)

?

Share this post


Link to post
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...

Share this post


Link to post
Share on other sites

Have you tried to debug the contents of $page that is passed to the loaded() method in the line above?

Share this post


Link to post
Share on other sites
4 minutes ago, adrian said:

Have you tried to debug the contents of $page that is passed to the loaded() method in the line above?

I can't now as guest doesn't have the permission to view a page, by the looks of it. Going to debug modules.php now.

Share this post


Link to post
Share on other sites

I've had a look at the schemas, and it appears everything is fine. In fact, everything matches my development server, except for whatever has been added. In terms of the permissions for ProcessPageView, I can't see anything wrong there either...

Not really sure how to proceed...

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Nah, doing that throws:

Error: 	Uncaught Error: Call to a member function execute() on null in /index.php:55 Stack trace: #0 {main}   thrown (line 55 of /index.php)

That line is part of the PW bootstrap:

echo $process->execute($config->internal);

 

Share this post


Link to post
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. :)

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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) 

 

Share this post


Link to post
Share on other sites
2 minutes ago, Timea said:

Call to a member function render()

Does basic-page template have comments field?

Share this post


Link to post
Share on other sites

Can you post the output of

var_dump($page->comments);

Also, try adding namespace

<?php namespace ProcessWire;

on the first line of basic-page.php

Share this post


Link to post
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) 

 

 

Share this post


Link to post
Share on other sites

You should place <?php namespace ProcessWire to very first line of the script (to the opening <?php tag), it should look like this

phpstorm64_2017-04-25_20-32-41.thumb.png.f7a1b24e6f5a7915245c8e2ac844ac61.png

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites
1 hour ago, MilenKo said:

was facing an issue of singular/plural of the word persons

  echo (count($page->comments) > 1) ? 'persons' : 'person';

 

  • Thanks 1

Share this post


Link to post
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

Share this post


Link to post
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>',
		)); 
	?>

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...