Hello forum members,
I have a problem with the plugin “Comments”. I have the plugin installed and activated. Also in the file: item.html.twig the following line of code inserted:
{% include ‘partials/comments.html.twig’ with {‘page’: page} %}
Unfortunately, I do not get an issue or reaction. Does anyone have a similar problem or does anyone have an idea why it might be.
Kind regards
David
@codiergott If the route of the page on which you want to add comments does not start with ‘/blog’, you need to add that route in ‘user/config/plugins/comments.yaml’ like:
enable_on_routes:
- /blog
- /myroute
Hello,
Thank you for your answer. I have already adjusted the values, without success. I changed it as follows:
https: // meineurl / techwiki
I registered - ‘\techwiki’. I even tried with -’\ '. Everything without success!
@codiergott Comments plugin uses the following code to test if comments should be shown:
if (!in_array($path, $disable_on_routes)) {
if (in_array($path, $enable_on_routes)) {
$this->enable = true;
} else {
foreach($enable_on_routes as $route) {
if (Utils::startsWith($path, $route)) {
$this->enable = true;
break;
}
}
}
}
‘$path’ is the path of the page you are visiting and is relative, like ‘/techwiki/mytopic’.
‘$route’ is the route listed in ‘enable_on_route’ and should also be relative.
Note the forward slashes…
If the url of the blog is ‘https://meineurl/techwiki’, the settings in ‘comments.yaml’ should be like:
enable_on_routes:
- /techwiki
Every blog topic below ‘/techwiki’ should now contain comments.
If this doesn’t help I have the following questions:
- Which theme are you using?
- What is the folder structure?
- What is the name of the page (*.md) that should contain the comments?
Thank you for your help. The ‘enable_routes’ I have checked and are entered correctly. I think I have another problem here. I added the following lines of code in the comments.html.twig, in the Comments plugin folder:
Try to load plugin!
{% if grav.twig.enable_comments_plugin%}
{% set scope = scope?: 'data.' %}
Plugin loaded !!
<H3> {{ 'PLUGIN_COMMENTS.ADD_COMMENT' | t}} </ h3>
It will only load “Try plugin!” displayed. The plugin is activated, but apparently not recognized or I see that wrong?
Kind regards
David
@codiergott Apparantly, the variable ‘enable_comments_plugin’ is not set.
This variable is set in ‘comments.php’ line 76:
$this->grav['twig']->enable_comments_plugin = $this->enable;
The variable ‘$this->enable’ is set in:
if (!in_array($path, $disable_on_routes)) {
if (in_array($path, $enable_on_routes)) {
$this->enable = true;
} else {
foreach($enable_on_routes as $route) {
if (Utils::startsWith($path, $route)) {
$this->enable = true;
break;
}
}
}
}
This leads me to suspect that the routes you have set in ‘comments.yaml’ are not correct.
Do you know how to debug and read the variables $path and $route?
If not, you could print the variables to the console of the browser. Change the above script like:
if (!in_array($path, $disable_on_routes)) {
if (in_array($path, $enable_on_routes)) {
$this->enable = true;
} else {
foreach($enable_on_routes as $route) {
echo "<script>console.log('$path | $route');</script>"; // Add this line
if (Utils::startsWith($path, $route)) {
$this->enable = true;
break;
}
}
}
}
Refresh the browser and open the console of the browser (How to for: Chrome, Firefox, Safari). You should see something like the following:
/techwiki/mytopic | /techwiki
Thanks for the code. He helped me a lot. I receive the following edition:
/techwiki/sonstiges/dyndns-domain-mit-ssl-support-auf-einer-fritzbox-einrichten | /blog
How can I change the second value? Find this nowhere in the settings. Adding / blog to the enable_on_routes will not work either.
Could you help me again?
Kind regards
David
@codiergott
-
The values for ‘$path | $route’ are written to the console. In your case:
/techwiki/sonstiges/dyndns-domain-mit-ssl-support-auf-einer-fritzbox-einrichten | /blog
-
‘/techwiki/sonstiges/dyndns-domain-mit-ssl-support-auf-einer-fritzbox-einrichten’ is the value of $path which is the url of the page on which you want to see comments.
-
‘/blog’ is the value of $route which is looping through the values of ‘enable_on_routes’ in either ‘/user/config/plugins/comments.yaml’ or the original ‘/user/plugins/comments/comments.yaml’
The default values in ‘/user/plugins/comments/comments.yaml’ are:
enable_on_routes:
- '/blog'
Because the console only shows the entry /blog, you probably haven’t added ‘/techwiki’ to enable_on_routes in the right place.
You can override the default settings of ‘comments’ plugin by copying the file ‘/user/plugins/comments/comments.yaml’ to ‘/user/config/plugins/comments.yaml’ and update the new ‘comments.yaml’ as follows:
enable_on_routes:
- '/blog' // This value may be removed if you're not going to use /blog in your site.
- /techwiki